Skip to content

kokchuan101/lambda-terraform-helloworld

Repository files navigation

Lambda Terraform Helloworld

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.


Prerequisite

Local Testing

  • Install npm packages
cd hello-world
npm install
cd ..

AWS SAM CLI

  1. Ensure you are in root directory
sam local start-api --debug
  1. Test the result by going to http://127.0.0.1:3000/hello

Terraform to Localstack

  1. Boot up LocalStack
localstack start
  1. Ensure localstack container is running
docker ps

Alt text

  1. Go to terraform-localstack directory and copy terraform variable file
cd terraform-localstack
cp auto.tfvars.example auto.tfvars
  1. Init terraform
terraform init
  1. Verify lambda resources that will be created
terraform plan
  1. Create lambda resources in LocalStack
terraform apply
  1. Get REST API ID from the output and construct the url from

http://localhost:4566/restapis/${REST_API_ID}/prod/_user_request_/${REQUEST_PATH}

Alt text

For example go to http://localhost:4566/restapis/r37nr953pa/prod/_user_request_/hello

Deployment

AWS SAM CLI

sam deploy --guided

Terraform

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.

Reference

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published