Setting Up a NodeJs/TypeScript CI/CD Pipeline for AWS Lambda Using GitHub Action and Serverless

Introduction

When working with an AWS Lambda project, it’s common to change your code locally, and then upload it on AWS daily or at least a weekly basis. It’s very much true for projects that are medium or large and where new features and bugfixes are constantly being added. A CI/CD pipeline can help a lot in these cases, as the developers don’t have to push the code on AWS manually and don’t have to worry about stuff related to pushing. They can just do their work and forget about the unnecessary things. And it’s also very easy to create such a pipeline just using GitHub Actions and Serverless Framework.

Today we are going to create a pipeline to automate the deploy of a NodeJs project using GitHub Action and Serverless. I’m going to use Node as the runtime of the AWS Lambda and will be using TypeScript to write the lambda.

Source Code

You can get all the codes used in this project in this GitHub Repository. If you want, you can skip the post and get the codes and play around with them however you like.

Requirements

Before getting started with the post, you must have some knowledge on Serverless Framework which is used for deploying AWS Lambda functions. It also helps to configure the AWS services so that we don’t have to do them manually from the AWS console.

But if you aren’t familiar with it and want to get started, then you follow this quick-start guide for the Serverless Framework.

Before following this tutorial, make sure you have Serverless Framework installed and configured on your working environment. Also, you need to have a working GitHub account. If you don’t have any then create a GitHub account from here.

Now let’s get going with the tutorial…

Create AWS Lambda project with Serverless

First, we need to create a TypeScript based AWS Lambda project using the Serverless Framework CLI. Open up any terminal and enter the following command:

serverless create --template aws-nodejs-typescript --path <your-folder-name>
Here, the --template flag will generate some files for the given template. You can get more templates from here. For more info on serverless create command, check here.

The above command will create a new directory with the name you gave in <your-folder-name> and create some new files like handler.tsserverless.tspackage.jsontsconfig.jsonwebpack.config.js. The handler.tsfile contains some boiler code:

And the serverless.ts file contains the configuration for deploying the function on AWS. If want to know what can be configured with the file, take a look here.

Now go to the directory that was created and open cmd pointing the directory and run the following command:

npm install

This will install all necessary npm libraries.

Create Serverless Deploy Script

In the project directory, we need to configure the package.json file a bit to be able to deploy the lambda project on AWS Lambda directly using the npm script command. Here is the code after adding the npm script:

Note: Please make sure that the serverless npm library is present in the package.jsonfile. If not then you can add it using this command:

npm i serverless -D

Now it’s time to deploy the code!!

Test Deploy Manually

To test that the npm script is working as well as the main code, let’s deploy the lambda project manually. For that, go to the root directory, open any terminal, and execute:

npm run-script deploy

If everything is working as expected, then you will see this message on your terminal:

Serverless deploy response

And if you go to the address given in the endpoints section, we will see this:

If all of these went well, then we are good to create the GitHub Action and create the pipeline.

Create GitHub Action

For making GitHub Action work, we have to put the workflow file into a specific folder in the desired project directory. We will create the folders and the workflow/action file using some simple steps:

  1. In the root directory of the project, create a folder named .github
  2. Go the the .githubfolder and create another folder named workflows
  3. Go to the newly created folder and create a new file named aws_lambda_deploy.yamland copy-paste the following code snippet:

This code will be triggered when the action workflow when you push any code on the master branch on GitHub. You can learn more options for the trigger from here.

Add AWS Secrets

To make the workflow work as intended, we need to add the AWS User access key and secret access key and set them inAWS_ACCESS_KEY_IDand AWS_SECRET_ACCESS_KEY respectively. We will set this up now.

Get the User Secrets from AWS Console

For getting the access key and secret access key, go to IAM service in the AWS Management Console. If you are not sure how to get them or need to create a new role, check this article.

Set the User Secrets in GitHub

To set the user secrets, use the following steps:

  1. Open your GitHub repository on any browser, then click on the “Settings” tab.
Select the “Settings” tab

2. Then on the left navigation panel, select “Secrets”.

3. In the “Secrets” page, press the “New Secret” button on the top right corner.

4. On the new page, put AWS_ACCESS_KEY_ID in the “Name” filed and put the access key value from the AWS IAM.

5. Repeat the 3rd step for adding AWS_SECRET_ACCESS_KEY secret.

Sit Back and Enjoy!

And we are done! If you have made this far, that means you have successfully created a pipeline to deploy your AWS Lambda project on AWS! Now it’s time to see it in action. Make some changes in your handler.tsfile and push it on the master branch. I made the following change:

import "source-map-support/register";
import { APIGatewayProxyHandler } from "aws-lambda";
export const hello: APIGatewayProxyHandler = async (event, _context) => {
return {
statusCode: 200,
body: JSON.stringify(
{
message:
"This code is deployed directly from the CI/CD pipeline using GitHub Action and Serverless!",
httpMethod: event.httpMethod,
},
null,
2
),
};
};

After you push the code, you can track the pipeline by going to the “Actions” tab on the GitHub repository and selecting the latest workflow. If you see a green checkmark (✔️) beside the workflow, that means the workflow was successfully executed and you have deployed your code without doing any extra steps!

You can also verify that by going to the URL you got before when we deployed the code manually. Here is mine:

This is all you need to create a basic CI/CD pipeline to deploy on AWS Lambda. You can also add code unit testing, lint checking, and various other things by modifying the GitHub Action workflow according to your needs, but this post is just to get you started.

Hope this will help you with your developer journey. Happy coding! 😃

Picture of Asif Imtiaj Safi

Asif Imtiaj Safi

Software Development Engineer

Hire Exceptional Developers Quickly

Share this blog on

Hire Your Software Development Team

Let us help you pull out your hassle recruiting potential software engineers and get the ultimate result exceeding your needs.

Contact Us Directly

Address:

Plot # 272, Lane # 3 (Eastern Road) DOHS Baridhara, Dhaka 1206

Talk to Us
Scroll to Top