Skip to content

Latest commit

 

History

History
 
 

alias

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

AWS Lambda Alias

Terraform module, which creates Lambda alias as well as takes care of async event configuration and Lambda permissions for alias.

Lambda Alias is required to do complex Lambda deployments, eg. using external tools like AWS CodeDeploy.

This Terraform module is the part of serverless.tf framework, which aims to simplify all operations when working with the serverless in Terraform.

Usage

Lambda Function and statically configured alias with the version of Lambda Function

module "lambda_function" {
  source = "terraform-aws-modules/lambda/aws"

  function_name = "my-lambda1"
  handler       = "index.lambda_handler"
  runtime       = "python3.8"

  source_path = "../src/lambda-function1"
}

module "alias_no_refresh" {
  source = "terraform-aws-modules/lambda/aws//modules/alias"

  refresh_alias = false

  name = "current-no-refresh"

  function_name    = module.lambda_function.lambda_function_name
  function_version = module.lambda_function.lambda_function_version

  allowed_triggers = {
    AnotherAPIGatewayAny = {
      service    = "apigateway"
      source_arn = "arn:aws:execute-api:eu-west-1:135367859851:abcdedfgse/*/*/*"
    }
  }
}

Lambda Alias (auto-refreshing when version changed externally)

This is useful when doing complex deployments using external tool.

module "alias_refresh" {
  source = "terraform-aws-modules/lambda/aws//modules/alias"

  name          = "current-with-refresh"
  function_name = module.lambda_function.lambda_function_name
}

Lambda Alias (using existing alias)

This is useful when extra configuration on existing Lambda alias is required.

module "alias_refresh" {
  source = "terraform-aws-modules/lambda/aws//modules/alias"

  name          = "current-with-refresh"
  function_name = module.lambda_function.lambda_function_name
}

module "alias_existing" {
  source = "terraform-aws-modules/lambda/aws//modules/alias"

  use_existing_alias = true

  name          = module.alias_refresh.lambda_alias_name
  function_name = module.lambda_function.lambda_function_name

  allowed_triggers = {
    AnotherAwesomeAPIGateway = {
      service    = "apigateway"
      source_arn = "arn:aws:execute-api:eu-west-1:999967859851:aqnku8akd0/*/*/*"
    }
  }
}

Conditional creation

Sometimes you need to have a way to create resources conditionally but Terraform does not allow usage of count inside module block, so the solution is to specify create arguments.

module "lambda" {
  source = "terraform-aws-modules/lambda/aws//modules/alias"

  create = false # to disable all resources

  create_async_event_config                 = false  # to control async configs
  create_version_async_event_config         = false
  create_qualified_alias_async_event_config = false

  create_version_allowed_triggers         = false # to control allowed triggers on version
  create_qualified_alias_allowed_triggers = false # to control allowed triggers on alias

  # ... omitted
}

Examples

  • Alias - Create Lambda function and aliases in various combinations with all supported features.

Requirements

Name Version
terraform >= 0.13.1
aws >= 3.35

Providers

Name Version
aws >= 3.35

Modules

No modules.

Resources

Name Type
aws_lambda_alias.no_refresh resource
aws_lambda_alias.with_refresh resource
aws_lambda_function_event_invoke_config.this resource
aws_lambda_permission.qualified_alias_triggers resource
aws_lambda_permission.version_triggers resource
aws_lambda_alias.existing data source

Inputs

Name Description Type Default Required
allowed_triggers Map of allowed triggers to create Lambda permissions map(any) {} no
create Controls whether resources should be created bool true no
create_async_event_config Controls whether async event configuration for Lambda Function/Alias should be created bool false no
create_qualified_alias_allowed_triggers Whether to allow triggers on qualified alias bool true no
create_qualified_alias_async_event_config Whether to allow async event configuration on qualified alias bool true no
create_version_allowed_triggers Whether to allow triggers on version of Lambda Function used by alias (this will revoke permissions from previous version because Terraform manages only current resources) bool true no
create_version_async_event_config Whether to allow async event configuration on version of Lambda Function used by alias (this will revoke permissions from previous version because Terraform manages only current resources) bool true no
description Description of the alias. string "" no
destination_on_failure Amazon Resource Name (ARN) of the destination resource for failed asynchronous invocations string null no
destination_on_success Amazon Resource Name (ARN) of the destination resource for successful asynchronous invocations string null no
function_name The function ARN of the Lambda function for which you want to create an alias. string "" no
function_version Lambda function version for which you are creating the alias. Pattern: ($LATEST|[0-9]+). string "" no
maximum_event_age_in_seconds Maximum age of a request that Lambda sends to a function for processing in seconds. Valid values between 60 and 21600. number null no
maximum_retry_attempts Maximum number of times to retry when the function returns an error. Valid values between 0 and 2. Defaults to 2. number null no
name Name for the alias you are creating. string "" no
refresh_alias Whether to refresh function version used in the alias. Useful when using this module together with external tool do deployments (eg, AWS CodeDeploy). bool true no
routing_additional_version_weights A map that defines the proportion of events that should be sent to different versions of a lambda function. map(number) {} no
use_existing_alias Whether to manage existing alias instead of creating a new one. Useful when using this module together with external tool do deployments (eg, AWS CodeDeploy). bool false no

Outputs

Name Description
lambda_alias_arn The ARN of the Lambda Function Alias
lambda_alias_description Description of alias
lambda_alias_function_version Lambda function version which the alias uses
lambda_alias_invoke_arn The ARN to be used for invoking Lambda Function from API Gateway
lambda_alias_name The name of the Lambda Function Alias

Authors

Module managed by Anton Babenko. Check out serverless.tf to learn more about doing serverless with Terraform.

Please reach out to Betajob if you are looking for commercial support for your Terraform, AWS, or serverless project.

License

Apache 2 Licensed. See LICENSE for full details.