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-iconlambda-dead-letter-queue
file-type-iconlambda-edge
file-type-iconnodejs
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvars.tf
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-iconlambda-dead-letter-queue
file-type-iconlambda-edge
file-type-iconnodejs
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvars.tf
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@Edge example

This folder shows an example of how to use the lambda-edge module to create a Lambda function that logs incoming requests.

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.

We do not yet have the CloudFront trigger automated, so once the example is deployed, you'll have to configure the trigger manually.

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 (full format available in the Lambda@Edge Event Structure documentation):

{
  "Records": [
    {
      "cf": {
        "config": {
          "distributionId": "EDFDVBD6EXAMPLE",
          "requestId": "MRVMF7KydIvxMWfJIglgwHQwZsbG2IhRJ07sn9AkKUFSHS9EXAMPLE=="
        },
        "request": {
          "clientIp": "2001:0db8:85a3:0:0:8a2e:0370:7334",
          "querystring": "size=large",
          "uri": "/picture.jpg",
          "method": "GET",
          "headers": {
            "host": [
              {
                "key": "Host",
                "value": "d111111abcdef8.cloudfront.net"
              }
            ],
            "user-agent": [
              {
                "key": "User-Agent",
                "value": "curl/7.51.0"
              }
            ]
          },
          "origin": {
            "custom": {
              "customHeaders": {
                "my-origin-custom-header": [
                  {
                    "key": "My-Origin-Custom-Header",
                    "value": "Test"
                  }
                ]
              },
              "domainName": "example.com",
              "keepaliveTimeout": 5,
              "path": "/custom_path",
              "port": 443,
              "protocol": "https",
              "readTimeout": 5,
              "sslProtocols": [
                "TLSv1",
                "TLSv1.1"
              ]
            },
            "s3": {
              "authMethod": "origin-access-identity",
              "customHeaders": {
                "my-origin-custom-header": [
                  {
                    "key": "My-Origin-Custom-Header",
                    "value": "Test"
                  }
                ]
              },
              "domainName": "my-bucket.s3.amazonaws.com",
              "path": "/s3_path",
              "region": "us-east-1"
            }
          }
        }
      }
    }
  ]
}

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. There are multiple frameworks for local testing NodeJS Lambda functions, including:

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?