Examples
These examples demonstrate practical, real-world usage patterns for the lambda-function module. Each example is self-contained and ready to run—simply copy the configuration, customize the values for your environment, and apply.
Getting Started
To run any example, follow these steps:
- Authenticate with the registry:
terraform login registry.patterneddesigns.ca - Initialize the working directory:
terraform init - Review the execution plan:
terraform plan - Apply the configuration:
terraform apply
Usage Examples
Minimal configuration for a Python Lambda function
module "basic_function" {
source = "registry.patterneddesigns.ca/patterneddesigns/lambda-function/aws"
version = "3.1.0"
function_name = "hello-world"
runtime = "python3.12"
handler = "main.handler"
source_path = "./src"
}
Lambda function optimized for API Gateway integration
module "api_handler" {
source = "registry.patterneddesigns.ca/patterneddesigns/lambda-function/aws"
version = "3.1.0"
function_name = "api-handler"
runtime = "nodejs20.x"
handler = "index.handler"
source_path = "./dist"
memory_size = 256
timeout = 29 # API Gateway limit
environment_variables = {
NODE_ENV = "production"
API_VERSION = "v1"
}
}
# Grant API Gateway permission to invoke
resource "aws_lambda_permission" "api_gateway" {
statement_id = "AllowAPIGateway"
action = "lambda:InvokeFunction"
function_name = module.api_handler.function_name
principal = "apigateway.amazonaws.com"
}
Function with access to private resources (RDS, ElastiCache)
data "aws_vpc" "main" {
tags = { Name = "main" }
}
data "aws_subnets" "private" {
filter {
name = "vpc-id"
values = [data.aws_vpc.main.id]
}
tags = { Tier = "private" }
}
resource "aws_security_group" "lambda" {
name_prefix = "lambda-"
vpc_id = data.aws_vpc.main.id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
module "db_processor" {
source = "registry.patterneddesigns.ca/patterneddesigns/lambda-function/aws"
version = "3.1.0"
function_name = "db-processor"
runtime = "python3.12"
handler = "main.handler"
source_path = "./src"
memory_size = 512
timeout = 300
vpc_config = {
subnet_ids = data.aws_subnets.private.ids
security_group_ids = [aws_security_group.lambda.id]
}
environment_variables = {
DB_HOST = "db.internal.example.com"
DB_NAME = "myapp"
}
}
High-throughput event processing with concurrency controls
module "event_processor" {
source = "registry.patterneddesigns.ca/patterneddesigns/lambda-function/aws"
version = "3.1.0"
function_name = "event-processor"
runtime = "python3.12"
handler = "main.handler"
source_path = "./src"
memory_size = 1024
timeout = 60
# Limit concurrent executions to protect downstream services
reserved_concurrent_executions = 50
environment_variables = {
BATCH_SIZE = "100"
MAX_RETRIES = "3"
}
}
# SQS trigger
resource "aws_lambda_event_source_mapping" "sqs" {
event_source_arn = aws_sqs_queue.events.arn
function_name = module.event_processor.function_arn
batch_size = 10
}
CloudWatch Events scheduled execution
module "scheduled_task" {
source = "registry.patterneddesigns.ca/patterneddesigns/lambda-function/aws"
version = "3.1.0"
function_name = "daily-report"
runtime = "python3.12"
handler = "main.handler"
source_path = "./src"
timeout = 900 # 15 minutes for long-running reports
environment_variables = {
REPORT_BUCKET = aws_s3_bucket.reports.id
}
}
resource "aws_cloudwatch_event_rule" "daily" {
name = "daily-report"
schedule_expression = "cron(0 6 * * ? *)" # 6 AM UTC daily
}
resource "aws_cloudwatch_event_target" "lambda" {
rule = aws_cloudwatch_event_rule.daily.name
target_id = "DailyReport"
arn = module.scheduled_task.function_arn
}
resource "aws_lambda_permission" "events" {
statement_id = "AllowCloudWatchEvents"
action = "lambda:InvokeFunction"
function_name = module.scheduled_task.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.daily.arn
}
Using the same module across environments
locals {
environments = {
dev = {
memory_size = 128
timeout = 30
log_level = "DEBUG"
}
prod = {
memory_size = 512
timeout = 60
log_level = "INFO"
}
}
}
module "api_function" {
for_each = local.environments
source = "registry.patterneddesigns.ca/patterneddesigns/lambda-function/aws"
version = "3.1.0"
function_name = "api-handler-${each.key}"
runtime = "python3.12"
handler = "main.handler"
source_path = "./src"
memory_size = each.value.memory_size
timeout = each.value.timeout
environment_variables = {
ENVIRONMENT = each.key
LOG_LEVEL = each.value.log_level
}
}