Terraform Module for AWS Lambda Function
This is a stable example. It should successfully build out of the box
This examples does is built on Construct Libraries marked "Stable" and does not have any infrastructure prerequisites to build.
This module may be used to create Lambda Function resources in AWS cloud provider......
This module needs Terraform 0.12.29 or newer. You can download the latest Terraform version from here.
This module deploys aws services details are in respective feature branches.
Below we are able to check the resources that are being created as part of this module call:
- Lambda Function
To use this module, add the following call to your code:
module "lambda_function" {
source = "git::https://github.com/nitinda/terraform-module-aws-lambda-function.git?ref=master"
providers = {
aws = aws.services
}
filename = "lambda_function_payload.zip"
function_name = "lambda_function_name"
role = aws_iam_role.iam_for_lambda.arn
handler = "exports.test"
source_code_hash = filebase64sha256("lambda_function_payload.zip")
runtime = "nodejs12.x"
environment {
variables = {
foo = "bar"
}
}
tags = merge(
var.common_tags,
{
Environment = "prod"
Name = "lambda-function"
}
)
}
module "lambda_function" {
source = "git::https://github.com/nitinda/terraform-module-aws-lambda-function.git?ref=master"
providers = {
aws = aws.services
}
file_system_config = {
# EFS file system access point ARN
arn = "${aws_efs_access_point.access_point_for_lambda.arn}"
# Local mount path inside the lambda function. Must start with '/mnt/'.
local_mount_path = "/mnt/efs"
}
vpc_config = {
# Every subnet should be able to reach an EFS mount target in the same Availability Zone. Cross-AZ mounts are not permitted.
subnet_ids = ["${aws_subnet.subnet_for_lambda.id}"]
security_group_ids = ["${aws_security_group.sg_for_lambda.id}"]
}
tags = merge(
var.common_tags,
{
Environment = "prod"
Name = "lambda-function"
}
)
}
The following arguments are supported:
- filename - (Optional) The path to the function's deployment package within the local filesystem. If defined, The s3_-prefixed options cannot be used.
- s3_bucket - (Optional) The S3 bucket location containing the function's deployment package. Conflicts with filename. This bucket must reside in the same AWS region where you are creating the Lambda function.
- s3_key - (Optional) The S3 key of an object containing the function's deployment package. Conflicts with filename.
- s3_object_version - (Optional) The object version containing the function's deployment package. Conflicts with filename.
- function_name - (Required) A unique name for your Lambda Function.
- dead_letter_config - (Optional) Nested block to configure the function's dead letter queue. See details below.
- handler - (Required) The function entrypoint in your code.
- role - (Required) IAM role attached to the Lambda Function. This governs both who / what can invoke your Lambda Function, as well as what resources our Lambda Function has access to. See Lambda Permission Model for more details.
- description - (Optional) Description of what your Lambda Function does.
- layers - (Optional) List of Lambda Layer Version ARNs (maximum of 5) to attach to your Lambda Function. See Lambda Layers
- memory_size - (Optional) Amount of memory in MB your Lambda Function can use at runtime. Defaults to 128. See Limits.
- runtime - (Required) See Runtimes for valid values.
- timeout - (Optional) The amount of time your Lambda Function has to run in seconds. Defaults to 3. See Limits.
- reserved_concurrent_executions - (Optional) The amount of reserved concurrent executions for this lambda function. A value of 0 disables lambda from being triggered and -1 removes any concurrency limitations. Defaults to Unreserved Concurrency Limits -1. See Managing Concurrency.
- publish - (Optional) Whether to publish creation/change as new Lambda Function Version. Defaults to false.
- vpc_config - (Optional) Provide this to allow your function to access your VPC. Fields documented below. See Lambda in VPC.
- environment - (Optional) The Lambda environment's configuration settings.
- kms_key_arn - (Optional) Amazon Resource Name (ARN) of the AWS Key Management Service (KMS) key that is used to encrypt environment variables. If this configuration is not provided when environment variables are in use, AWS Lambda uses a default service key. If this configuration is provided when environment variables are not in use, the AWS Lambda API does not save this configuration and Terraform will show a perpetual difference of adding the key. To fix the perpetual difference, remove this configuration.
- source_code_hash - (Optional) Used to trigger updates. Must be set to a base64-encoded SHA256 hash of the package file specified with either filename or s3_key. The usual way to set this is filebase64sha256("file.zip") (Terraform 0.11.12 and later) or base64sha256(file("file.zip")) (Terraform 0.11.11 and earlier), where "file.zip" is the local filename of the lambda function source archive.
- tags - (Optional) A map of tags to assign to the resource
- file_system_config - (Optional) The connection settings for an EFS file system. Fields documented below. Before creating or updating Lambda functions with **file_system\config**, EFS mount targets much be in available lifecycle state. Use depends_on to explicitly declare this dependency. See Using Amazon EFS with Lambda._
In addition to all arguments above, the following attributes are exported:
- arn - The Amazon Resource Name (ARN) identifying your Lambda Function.
- invoke_arn - The ARN to be used for invoking Lambda Function from API Gateway - to be used in aws_api_gateway_integration's uri.
- version - Latest published version of your Lambda Function.
- last_modified - The date this resource was last modified.
- source_code_hash - Base64-encoded representation of raw SHA-256 sum of the zip file, provided either via filename or s3* parameters._
- source_code_size - The size in bytes of the function .zip file.
In order for the variables to be accessed on module level please use the syntax below:
module.<module_name>.<output_variable_name>
The output variable is able to be accessed through terraform state file using the syntax below:
data.terraform_remote_state.<module_name>.<output_variable_name>
Module maintained by Module maintained by the - Nitin Das