Browse the Repo

file-type-icon.circleci
file-type-icon_docs
file-type-iconexamples
file-type-iconapi-gateway-account-settings
file-type-iconlambda-build
file-type-iconpython
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvars.tf
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-iconlambda-service
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
file-type-iconterraform-cloud-enterprise-private-module-...

Browse the Repo

file-type-icon.circleci
file-type-icon_docs
file-type-iconexamples
file-type-iconapi-gateway-account-settings
file-type-iconlambda-build
file-type-iconpython
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvars.tf
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-iconlambda-service
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
file-type-iconterraform-cloud-enterprise-private-module-...
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 Build example

This folder shows an example of how to package code with dependencies and build steps and then deploy it as a Lambda function using the lambda module. The actual Python code in index.py is a simple example that uses an external library called requests to make HTTP requests and returns the results.

With AWS Lambda, your deployment package (zip file) must contain ALL of the dependencies for your app already bundled within it. Moreover, since Lambda functions run on Amazon Linux, all of those dependencies must be compiled specifically for Amazon Linux. This example creates the deployment package as follows:

  1. Build the code using Docker, using an Amazon Linux image as the base image, as shown in the example Dockerfile. This Docker image installs all of your dependencies and source code into the /usr/src/lambda folder.

  2. When developing and testing locally, you can run your Lambda code directly in the Docker image. You can mount the python/src directory from your host OS into /usr/src/lambda/src so that your local changes are visible immediately in the container.

  3. To deploy to AWS, you use docker cp to copy the /usr/src/lambda folder to a local path (see the build.sh script)and then run terraform apply to zip up that local path and deploy it to AWS.

How do you deploy this example?

First, build the deployment package:

  1. Install Docker.
  2. ./python/build.sh

Next, deploy the code with Terraform:

  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:

{
  "url": "http://www.example.com"
}

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. The example Python app includes a test_harness.py file that is configured to allow you to run your code locally. This test harness script is configured as the ENTRYPOINT for the Docker container, so you can test locally as follows:

cd python
docker build -t lambda-build-example .
docker run -it --rm lambda-build-example http://www.example.com

To avoid having to do a docker build every time, you can do all subsequent docker run calls with your local src folder mounted as a volume so that the Docker container always sees your latest source code:

docker run -it --rm -v src:/usr/src/lambda/src lambda-build-example http://www.example.com

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?