AWS Lambda with API Gateway Terraform Module

This project deploys an AWS Lambda function with an API Gateway that returns a simple "Hello, World!" message.

Project Structure

.
├── main.tf               # Main Terraform configuration
├── variables.tf          # Root module variables
├── outputs.tf            # Root module outputs
├── versions.tf           # Terraform version constraints
└── modules/
    ├── lambda/           # Lambda module
    │   ├── main.tf       # Lambda module configuration
    │   ├── variables.tf  # Lambda module variables
    │   ├── outputs.tf    # Lambda module outputs
    │   └── function/     # Lambda function code
    │       └── index.js  # Lambda handler
    └── api_gateway/      # API Gateway module
        ├── main.tf       # API Gateway module configuration
        ├── variables.tf  # API Gateway module variables
        └── outputs.tf    # API Gateway module outputs

Usage

module "lambda_api" {
  source = "path/to/module"

  region        = "us-east-1"
  aws_profile   = "my-profile"
  
  # Lambda settings
  function_name = "hello-world"
  runtime       = "nodejs20.x"
  handler       = "index.handler"
  
  # API Gateway settings
  api_name      = "hello-world-api"
  resource_path = "hello"
  http_method   = "GET"
  stage_name    = "dev"
}

Features

  • Lambda Function: Serverless function implementation
  • API Gateway: HTTP endpoint for invoking the Lambda function
  • Custom Domain: HTTPS endpoint with your own domain name
  • SSL/TLS: Secure communication using ACM certificates
  • Route53: Automatic DNS configuration

Prerequisites

  1. AWS Account and AWS CLI configured
  2. Terraform installed
  3. A registered domain in Route53
  4. SSL certificate in AWS Certificate Manager (ACM)

Configuration

  1. Copy terraform.tfvars.example to terraform.tfvars
  2. Update the variables with your values:
    • domain_name: Your custom domain (e.g., "api.example.com")
    • certificate_arn: ARN of your ACM certificate
    • route53_zone_id: Your Route53 hosted zone ID

Security Note

This repository template does not include sensitive information. You need to:

  1. Generate your own SSL certificate
  2. Configure your own domain in Route53
  3. Set up your own ACM certificate
  4. Never commit terraform.tfvars or any certificate files
  • HTTPS Support: Option to enable custom domain with HTTPS
  • Modular Structure: Easily extensible for additional components

Requirements

  • Terraform >= 1.5.0
  • AWS provider >= 5.0.0
  • AWS CLI configured with appropriate credentials

Inputs

Name Description Type Default Required
region AWS region string "us-east-1" no
aws_profile AWS profile for authentication string "default" no
function_name Lambda function name string "hello-world" no
runtime Lambda function runtime string "nodejs20.x" no
handler Lambda function handler string "index.handler" no
api_name Name of the API Gateway string "hello-world-api" no
resource_path API resource path string "hello" no
http_method HTTP method for the API Gateway string "GET" no
stage_name Name of the API Gateway stage string "dev" no
enable_custom_domain Enable custom domain for API Gateway bool false no
domain_name Custom domain name for API Gateway string "" no
certificate_arn ACM certificate ARN for custom domain string "" no

Outputs

Name Description
lambda_function_arn ARN of the Lambda function
lambda_function_name Name of the Lambda function
lambda_function_invoke_arn Invoke ARN of the Lambda function
api_endpoint Base endpoint URL of the API Gateway
api_invoke_url Full URL to invoke the API at the resource path
api_root_url Root URL of the API Gateway
custom_domain_endpoint Custom domain endpoint (if enabled)