Terraformterraformawslambdaapi-gatewayproduction-ready
Lambda with API Gateway
Serverless API using Lambda and API Gateway v2 (HTTP API) with custom domain, stages, and IAM permissions.
title: "Lambda with API Gateway" description: "Serverless API using Lambda and API Gateway v2 (HTTP API) with custom domain, stages, and IAM permissions." category: "Terraform" language: "hcl" tags: ["terraform", "aws", "lambda", "api-gateway", "production-ready"] createdAt: "2025-03-10" updatedAt: "2025-03-30"
Overview
A serverless API setup using AWS Lambda with HTTP API Gateway (v2). Includes CORS configuration, structured access logging, X-Ray tracing, and automatic deployment. HTTP API is faster and cheaper than REST API for most use cases.
Code
module "api" {
source = "./modules/lambda-api"
function_name = "user-api"
source_dir = "./dist"
environment_variables = { DB_URL = var.database_url }
}Notes
- HTTP API (v2) is 70% cheaper and lower latency than REST API (v1) for most workloads
payload_format_version = "2.0"passes a simplified event format to Lambdasource_code_hashtriggers redeployment only when code actually changes- X-Ray tracing provides distributed tracing across API Gateway and Lambda
- The
$defaultroute catches all paths; add specific routes for path-based routing