A Terraform module for deploying an automated Lambda function warming solution. This module helps prevent cold starts in your Lambda functions by periodically invoking them based on tags.
This module creates a Lambda function that automatically warms up other Lambda functions based on specified tags. It uses EventBridge Scheduler to trigger the warming process at regular intervals.
- Tag-based function selection for warming
- Configurable warming schedule
- Customizable tag key/value pairs for targeting functions
- Customizable Lambda Warmer function invocation type ( Event / RequestResponse )
module "lambda_warmer" {
source = "../modules/lambda_warmer"
aws_region = "ap-northeast-1"
# Optional: Custom configuration
environment = "production"
prewarm_tag_key = "Project"
prewarm_tag_value = "MyProject"
lambda_schedule_expression = "rate(5 minutes)"
timeout = 60
memory_size = 256
scheduler_max_retry_attempts = 0
invocation_type = "Event"
}
To mark a Lambda function for warming, add the appropriate tags:
resource "aws_lambda_function" "example" {
# ... other configuration ...
tags = {
Prewarm = "true" # Default tags
# Or use custom tags as configured in the module
Project = "MyProject"
}
}
When a Lambda function is invoked for prewarming, it's important to differentiate prewarming requests from normal business logic. Below is an python example of how to handle prewarming requests
def lambda_handler(event, context):
# Identify if the incoming event is a prewarm request
if event.get("action") == "PREWARM":
logger.info("Received a prewarm request. Skipping business logic.")
return {
"statusCode": 200,
"body": "Successfully warmed up"
}
Name | Version |
---|---|
Terraform | >= 1.8.0 |
AWS Provider | >= 5.54 |
Name | Description | Type |
---|---|---|
aws_region | AWS region where the Lambda warmer will be deployed | string |
Name | Description | Type | Default |
---|---|---|---|
environment | Current environment, e.g. dev, stage, prod | string | "default" |
lambda_function_name | Name of the Lambda function used for prewarming | string | "lambda-warmer" |
lambda_schedule_expression | EventBridge schedule expression for triggering the Lambda warmer | string | "rate(5 minutes)" |
scheduler_max_retry_attempts | Maximum retry attempts for the EventBridge scheduler target | number | 0 |
timeout | Timeout for the Lambda function in seconds | number | 10 |
memory_size | Memory size for the Lambda function | number | 128 |
prewarm_tag_key | The tag key to identify functions that need prewarming | string | "Prewarm" |
prewarm_tag_value | The expected value of the tag for functions that need prewarming | string | "true" |
invocation_type | The invocation type of the Lambda Warmer function (Event / RequestResponse) | string | "Event" |
Name | Description |
---|---|
scheduler_group_arn | ARN of the EventBridge Scheduler Group |
scheduler_arn | ARN of the EventBridge Scheduler |
lambda_function_name | Name of the Lambda warmer function |
lambda_function_arn | ARN of the Lambda warmer function |
lambda_role_name | Name of the Lambda IAM Role |
lambda_role_arn | ARN of the Lambda IAM Role |
.
├── README.md
├── main.tf # Main Terraform configuration
├── variables.tf # Input variables
├── versions.tf # Terraform version constraints
├── outputs.tf # Output definitions
└── lambda_warmer/ # Lambda function code
└── lambda_function.py # Python implementation
The module creates two separate IAM roles:
-
Lambda Role:
- CloudWatch Logs access
- Lambda function invocation
-
EventBridge Scheduler Role:
- Permission to invoke the warmer Lambda function
- Every 5 minutes:
rate(5 minutes)
- Every hour:
rate(1 hour)
The Lambda warmer function logs its activities to CloudWatch Logs. You can monitor:
- Functions being warmed
- Warming success/failure
- Number of functions processed
If you encounter any issues, feel free to reach out at [email protected] or submit an issue.
This project is licensed under the MIT License. See the LICENSE file for details.
Copyright © 2024 AWS Educate Cloud Ambassador Taiwan