Lambda
Overview
This service contains code to deploy a Lambda on AWS that can be used for either production or non-production workloads.
Lambda architecture
Features
- The lambda function
- Optionally, a schedule expression if you want to execute the lambda periodically
- Optionally, an alarm that can be triggered when the lambda fails
Learn
note
This repo is a part of the Gruntwork Service Catalog, a collection of reusable, battle-tested, production ready infrastructure code. If you’ve never used the Service Catalog before, make sure to read How to use the Gruntwork Service Catalog!
Under the hood, this is all implemented using Terraform modules from the Gruntwork terraform-aws-lambda repo. If you are a subscriber and don’t have access to this repo, email support@gruntwork.io.
Core concepts
To understand core concepts like what’s a Lambda, how to test it, and more, see the documentation in the terraform-aws-lambda repo.
Repo organization
- modules: The main implementation code for this repo, broken down into multiple standalone, orthogonal submodules.
- examples: This folder contains working examples of how to use the submodules.
- test: Automated tests for the modules and examples.
Deploy
Non-production deployment (quick start for learning)
If you just want to try this repo out for experimenting and learning, check out the following resources:
- examples/for-learning-and-testing folder: The
examples/for-learning-and-testing
folder contains standalone sample code optimized for learning, experimenting, and testing (but not direct production usage).
Production deployment
If you want to deploy this repo in production, check out the following resources:
examples/for-production folder: The
examples/for-production
folder contains sample code optimized for direct usage in production. This is code from the Gruntwork Reference Architecture, and it shows you how we build an end-to-end, integrated tech stack on top of the Gruntwork Service Catalog.How to configure a production-grade CI/CD workflow for application and infrastructure code: step-by-step guide on how to configure CI / CD for your apps and infrastructure.
Reference
- Inputs
- Outputs
Required
alarm_sns_topic_arns
list(string)A list of SNS topic ARNs to notify when the lambda alarms change to ALARM, OK, or INSUFFICIENT_DATA state
memory_size
numberThe maximum amount of memory, in MB, your Lambda function will be able to use at runtime. Can be set in 64MB increments from 128MB up to 1536MB. Note that the amount of CPU power given to a Lambda function is proportional to the amount of memory you request, so a Lambda function with 256MB of memory has twice as much CPU power as one with 128MB.
name
stringThe name of the Lambda function. Used to namespace all resources created by this module.
timeout
numberThe maximum amount of time, in seconds, your Lambda function will be allowed to run. Must be between 1 and 900 seconds.
Optional
additional_security_group_ids
list(string)A list of Security Group IDs that should be attached to the Lambda function when running in a VPC. Only used if run_in_vpc
is true.
[]
assume_role_policy
stringA custom assume role policy for the IAM role for this Lambda function. If not set, the default is a policy that allows the Lambda service to assume the IAM role, which is what most users will need. However, you can use this variable to override the policy for special cases, such as using a Lambda function to rotate AWS Secrets Manager secrets.
null
The ID (ARN, alias ARN, AWS ID) of a customer managed KMS Key to use for encrypting log data.
null
The number of days to retain log events in the log group. Refer to https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group#retention_in_days for all the valid values. When null, the log events are retained forever.
null
The ARN of the destination to deliver matching log events to. Kinesis stream or Lambda function ARN. Only applicable if should_create_cloudwatch_log_group
is true.
null
The method used to distribute log data to the destination. Only applicable when cloudwatch_log_group_subscription_destination_arn
is a kinesis stream. Valid values are Random
and ByLogStream
.
null
A valid CloudWatch Logs filter pattern for subscribing to a filtered stream of log events.
""
ARN of an IAM role that grants Amazon CloudWatch Logs permissions to deliver ingested log events to the destination. Only applicable when cloudwatch_log_group_subscription_destination_arn
is a kinesis stream.
null
cloudwatch_log_group_tags
map(string)Tags to apply on the CloudWatch Log Group, encoded as a map where the keys are tag keys and values are tag values.
null
command
list(string)The CMD for the docker image. Only used if you specify a Docker image via image_uri.
[]
comparison_operator
stringThe arithmetic operation to use when comparing the specified Statistic and Threshold. The specified Statistic value is used as the first operand. Either of the following is supported: GreaterThanOrEqualToThreshold
, GreaterThanThreshold
, LessThanThreshold
, LessThanOrEqualToThreshold
. Additionally, the values LessThanLowerOrGreaterThanUpperThreshold
, LessThanLowerThreshold
, and GreaterThanUpperThreshold
are used only for alarms based on anomaly detection models.
"GreaterThanThreshold"
create_resources
boolSet to false to have this module skip creating resources. This weird parameter exists solely because Terraform does not support conditional modules. Therefore, this is a hack to allow you to conditionally decide if this module should create anything or not.
true
datapoints_to_alarm
numberThe number of datapoints that must be breaching to trigger the alarm.
1
The ARN of an SNS topic or an SQS queue to notify when invocation of a Lambda function fails. If this option is used, you must grant this function's IAM role (the ID is outputted as iam_role_id) access to write to the target object, which means allowing either the sns:Publish or sqs:SendMessage action on this ARN, depending on which service is targeted.
null
description
stringA description of what the Lambda function does.
null
Set to true to enable versioning for this Lambda function. This allows you to use aliases to refer to execute different versions of the function in different environments. Note that an alternative way to run Lambda functions in multiple environments is to version your Terraform code.
false
entry_point
list(string)The ENTRYPOINT for the docker image. Only used if you specify a Docker image via image_uri.
[]
environment_variables
map(string)A map of environment variables to pass to the Lambda function. AWS will automatically encrypt these with KMS and decrypt them when running the function.
{
EnvVarPlaceHolder = "Placeholder"
}
evaluation_periods
numberThe number of periods over which data is compared to the specified threshold.
1
The ARN of an EFS access point to use to access the file system. Only used if mount_to_file_system
is true.
null
file_system_mount_path
stringThe mount path where the lambda can access the file system. This path must begin with /mnt/. Only used if mount_to_file_system
is true.
null
handler
stringThe function entrypoint in your code. This is typically the name of a function or method in your code that AWS will execute when this Lambda function is triggered.
null
iam_policy
map(object(…))An object defining the policy to attach to iam_role_name
if the IAM role is going to be created. Accepts a map of objects, where the map keys are sids for IAM policy statements, and the object fields are the resources, actions, and the effect ('Allow' or 'Deny') of the statement. Ignored if iam_role_arn
is provided. Leave as null if you do not wish to use IAM role with Service Accounts.
map(object({
resources = list(string)
actions = list(string)
effect = string
}))
null
image_uri
stringThe ECR image URI containing the function's deployment package. Example: 01234501234501.dkr.ecr.us-east-1.amazonaws.com/image_name:image_tag
null
kms_key_arn
stringA custom KMS key to use to encrypt and decrypt Lambda function environment variables. Leave it blank to use the default KMS key provided in your AWS account.
null
The ARN of the policy that is used to set the permissions boundary for the IAM role for the lambda
null
layers
list(string)The list of Lambda Layer Version ARNs to attach to your Lambda Function. You can have a maximum of 5 Layers attached to each function.
[]
metric_name
stringThe name for the alarm's associated metric.
"Errors"
Set to true to mount your Lambda function on an EFS. Note that the lambda must also be deployed inside a VPC (run_in_vpc must be set to true) for this config to have any effect.
false
namespace
stringThe namespace to use for all resources created by this module. If not set, lambda_function_name
, with '-scheduled' as a suffix, is used.
null
period
numberThe period in seconds over which the specified statistic
is applied.
60
The amount of reserved concurrent executions for this lambda function or -1 if unreserved.
null
run_in_vpc
boolSet to true to give your Lambda function access to resources within a VPC.
false
runtime
stringThe runtime environment for the Lambda function (e.g. nodejs, python2.7, java8). See https://docs.aws.amazon.com/lambda/latest/dg/API_CreateFunction.html#SSS-CreateFunction-request-Runtime for all possible values.
null
s3_bucket
stringAn S3 bucket location containing the function's deployment package. Exactly one of source_path
or the s3_xxx
variables must be specified.
null
s3_key
stringThe path within s3_bucket
where the deployment package is located. Exactly one of source_path
or the s3_xxx
variables must be specified.
null
s3_object_version
stringThe version of the path in s3_key
to use as the deployment package. Exactly one of source_path
or the s3_xxx
variables must be specified.
null
schedule_expression
stringAn expression that defines the schedule for this lambda job. For example, cron(0 20 * ? ) or rate(5 minutes). For more information visit https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents-expressions.html
null
If set to false, this function will no longer set the source_code_hash parameter, so this module will no longer detect and upload changes to the deployment package. This is primarily useful if you update the Lambda function from outside of this module (e.g., you have scripts that do it separately) and want to avoid a plan diff. Used only if source_path
is non-empty.
true
When true, precreate the CloudWatch Log Group to use for log aggregation from the lambda function execution. This is useful if you wish to customize the CloudWatch Log Group with various settings such as retention periods and KMS encryption. When false, AWS Lambda will automatically create a basic log group to use.
true
If true, create an egress rule allowing all outbound traffic from Lambda function to the entire Internet (e.g. 0.0.0.0/0).
false
skip_zip
boolSet to true to skip zip archive creation and assume that source_path
points to a pregenerated zip archive.
false
source_path
stringThe path to the directory that contains your Lambda function source code. This code will be zipped up and uploaded to Lambda as your deployment package. If skip_zip
is set to true, then this is assumed to be the path to an already-zipped file, and it will be uploaded directly to Lambda as a deployment package. Exactly one of source_path
or the s3_xxx
variables must be specified.
null
statistic
stringThe statistic to apply to the alarm's associated metric.
"Sum"
subnet_ids
list(string)A list of subnet IDs the Lambda function should be able to access within your VPC. Only used if run_in_vpc
is true.
[]
tags
map(string)A map of tags to apply to the Lambda function.
{}
threshold
numberThe value against which the specified statistic is compared. This parameter is required for alarms based on static thresholds, but should not be used for alarms based on anomaly detection models.
0
When true, all IAM policies will be managed as dedicated policies rather than inline policies attached to the IAM roles. Dedicated managed policies are friendlier to automated policy checkers, which may scan a single resource for findings. As such, it is important to avoid inline policies when targeting compliance with various security standards.
true
vpc_id
stringThe ID of the VPC the Lambda function should be able to access. Only used if run_in_vpc
is true.
null
working_directory
stringThe working directory for the docker image. Only used if you specify a Docker image via image_uri.
null
zip_output_path
stringThe path to store the output zip file of your source code. If empty, defaults to module path. This should be the full path to the zip file, not a directory.
null
The list of actions to execute when this alarm transitions into an ALARM state from any other state
ARN of the Cloudwatch alarm
Name of the Cloudwatch alarm
Name of the (optionally) created CloudWatch log group for the lambda function.
Cloudwatch Event Rule Arn
Cloudwatch Event Rule schedule expression
Amazon Resource Name (ARN) identifying the Lambda Function
Unique name for Lambda Function
Amazon Resource Name (ARN) of the AWS IAM Role created for the Lambda Function
Name of the AWS IAM Role created for the Lambda Function
The list of actions to execute when this alarm transitions into an INSUFFICIENT_DATA state from any other state
Amazon Resource Name (ARN) to be used for invoking the Lambda Function
The list of actions to execute when this alarm transitions into an OK state from any other state
Amazon Resource Name (ARN) identifying your Lambda Function version
Security Group ID of the Security Group created for the Lambda Function
Latest published version of your Lambda Function