Setup CI/CD pipeline for serverless framework

Setup CI/CD pipeline for serverless framework
Photo by Tyler Lastovich / Unsplash

In this article, we will walk through how to set up a CI/CD pipeline for a serverless application using the Serverless Framework. The pipeline will use GitHub Actions as the CI/CD tool and AWS as the cloud provider. By the end of this article, you will have a fully functional CI/CD pipeline that can automatically deploy your serverless application whenever you push changes to the main branch of your GitHub repository.

Overview of Serverless Framework

The Serverless Framework is a popular open-source framework for building serverless applications. It supports multiple cloud providers such as AWS, Azure, and Google Cloud Platform, and allows developers to easily create, deploy, and manage serverless applications.

Serverless applications consist of small, independent functions that are deployed and executed on-demand, without the need for managing server infrastructure. The Serverless Framework abstracts away much of the complexity of serverless application development, providing developers with a simple and intuitive way to build scalable, resilient, and cost-effective applications.

Setting up the Project

Before we start setting up the CI/CD pipeline, let's first create a simple serverless application using the Serverless Framework. For this example, we will create a serverless application that provides an HTTP API using AWS Lambda and API Gateway.

First, make sure you have the following prerequisites installed on your machine:

  • Node.js (version 12.x or higher)
  • Serverless Framework (version 2.x or higher)
  • AWS CLI

To create a new Serverless project, open your terminal and run the following command:

sls create --template aws-nodejs --path my-service

This will create a new Serverless project in a directory called my-service, using the AWS Node.js template.

Next, navigate to the my-service directory and install the dependencies:

cd my-service
npm install

Finally, deploy the application to AWS:

sls deploy

This will deploy your serverless application to AWS. You can now test your application by invoking the provided API endpoint:

curl https://<api-gateway-id>.execute-api.<region>.amazonaws.com/dev/hello

You should receive a response like this:

{
  "message": "Go Serverless v1.0! Your function executed successfully!"
}

Setting up GitHub Actions

Now that we have a working serverless application, let's set up a CI/CD pipeline to automatically deploy changes whenever we push code to GitHub. We will use GitHub Actions as our CI/CD tool.

First, create a new repository on GitHub and clone it to your local machine:

git clone https://github.com/<your-username>/<your-repo-name>.git
cd <your-repo-name>

Next, create a new file in the root of your repository called .github/workflows/deploy.yml. This file will contain the definition of our GitHub Actions workflow.

Add the following contents to the file:

name: Deploy

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 14.x

      - name: Install dependencies
        run: npm install

      - name: Deploy to AWS
        run: sls deploy
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS

Configuring GitHub Secrets

Before we can use our workflow, we need to configure some secrets in our GitHub repository. These secrets will allow our workflow to authenticate with AWS and deploy our serverless application.

To configure the secrets, go to your GitHub repository and click on "Settings". Then, click on "Secrets" and click "New repository secret".

Create two new secrets with the following names:

  • AWS_ACCESS_KEY_ID: Your AWS access key ID
  • AWS_SECRET_ACCESS_KEY: Your AWS secret access key

Make sure to keep these secrets private and do not share them with anyone.

Testing the Workflow

Now that we have our workflow and secrets configured, let's test it out by making a change to our serverless application and pushing it to GitHub.

Open the handler.js file in your my-service directory and modify the response message:

module.exports.hello = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Hello, world!',
    }),
  };
};

Commit the changes and push them to GitHub:

git add handler.js
git commit -m "Update response message"
git push origin main

Once you push your changes, GitHub Actions will automatically trigger a new build and deployment. You can view the progress of the workflow by going to your repository's "Actions" tab.

Once the workflow completes, you can test your updated serverless application by invoking the API endpoint:

curl https://<api-gateway-id>.execute-api.<region>.amazonaws.com/dev/hello

You should receive a response like this:

{
  "message": "Hello, world!"
}

Conclusion

In this article, we walked through how to set up a CI/CD pipeline for a serverless application using the Serverless Framework and GitHub Actions. By following the steps outlined in this article, you should now have a fully functional CI/CD pipeline that can automatically deploy changes to your serverless application whenever you push code to GitHub.

Using a CI/CD pipeline is essential for ensuring that your serverless applications are deployed reliably and consistently. By automating the deployment process, you can reduce the risk of human error and minimize the time it takes to get your applications into production.

Thank you for reading!