top of page

Part 1 - AWS Lambda Development Environment with CI/CD


In this four part article, we'll explore setting up a local AWS Lambda environment as well as setting up a CI/CD pipeline using GitHub Actions and Terraform.

We'll build the below CI/CD pipeline that will enable a developer to check-in his code in GitHub. GitHub Actions workflow will then build it, execute test cases and finally deploy the code to AWS Lambda using Terraform.

In Part 1 of this series, we'll cover

  1. Creating a local Lambda project using AWS SAM CLI

  2. We'll be using node.js as our runtime for the lambda function

  3. We'll add Typescript support for the Lambda function

As always, all code is available at GitHub

Required Tools

We need to install the below tools to get started:

  1. Docker. Get Docker

  2. AWS CLI. Get AWS CLI and Configure AWS CLI


Initialize the Lambda Application

We can now use the SAM CLI to initialize the lambda application. Use the below command to create the application using the hello-world template:

SAM will now create a boilerplate folder structure for the lambda code. The root directory will be named aws-lambda-hello-world and will have the files as shown below:

Lets navigate into the aws-lambda-hello-world folder. We should now be able to run the lambda function by executing the below command:

We should see the below output. A status code of 200 indicates a successful invocation of the lambda function. We used the files under the events folder as an input to the lambda function

We now have a working local development environment for building lambda functions.

Adding Typescript support for our lambda function

Typescript is an object-oriented programming language developed and maintained by the Microsoft Corporation. It is a superset of JavaScript and contains all of its elements. It simplifies JavaScript code, making it easier to read and debug. For more information about Typescript, please checkout the Typescript website

To add Typescript support, navigate to the hello-world folder containing the package.json file and execute the below commands:

These commands should install the typescript library as well as the lambda and node types that we'll need to reference in our typescript code

Node.js does not natively understand the Typescript code. We will have to configure the typescript compiler to compile the typescript code we write into javascript code that the node.js runtime can execute

The typescript compiler requires a tsconfig.json file which contains instructions on how to compile typescript into javascript.

Lets create a tsconfig.json file in the hello-world folder. Please use the below tsconfig.json file as a reference

We can now add a build command under the scripts section in package.json to compile any typescript code that we write. Please see the below package.json file for reference

We are now ready to convert the lambda javascript function into a lambda typescript function. SAM auto-generated the lambda javascript function in the app.js file. We'll create an app.ts file with the same functionality using typescript syntax. Please note the file extensions. By convention, all typescript files have a .ts extenstion

Create an app.ts file in the same folder that contains the app.js file. Add the below contents to the app.ts file

We can now compile this typescript code by executing the below command:

npm runs the build script which invokes the tsc - the typescript compiler. The compiler searches for all files with the **/*.ts extension and complies them into javascript files. The newly compiled files are written to the dist folder. Our folder structure should now look something like below. Note the dist folder that contains new javascript files

The above folder structure now contains two app.js files.

  1. The app.js file under hello-world folder contains the original file created by the SAM CLI tool. This file is now redundant and should be deleted

  2. The app.js file under hello-world/dist folder. This is our latest and greatest file that will get deployed to AWS. We make changes to app.ts (Typescript file) with our typescript code and then compile it to app.js (Javascript file) under the dist folder

Also note the template.yaml file under the root folder. This file was created by the SAM CLI tool and still references the older app.js under the hello-world folder. Since we deleted the original app.js file, we need to provider template.yaml the location of our new app.js file.

Edit the template.yaml file and update the CodeUri section (Line#17 below) to point to hello-world/dist

Lets now add a "invoke-lambda "script to package.json to execute our new typescript code.

See the below package.json file for reference:

We can now execute our typescript compatible code and verify that we can execute the lambda function successfully.

Success! We now have a Typescript compatible Lambda function.

In Part 2 article, we'll configure JEST for writing unit-test cases and Webpack for bundling the lambda code as a deploy-able artifact

543 views0 comments
Post: Blog2_Post
bottom of page