Browse the Repo

file-type-icon.circleci
file-type-icon.github
file-type-icon.patcher
file-type-icon_docs
file-type-iconcodegen
file-type-iconexamples
file-type-iconmodules
file-type-iconapi-gateway-account-settings
file-type-iconapi-gateway-proxy-methods
file-type-iconapi-gateway-proxy
file-type-iconkeep-warm
file-type-iconlambda-edge-log-group
file-type-iconlambda-edge-multi-region-log-groups
file-type-iconlambda-edge
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvariables.tf
file-type-iconlambda-http-api-gateway
file-type-iconlambda
file-type-iconrun-lambda-entrypoint
file-type-iconscheduled-lambda-job
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.github
file-type-icon.patcher
file-type-icon_docs
file-type-iconcodegen
file-type-iconexamples
file-type-iconmodules
file-type-iconapi-gateway-account-settings
file-type-iconapi-gateway-proxy-methods
file-type-iconapi-gateway-proxy
file-type-iconkeep-warm
file-type-iconlambda-edge-log-group
file-type-iconlambda-edge-multi-region-log-groups
file-type-iconlambda-edge
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvariables.tf
file-type-iconlambda-http-api-gateway
file-type-iconlambda
file-type-iconrun-lambda-entrypoint
file-type-iconscheduled-lambda-job
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 Function Module

This module makes it easy to deploy and manage an AWS Lambda@Edge function. Lambda@Edge gives you a way to run code on-demand in AWS Edge locations without having to manage servers.

Lambda@Edge has the following limitations compared to regular Lambda (see the CloudFront Developer Guide for the full details):

  • The functions must not have any environment variables.
  • The execution timeout must not be higher than 30 seconds.
  • The function must be versioned in order to be a target for Cloudfront events.
  • The function must be deployed in the us-east-1 region.
  • The function runtime must be one of:
    • nodejs10.x or newer
    • python3.7 or newer

How do you use this module?

The general idea is to:

  1. Create a folder that contains your source code in one of the supported languages: Python, JavaScript, Java, etc (see Lambda programming model for the complete list).
  2. Use this lambda-edge module to automatically zip that folder, upload it to AWS, and configure it as a Lambda function.
  3. Trigger your Lambda function using one of the following options:
    1. AWS Console UI.
    2. AWS API.
    3. AWS CLI.
    4. API Gateway API you expose.
    5. An event source, such as a new Cloudfront event.

What is AWS Lambda?

AWS Lambda lets you run code without provisioning or managing servers. You define a function in a supported language (currently: Python, JavaScript, Java, and C#), upload the code to Lambda, specify how that function can be triggered, and then AWS Lambda takes care of all the details of deploying and scaling the infrastructure to run that code.

How do you add additional IAM policies and permissions?

By default, the lambda-edge module configures your lambda function with an IAM role that allows it to write logs to CloudWatch Logs. The ID of the IAM role is exported as the output iam_role_id and the ID of the lambda function is exported as the output function_arn, so you can add custom rules using the aws_iam_role_policy or aws_lambda_permission resources, respectively. For example, to allow your lambda function to be triggered by SNS:

module "my_lambda_function" {
  source = "git::git@github.com:gruntwork-io/terraform-aws-lambda.git//modules/lambda-edge?ref=v1.0.8"
  # (params omitted)
}

resource "aws_lambda_permission" "with_sns" {
  statement_id = "AllowExecutionFromSNS"
  action = "lambda:InvokeFunction"
  function_name = "${module.my_lambda_function.function_arn}"
  principal = "sns.amazonaws.com"
  source_arn = "${aws_sns_topic.default.arn}"
}

How to get the logs of your function

Lambda@Edge stores CloudWatch Logs in the AWS Regions closest to the location where the function receives traffic and is executed. That means a log group must be created in every region that have Regional Edge Caches. Instructions on how to do this can be found at the lambda-edge-multi-region-log-groups module. To see which regions are receiving traffic, you can find graphs of metrics for the function on the CloudFront console and choose your region there.

How to trigger this Lambda function from Cloudfront

This module deploys the Lambda function but doesn't create any CloudFront trigger. There are two ways to create those triggers:

  1. Using terraform. To link the Lambda@Edge function to the s3-cloudfront module from terraform-aws-static-assets, you can use the default_lambda_associations input variable.
  2. Manually from the AWS Console as described in the Lambda@Edge documentation

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?