Day 15: Automating Serverless Deployments with AWS Lambda and CI/CD


Welcome to Day 15 of our exciting "30 Days of AWS" journey! If you've been following along from the beginning, kudos to you for diving into the world of Amazon Web Services. Your dedication and curiosity are truly commendable.

For those who might have just joined us or are specifically interested in today's topic, a warm welcome to you as well! While each article in this series delves into a different facet of AWS, rest assured that they are all interconnected, building upon the knowledge we've been cultivating day by day.

If you're here for the first time, I encourage you to take a moment to catch up on our previous discussions. This will enhance your understanding and ensure a seamless flow as we dive deeper into the fascinating journey of AWS together.

In today’s installment, we will explore "Automating Serverless Deployments with AWS Lambda and CI/CD." AWS Lambda is a powerful service that lets you run code without provisioning or managing servers, making it ideal for automating small tasks or running serverless applications.

As always, feel free to engage, ask questions, and share your thoughts in the comments. Your participation is what makes this series vibrant and valuable. I’m thrilled to have you join us on this journey. Let’s get started!

What is AWS Lambda?

Let’s start with a technical definition:

AWS Lambda is a serverless compute service that lets you run code in response to events (like an HTTP request, file upload, or database change) without needing to manage servers. You only pay for the compute time you use, and there’s no need to worry about scaling or maintenance.

To put it in a simple way:

AWS Lambda is like having a chef on standby in your kitchen. This chef (your Lambda function) only springs into action when there’s an order (event) to fulfill. The best part? You don’t pay the chef when they’re just waiting around—only when they’re actively cooking!

Common Use Cases of AWS Lambda

  1. Automated File Processing:
    For example, automatically compressing and resizing images uploaded to an S3 bucket.

  2. Real-Time Data Transformation:
    Transforming incoming data streams before storing them in a database.

  3. Serverless APIs:
    Building entire backend services that automatically scale up or down based on incoming traffic.

  4. Scheduled Tasks:
    Running a task at specific intervals (e.g., a daily report generator).

How Does Lambda Work?

Here’s the technical workflow:

  1. You upload your code to Lambda.

  2. Lambda is triggered by an event (like an API call or an S3 file upload).

  3. The code runs inside a stateless environment (it doesn’t remember anything from previous runs).

  4. Lambda scales automatically based on the number of incoming events.

To put it in a simple way:

Think of Lambda as a pop-up restaurant. It only opens when a customer arrives and closes immediately after serving. If 10 customers come in, 10 mini pop-up stalls will open simultaneously to serve them. Once everyone is served, all stalls close, and you’re not charged for the time they weren’t serving.

Integrating AWS Lambda with CI/CD

Now that we have a solid understanding of AWS Lambda, let’s see how to automate its deployment using AWS CodePipeline and AWS CodeBuild. The goal is to create a pipeline that automatically deploys our Lambda function every time we push new code to our GitHub repository.

Step-by-Step: Building a Serverless CI/CD Pipeline

We’ll start with a simple Lambda function that prints a welcome message. Whenever we update the code, CodePipeline will automatically deploy the new version.

Step 1: Create a Basic AWS Lambda Function

  1. Go to the AWS Lambda Console.

  2. Click on Create Function and choose Author from scratch.

  3. Name the function welcomeLambda.

  4. Choose Node.js as the runtime.

  5. Click Create Function.

Step 2: Create the Lambda Function Code

In the function editor, replace the default code with the following:

exports.handler = async (event) => {
    console.log("Welcome to AWS Lambda!");
    return {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
};

Click Deploy.

Step 3: Create a GitHub Repository

  1. Go to GitHub and create a new repository named lambda-cicd.

  2. Clone the repository to your local machine.

  3. Add a new file named index.js:

     exports.handler = async (event) => {
         console.log("Welcome to AWS Lambda!");
         return {
             statusCode: 200,
             body: JSON.stringify('Hello from Lambda, automatically deployed using CI/CD!'),
         };
     };
    
  4. Commit and push the changes to GitHub.

Step 4: Create an S3 Bucket for Code Storage

  1. Go to the S3 Console and create a new bucket named lambda-code-bucket.

  2. Leave the permissions as default.

Step 5: Create a CodePipeline

  1. Go to the CodePipeline Console and click on Create Pipeline.

  2. Name your pipeline lambda-deploy-pipeline.

  3. Create a new service role and click Next.

Step 6: Add a Source Stage

  1. Choose GitHub as the source provider.

  2. Connect your GitHub account and select the lambda-cicd repository.

  3. Choose main as the branch and click Next.

Step 7: Add a Build Stage Using CodeBuild

  1. Choose AWS CodeBuild as the build provider.

  2. Create a new build project named LambdaBuildProject.

  3. Use the following buildspec.yml to package the Lambda function:

     version: 0.2
    
     phases:
       install:
         runtime-versions:
           nodejs: 12
       build:
         commands:
           - echo "Packaging Lambda function..."
           - zip -r function.zip index.js
     artifacts:
       files:
         - function.zip
    
  4. Click Next.

Step 8: Add a Deploy Stage to Lambda

  1. Add a Deploy Stage.

  2. Choose AWS Lambda as the deploy provider.

  3. Select the Lambda function (welcomeLambda).

  4. Click Next and then Create Pipeline.

Step 9: Test the Pipeline

  1. Make a change to the index.js file in your GitHub repository.

  2. Push the changes to main.

  3. Watch as CodePipeline triggers the build, packages the Lambda function, and deploys it automatically!

Summary

Today, we explored:

  • What AWS Lambda is and how it works.

  • Creating a simple Lambda function.

  • Using AWS CodePipeline and CodeBuild to automate the deployment of Lambda functions.

What’s Next?

In Day 16, we’ll integrate AWS Lambda with other AWS services like API Gateway and S3. We’ll see how to build serverless applications that respond to various events.

Stay tuned, and let’s keep this AWS learning journey going strong!


Hope you find this blog helpful. Please share your thoughts in the comments—it will help me refine and provide more insightful content. Happy Learning!

Connect with Me - LinkedIn - Twitter/X - Topmate