Terraform AWS Lambda API Gateway
Uploads lambda zip bundle to AWS S3 during terraform apply
Creates VPC with private and public subnets
Deploys lambda function into private subnet (behind NAT Gateway)
module "lambda_api_gateway" {
source = " [email protected] :techjacker/terraform-aws-lambda-api-gateway"
# tags
project = " todo-mvc"
service = " acme-corp"
owner = " Roadrunner"
costcenter = " acme-abc"
# vpc
vpc_cidr = " 10.0.0.0/16"
public_subnets_cidr = [" 10.0.1.0/24" , " 10.0.2.0/24" ]
private_subnets_cidr = [" 10.0.3.0/24" , " 10.0.4.0/24" ]
nat_cidr = [" 10.0.5.0/24" , " 10.0.6.0/24" ]
igw_cidr = " 10.0.8.0/24"
azs = [" eu-west-1a" , " eu-west-1b" ]
# lambda
lambda_zip_path = " dist/todo-mvc.zip"
lambda_handler = " entry.run_app"
lambda_runtime = " python3.6"
lambda_function_name = " HttpWebserver"
# API gateway
region = " eu-west-1"
account_id = " 123456789"
}
Run build process to create lambda zip bundle locally
Update terraform variable lambda_zip_path
with path to zip bundle on local machine
Provide values for other required terraform variables
Create/Select terraform workspace on 1st/subsequent deployments
Deploy with $ terraform apply
Example Deployment Script
#! /usr/bin/env bash
if [[ ! -d .terraform ]]; then
terraform init
fi
if ! terraform workspace list 2>&1 | grep -qi " $ENVIRONMENT " ; then
terraform workspace new " $ENVIRONMENT "
fi
terraform workspace select "$ENVIRONMENT"
terraform get
terraform apply \
-var " lambda_zip_path=$LAMBDA_ZIP_PATH " \
-var " region=$REGION " \
-var " account_id=$ACCOUNT_ID "