terraform-aws-modules/terraform-aws-lambda

repository·master·Indexed 21 days ago

https://github.com/terraform-aws-modules/terraform-aws-lambda

An AWS Lambda Terraform module that automates the creation of Lambda functions, layers, aliases, and complex deployment patterns. It supports dependency building and packaging for Python (pip, Poetry, UV), Node.js (npm), and Docker, with options for local or S3 storage. Features include VPC integration, container image deployment via ECR, code signing, and asynchronous event configuration using SQS and SNS.

Tokens
32.7K
Snippets
60
Records
109
Agent score
76%

What's inside terraform-aws-lambda

  1. Build deployment packages for AWS Lambda

    master

    This module provides various patterns for creating deployment packages for AWS Lambda functions. It supports multiple packaging scenarios, allowing you to automate the creation of artifacts directly within your Terraform workflow.

    Supported packaging scenarios include:

    • Python: Using pip requirements, Poetry, or UV.
    • Node.js: Using npm (with or without lock files).
    • Docker: Building packages within Docker containers.
    • Quiet Mode: Suppressing build output from tools like Poetry, pip, or npm by setting quiet_archive_local_exec = true.

    For runtimes that require compilation (like Rust, Go, or Java), refer to the Runtimes Examples in the repository.

  2. Build and push Docker images to ECR

    master

    The docker-build module automates the process of building a Docker image from a local Dockerfile and pushing it to an Amazon ECR repository. This is specifically designed to support AWS Lambda functions that are deployed using container images.

    If you require more granular control over your ECR resources, it is recommended to use the terraform-aws-ecr module instead.

  3. What is the docker-build wrapper module?

    master
    The wrappers/docker-build directory provides a module wrapper pattern. This pattern is designed to manage multiple instances of a module within a single configuration block. This is particularly useful in environments like Terragrunt where native Terraform for_each logic might not be feasible or where you want to manage multiple resources using a single configuration file without duplicating .hcl files for every instance.
  4. What is the Lambda module wrapper

    master
    The wrappers module is a single-module wrapper pattern designed to manage multiple instances of the root Lambda module within a single configuration block. This is particularly useful in environments like Terragrunt where native Terraform for_each logic might be difficult to implement across multiple files, or when you want to manage several resources using a single configuration file without duplicating terragrunt.hcl files for every instance.
  5. How building and packaging works

    master

    The module uses a Python script (package.py) to generate zip-archives for Lambda deployment packages. It calculates a hash based on file content to ensure that Lambda resources are only updated when the content actually changes, preventing unnecessary force-updates.

    Important Considerations:

    • Python Requirement: Ensure Python 3.6 or newer is installed on the machine running Terraform.
    • Concurrency Warning: If you call the module multiple times in a single execution with the same source_path, zip-archives may become corrupted due to concurrent writes.
    • Concurrency Solutions:
      1. Set different values for hash_extra to create unique archives.
      2. Create the package once using this module, then pass the resulting path to the local_existing_package argument for other resources.
  6. Use the deploy wrapper to manage multiple module instances

    master

    The wrappers/deploy module is a pattern designed to manage multiple instances of a single module within a single configuration block. This is particularly useful in environments like Terragrunt where native Terraform for_each might not be feasible for managing many distinct resources without duplicating configuration files.

    This wrapper does not add new AWS functionality; it simply iterates over a map of items and applies them to the underlying module (in this case, modules/deploy).

  7. Deploy Lambda functions with controlled rollouts

    master

    While simple deployments update the function directly, controlled deployments (rolling, canary, or rollbacks) require Lambda Aliases.

    • Simple Deployment: Updates the function code. If publish = true is set, a new Lambda version is created. You can invoke via version number or $LATEST.
    • Controlled Deployment: Uses aliases as pointers to specific versions or weighted versions.

    This module provides two specialized sub-modules for this:

    1. alias module: For managing alias configurations and updates.
    2. deploy module: For orchestrating deployments using AWS CodeDeploy, including waiting for completion.
  8. Test Lambda functions locally with AWS SAM CLI

    master

    You can use the AWS SAM CLI to test Lambda functions defined in your Terraform application. Use the --hook-name terraform flag to tell SAM CLI that the project is a Terraform application.

    Local Invocation: Run sam local invoke from your Terraform application root. You can provide the Terraform address or function name.

    On-cloud Testing (Accelerate): Use sam sync with the --watch flag to automatically update your AWS development account whenever you save changes to your local code.

    # Local testing
    sam local invoke --hook-name terraform module.hello_world_function.aws_lambda_function.this[0]
    
    # On-cloud testing (Accelerate)
    sam sync --hook-name terraform --watch
  9. Requirements for the docker-build module

    master

    Ensure your environment meets the following version requirements for Terraform and the necessary providers:

    ### Requirements
    
    | Name | Version |
    |------|---------|
    | terraform | >= 1.5.7 |
    | aws | >= 6.28 |
    | docker | >= 3.5.0 |
    | null | >= 2.0 |
    
    ### Providers
    
    | Name | Version |
    |------|---------|
    | aws | >= 6.28 |
    | docker | >= 3.5.0 |
    | null | >= 2.0 |
  10. Use the docker-build wrapper with Terragrunt

    master

    To use this wrapper with Terragrunt, define the source in the terraform block and provide configuration via the inputs block. The configuration is split into defaults (applied to all items) and items (the specific instances to create).

    terraform {
      source = "tfr:///terraform-aws-modules/lambda/aws//wrappers/docker-build"
      # Alternative source:
      # source = "git::git@github.com:terraform-aws-modules/terraform-aws-lambda.git//wrappers/docker-build?ref=master"
    }
    
    inputs = {
      defaults = {
        create = true
        tags = {
          Terraform   = "true"
          Environment = "dev"
        }
      }
    
      items = {
        my-item = {
          # can be any argument supported by the underlying module
        }
        my-second-item = {
          # can be any argument supported by the underlying module
        }
      }
    }
  11. Configure the deploy wrapper with Terragrunt

    master

    To use the deploy wrapper in Terragrunt, set the source to the wrapper path and define two main input blocks:

    1. defaults: A map of arguments applied to every instance created by the wrapper.
    2. items: A map where each key represents a unique instance name, and the value is a map of arguments specific to that instance. These arguments can be any valid input supported by the underlying modules/deploy module.

    Supported sources:

    • Registry: tfr:///terraform-aws-modules/lambda/aws//wrappers/deploy
    • Git: git::git@github.com:terraform-aws-modules/terraform-aws-lambda.git//wrappers/deploy?ref=master
    terraform {
      source = "tfr:///terraform-aws-modules/lambda/aws//wrappers/deploy"
    }
    
    inputs = {
      defaults = {
        create = true
        tags = {
          Terraform   = "true"
          Environment = "dev"
        }
      }
    
      items = {
        my-item = {
          # arguments supported by the underlying module
        }
        my-second-item = {
          # arguments supported by the underlying module
        }
      }
    }
  12. Deploy Lambda functions for various runtimes

    master

    This example demonstrates how to use the terraform-aws-lambda module to create deployment packages for specific programming languages and runtimes, including Rust, Go, Java, and .NET. The configuration automates the creation of the deployment package and provides executable Lambda functions for each runtime.

    For more general methods of building packages that are independent of the specific runtime, refer to the Build Package Examples.

    $ terraform init
    $ terraform plan
    $ terraform apply