Browse the Repo

file-type-icon.circleci
file-type-iconexamples
file-type-iconlambda-build
file-type-iconlambda-dead-letter-queue
file-type-iconlambda-edge
file-type-iconlambda-keep-warm
file-type-iconlambda-s3-deployment-package
file-type-iconlambda-s3
file-type-iconimages
file-type-iconpython
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvars.tf
file-type-iconlambda-vpc
file-type-iconscheduled-lambda-job
file-type-iconmodules
file-type-icontest
file-type-icon.gitignore
file-type-icon.pre-commit-config.yaml
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME.md
file-type-iconowners.txt

Browse the Repo

file-type-icon.circleci
file-type-iconexamples
file-type-iconlambda-build
file-type-iconlambda-dead-letter-queue
file-type-iconlambda-edge
file-type-iconlambda-keep-warm
file-type-iconlambda-s3-deployment-package
file-type-iconlambda-s3
file-type-iconimages
file-type-iconpython
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvars.tf
file-type-iconlambda-vpc
file-type-iconscheduled-lambda-job
file-type-iconmodules
file-type-icontest
file-type-icon.gitignore
file-type-icon.pre-commit-config.yaml
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME.md
file-type-iconowners.txt
AWS Lambda

AWS Lambda

Deploy Lambda functions with Terraform. Supports uploading deployment packages, configuring environment variables, and scheduled functions.

Code Preview

Preview the Code

mobile file icon

README.md

down

Lambda S3 example

This folder shows an example of how to use the lambda module to create a Lambda function that can read an image from S3 and return the contents of that image base64 encoded.

How do you deploy this example?

To apply the Terraform templates:

  1. Install Terraform.
  2. Open vars.tf, set the environment variables specified at the top of the file, and fill in any other variables that don't have a default.
  3. Run terraform get.
  4. Run terraform plan.
  5. If the plan looks good, run terraform apply.

How do you test the Lambda function?

There are two ways to test the Lambda function once it's deployed:

  1. Test in AWS
  2. Test locally

Test in AWS

Open up the AWS Console UI, find the function, click the "Test" button, and enter test data that looks something like this:

{
  "aws_region": "us-east-1",
  "s3_bucket": "lambda-s3-example-images-test",
  "image_filename": "gruntwork-logo.png"
}

Click "Save and test" and AWS will show you the log output and returned value in the browser.

Test locally

The code you write for a Lambda function is just regular code with a well-defined entrypoint (the "handler"), so you can also run it locally by calling that entrypoint. test_harness.py is an example of a simple script you can run locally that will execute the handler, decode the base64-encoded image in the return value, and write it to disk:

python python/test_harness.py --region us-east-1 --bucket lambda-s3-example-images-test --filename gruntwork-logo.png

See also the lambda-build example to see how you can execute build and packaging steps for your code before uploading it using Terraform.

Questions? Ask away.

We're here to talk about our services, answer any questions, give advice, or just to chat.

Ready to hand off the Gruntwork?