Updated: Dec 3, 2020
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
Creating a local Lambda project using AWS SAM CLI
We'll be using node.js as our runtime for the lambda function
We'll add Typescript support for the Lambda function
As always, all code is available at GitHub
We need to install the below tools to get started:
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
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
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
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:
The above folder structure now contains two app.js files.
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
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