This is a simple lambda project to:
- setup Terraform for lambda function instead of using AWS SAM CLI
- experiment applying Terraform in local machine using localstack
- create Github Action to orchestrate lambda resource in AWS and create CI/CD pipeline to deploy lambda function
Description below will cover AWS SAM CLI and Terraform implementation for both local testing and deployment.
- Node.js
- Docker
- AWS SAM CLI
- Terraform
- Localstack
- Install npm packages
cd hello-world
npm install
cd ..
- Ensure you are in root directory
sam local start-api --debug
- Test the result by going to http://127.0.0.1:3000/hello
- Boot up LocalStack
localstack start
- Ensure localstack container is running
docker ps
- Go to terraform-localstack directory and copy terraform variable file
cd terraform-localstack
cp auto.tfvars.example auto.tfvars
- Init terraform
terraform init
- Verify lambda resources that will be created
terraform plan
- Create lambda resources in LocalStack
terraform apply
- Get REST API ID from the output and construct the url from
http://localhost:4566/restapis/${REST_API_ID}/prod/_user_request_/${REQUEST_PATH}
For example go to http://localhost:4566/restapis/r37nr953pa/prod/_user_request_/hello
sam deploy --guided
Terraform Cloud and AWS IAM (client id and secret key) is setup according to this guide. Also add AWS client id and secret key to github repository secrets.
Github Actions setup includes terraform plan and terraform apply to orchestrate lambda resources. CI/CD pipeline deploys lambda function to AWS by zipping and executing aws lambda update-function-code
command.