TerraformAWS
AWS Lambda + API Gateway
HTTP API (v2) backed by Lambda, with IAM execution role, CloudWatch log group, and a deployment stage.
lambdaapi-gatewayserverlesshttp
Prerequisites
- •
A Lambda deployment package ZIP at the path specified by `lambda_zip_path` - •
Terraform >= 1.5.0
Template Code
# ─────────────────────────────────────────────────────────────────────────────
# AWS Lambda + HTTP API Gateway v2
# ─────────────────────────────────────────────────────────────────────────────
variable "environment" { default = "production" }
variable "function_name" { default = "my-api-handler" }
variable "lambda_zip_path" { default = "./dist/function.zip" }
variable "lambda_runtime" { default = "nodejs20.x" }
variable "lambda_handler" { default = "index.handler" }
# ── CloudWatch Log Group ──────────────────────────────────────────────────────
resource "aws_cloudwatch_log_group" "lambda" {
name = "/aws/lambda/${var.function_name}"
retention_in_days = 14
tags = { Environment = var.environment }
}
# ── IAM Role ──────────────────────────────────────────────────────────────────
resource "aws_iam_role" "lambda" {
name = "${var.function_name}-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = { Service = "lambda.amazonaws.com" }
Action = "sts:AssumeRole"
}]
})
}
resource "aws_iam_role_policy_attachment" "lambda_basic" {
role = aws_iam_role.lambda.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
# ── Lambda Function ───────────────────────────────────────────────────────────
resource "aws_lambda_function" "main" {
filename = var.lambda_zip_path
function_name = var.function_name
role = aws_iam_role.lambda.arn
handler = var.lambda_handler
runtime = var.lambda_runtime
source_code_hash = filebase64sha256(var.lambda_zip_path)
memory_size = 256
timeout = 30
environment {
variables = {
ENVIRONMENT = var.environment
LOG_LEVEL = "info"
}
}
depends_on = [
aws_iam_role_policy_attachment.lambda_basic,
aws_cloudwatch_log_group.lambda,
]
tags = { Environment = var.environment }
}
# ── HTTP API Gateway v2 ───────────────────────────────────────────────────────
resource "aws_apigatewayv2_api" "main" {
name = "${var.function_name}-api"
protocol_type = "HTTP"
cors_configuration {
allow_headers = ["Content-Type", "Authorization"]
allow_methods = ["GET", "POST", "PUT", "DELETE", "OPTIONS"]
allow_origins = ["*"]
max_age = 3600
}
}
resource "aws_apigatewayv2_integration" "lambda" {
api_id = aws_apigatewayv2_api.main.id
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.main.invoke_arn
payload_format_version = "2.0"
}
resource "aws_apigatewayv2_route" "default" {
api_id = aws_apigatewayv2_api.main.id
route_key = "$default"
target = "integrations/${aws_apigatewayv2_integration.lambda.id}"
}
resource "aws_apigatewayv2_stage" "default" {
api_id = aws_apigatewayv2_api.main.id
name = "$default"
auto_deploy = true
}
resource "aws_lambda_permission" "apigw" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.main.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_apigatewayv2_api.main.execution_arn}/*/*"
}
output "api_url" { value = aws_apigatewayv2_api.main.api_endpoint }
Usage
# Build your Lambda ZIP first, then: terraform init terraform apply -var="lambda_zip_path=./dist/function.zip"