Minimal Lambda function with zip packaging and container image support for provided.al2023 runtime
Terraform module that creates a Lambda function with zip packaging or container images, IAM execution role, and CloudWatch logs. Perfect for shell scripts and custom runtimes.
- Zip packaging from source directory
- Container image deployment from ECR
- IAM execution role with basic Lambda permissions
- CloudWatch log group with configurable retention
- provided.al2023 runtime optimized for shell scripts
- CloudPosse labeling for consistent naming
module "lambda" {
source = "ql4b/lambda-function/aws"
version = "~> 1.0"
source_dir = "../app/src"
template_dir = "../app/src"
create_templates = true
context = module.label.context
attributes = ["lambda"]
}This will automatically create bootstrap, handler.sh, and Makefile in your ../app/src directory.
module "lambda_function" {
source = "ql4b/lambda-function/aws"
version = "~> 1.0"
package_type = "Image"
image_uri = "${aws_ecr_repository.example.repository_url}:latest"
image_config = {
entry_point = ["/lambda-entrypoint.sh"]
command = ["app.handler"]
}
memory_size = 512
timeout = 30
environment_variables = {
THUMBNAILS_BUCKET = module.thumbnails_bucket.bucket_id
}
context = module.label.context
attributes = ["lambda"]
}Note: This module integrates seamlessly with any existing Terraform setup. The examples above show integration with CloudPosse labeling, but you can use it standalone or with any naming convention.
module "lambda_function" {
source = "ql4b/lambda-function/aws"
version = "~> 1.0"
package_type = "Zip"
source_dir = "./src"
handler = "bootstrap"
runtime = "provided.al2023"
architecture = "arm64"
memory_size = 256
timeout = 30
environment_variables = {
HANDLER = "/var/task/handler.sh"
PATH = "/opt/bin:/usr/local/bin:/usr/bin:/bin"
}
log_retention_days = 30
context = {
namespace = "myorg"
name = "myfunction"
stage = "prod"
}
}| Name | Description | Type | Default | Required |
|---|---|---|---|---|
| package_type | Lambda deployment package type | string |
"Zip" |
no |
| image_uri | ECR image URI for container image deployment | string |
null |
no |
| image_config | Container image configuration | object |
null |
no |
| source_dir | Path to source directory (Zip only) | string |
n/a | yes |
| handler | Lambda function handler (Zip only) | string |
"bootstrap" |
no |
| runtime | Lambda runtime (Zip only) | string |
"provided.al2023" |
no |
| architecture | Lambda function architecture | string |
"arm64" |
no |
| memory_size | Memory in MB | number |
128 |
no |
| timeout | Timeout in seconds | number |
3 |
no |
| environment_variables | Environment variables | map(string) |
{} |
no |
The image_config object supports:
image_config = {
entry_point = ["/lambda-entrypoint.sh"] # Optional
command = ["app.handler"] # Optional
working_directory = "/var/task" # Optional
}Set create_templates = true to automatically generate starter files:
bootstrap- Lambda runtime bootstrap (executable)handler.sh- Example function handlerMakefile- Deployment and testing commands
src/
├── bootstrap # Lambda runtime bootstrap (executable)
├── handler.sh # Your function code
└── Makefile # Deployment commands
#!/bin/bash
# bootstrap
set -euo pipefail
while true; do
HEADERS="$(mktemp)"
EVENT_DATA=$(curl -sS -LD "$HEADERS" -X GET "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next")
REQUEST_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2)
# Execute your handler
RESPONSE=$(bash "${HANDLER:-/var/task/handler.sh}" "$EVENT_DATA")
# Send response
curl -X POST "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$REQUEST_ID/response" -d "$RESPONSE"
done#!/bin/bash
# handler.sh
api_handler() {
local event="$1"
local name=$(echo "$event" | jq -r '.name // "World"')
echo '{
"statusCode": 200,
"body": "Hello, '"$name"'!"
}'
}
# Call handler with event data
api_handler "$1"terraform init
terraform apply# If using the module directly
export FUNCTION_NAME=$(terraform output -raw function_name)
# If using as a nested module (like in cloudless-minimal)
export FUNCTION_NAME=$(tf output --json lambda | jq -r .function_name)When create_templates = true, a Makefile is generated with deployment workflows:
# Set function name (adjust based on your output structure)
export FUNCTION_NAME=$(tf output --json lambda | jq -r .function_name)
# Navigate to your source directory
cd app/src
# Deploy function code
make deploy
# Test function
make invoke
# View logs
make logs
# Clean artifacts
make clean
# See all targets
make help# Package and deploy manually
FUNCTION_NAME=$(terraform output -raw function_name)
cd src && zip -r ../function.zip .
aws lambda update-function-code \
--function-name $FUNCTION_NAME \
--zip-file fileb://function.zip
# Test function
aws lambda invoke --function-name $FUNCTION_NAME /tmp/response.json
cat /tmp/response.json- Terraform >= 1.0
- AWS provider >= 5.0
- Archive provider >= 2.0 (Zip packaging only)
function_name- Lambda function namefunction_arn- Lambda function ARNexecution_role_arn- IAM execution role ARNexecution_role_name- IAM execution role namelog_group_name- CloudWatch log group name
# Add layers for additional tools
resource "aws_lambda_layer_version" "jq" {
layer_name = "jq"
filename = "jq-layer.zip"
compatible_runtimes = ["provided.al2023"]
compatible_architectures = ["arm64"]
}
module "lambda_function" {
source = "ql4b/lambda-function/aws"
version = "~> 1.0"
source_dir = "./src"
environment_variables = {
PATH = "/opt/bin:/usr/local/bin:/usr/bin:/bin"
}
context = {
namespace = "myorg"
name = "myfunction"
}
}
# Attach layer to function
resource "aws_lambda_function" "with_layers" {
# ... other configuration
layers = [aws_lambda_layer_version.jq.arn]
}| Name | Version |
|---|---|
| terraform | >= 1.0 |
| archive | >= 2.0 |
| aws | >= 5.0 |
| Name | Version |
|---|---|
| archive | >= 2.0 |
| aws | >= 5.0 |
| null | n/a |
| Name | Source | Version |
|---|---|---|
| this | cloudposse/label/null | 0.25.0 |
| Name | Type |
|---|---|
| aws_cloudwatch_log_group.lambda_logs | resource |
| aws_iam_role.lambda_execution | resource |
| aws_iam_role_policy_attachment.lambda_basic_execution | resource |
| aws_lambda_function.this | resource |
| aws_ssm_parameter.function_arn | resource |
| aws_ssm_parameter.function_name | resource |
| aws_ssm_parameter.invoke_arn | resource |
| null_resource.bootstrap_template | resource |
| null_resource.handler_template | resource |
| null_resource.makefile_template | resource |
| archive_file.lambda_zip | data source |
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
| additional_tag_map | Additional key-value pairs to add to each map in tags_as_list_of_maps. Not added to tags or id.This is for some rare cases where resources want additional configuration of tags and therefore take a list of maps with tag key, value, and additional configuration. |
map(string) |
{} |
no |
| architecture | Lambda function architecture | string |
"arm64" |
no |
| attributes | ID element. Additional attributes (e.g. workers or cluster) to add to id,in the order they appear in the list. New attributes are appended to the end of the list. The elements of the list are joined by the delimiterand treated as a single ID element. |
list(string) |
[] |
no |
| context | Single object for setting entire context at once. See description of individual variables for details. Leave string and numeric variables as null to use default value.Individual variable settings (non-null) override settings in context object, except for attributes, tags, and additional_tag_map, which are merged. |
any |
{ |
no |
| create_templates | Create template files (bootstrap, handler, Makefile) in template_dir | bool |
false |
no |
| delimiter | Delimiter to be used between ID elements. Defaults to - (hyphen). Set to "" to use no delimiter at all. |
string |
null |
no |
| descriptor_formats | Describe additional descriptors to be output in the descriptors output map.Map of maps. Keys are names of descriptors. Values are maps of the form {<br/> format = string<br/> labels = list(string)<br/>}(Type is any so the map values can later be enhanced to provide additional options.)format is a Terraform format string to be passed to the format() function.labels is a list of labels, in order, to pass to format() function.Label values will be normalized before being passed to format() so they will beidentical to how they appear in id.Default is {} (descriptors output will be empty). |
any |
{} |
no |
| enabled | Set to false to prevent the module from creating any resources | bool |
null |
no |
| environment | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | string |
null |
no |
| environment_variables | Map of environment variables for the Lambda function | map(string) |
{} |
no |
| filename | Path to pre-built zip file (alternative to source_dir) | string |
null |
no |
| handler | Lambda function handler | string |
"bootstrap" |
no |
| id_length_limit | Limit id to this many characters (minimum 6).Set to 0 for unlimited length.Set to null for keep the existing setting, which defaults to 0.Does not affect id_full. |
number |
null |
no |
| image_config | Container image configuration | object({ |
null |
no |
| image_uri | ECR image URI for container image deployment | string |
null |
no |
| label_key_case | Controls the letter case of the tags keys (label names) for tags generated by this module.Does not affect keys of tags passed in via the tags input.Possible values: lower, title, upper.Default value: title. |
string |
null |
no |
| label_order | The order in which the labels (ID elements) appear in the id.Defaults to ["namespace", "environment", "stage", "name", "attributes"]. You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. |
list(string) |
null |
no |
| label_value_case | Controls the letter case of ID elements (labels) as included in id,set as tag values, and output by this module individually. Does not affect values of tags passed in via the tags input.Possible values: lower, title, upper and none (no transformation).Set this to title and set delimiter to "" to yield Pascal Case IDs.Default value: lower. |
string |
null |
no |
| labels_as_tags | Set of labels (ID elements) to include as tags in the tags output.Default is to include all labels. Tags with empty values will not be included in the tags output.Set to [] to suppress all generated tags.Notes: The value of the name tag, if included, will be the id, not the name.Unlike other null-label inputs, the initial setting of labels_as_tags cannot bechanged in later chained modules. Attempts to change it will be silently ignored. |
set(string) |
[ |
no |
| layers | List of Lambda layer ARNs to attach to the function | list(string) |
[] |
no |
| log_retention_days | CloudWatch log retention in days | number |
14 |
no |
| memory_size | Amount of memory in MB your Lambda Function can use at runtime | number |
128 |
no |
| name | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'. This is the only ID element not also included as a tag.The "name" tag is set to the full id string. There is no tag with the value of the name input. |
string |
null |
no |
| namespace | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | string |
null |
no |
| package_type | Lambda deployment package type | string |
"Zip" |
no |
| regex_replace_chars | Terraform regular expression (regex) string. Characters matching the regex will be removed from the ID elements. If not set, "/[^a-zA-Z0-9-]/" is used to remove all characters other than hyphens, letters and digits. |
string |
null |
no |
| runtime | Lambda runtime | string |
"provided.al2023" |
no |
| source_dir | Path to the source directory containing Lambda function code | string |
null |
no |
| stage | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | string |
null |
no |
| tags | Additional tags (e.g. {'BusinessUnit': 'XYZ'}).Neither the tag keys nor the tag values will be modified by this module. |
map(string) |
{} |
no |
| template_dir | Directory to create template files in | string |
"./src" |
no |
| tenant | ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for | string |
null |
no |
| timeout | Amount of time your Lambda Function has to run in seconds | number |
3 |
no |
| Name | Description |
|---|---|
| execution_role_arn | Lambda execution role ARN |
| execution_role_name | Lambda execution role name |
| function_arn | Lambda function ARN |
| function_invoke_arn | Lambda function invoke ARN |
| function_last_modified | Lambda function last modified date |
| function_name | Lambda function name |
| function_qualified_arn | Lambda function qualified ARN |
| function_source_code_hash | Lambda function source code hash |
| function_source_code_size | Lambda function source code size |
| function_version | Lambda function version |
| log_group_arn | CloudWatch log group ARN |
| log_group_name | CloudWatch log group name |
| package_path | Path to the Lambda deployment package |
| package_size | Size of the Lambda deployment package |
| ssm_parameters | SSM parameter names for Serverless integration |
| template_files | Paths to created template files |
Part of the cloudless ecosystem.