Terraform module which deploys a serverless HTTP endpoint backed by AWS API Gateway, Lambda & SQS
Attention
Starting from version 1.6.0, this module targets Terraform 0.12+. If you are using Terraform <=v0.11 you must use up to version 1.5.0.
This module is created with a single stage that is given as parameter.
The default path that is created is /api/messages
. This can be expanded upon as the API GW ID, resources and methods are exposed.
If you do not wish to have the default values, you can specify api_gw_disable_resource_creation = true
and you can create the paths desired.
Allows specification of Endpoint Configuration Type via variable api_gw_endpoint_configuration_type
with EDGE
, REGIONAL
or PRIVATE
. Defaults to EDGE
Note
This results in having to create the final aws_api_gateway_deployment
as well.
This module is created with full customization by user.
- Can use either local filename path
lambda_file_name
or remote S3 bucket configuration. - Supports Lambda Layers
- Supports VPC
Must use either the local filename or S3 option as they are mutually exclusive.
Exports S3 bucket to allow usage by multiple Lambda's but given lambda_code_s3_bucket_use_existing=true
it will use existing S3 bucket provided in lambda_code_s3_bucket_existing
.
- This module by default, if created allows accompanying Lambda access to SQS if SQS entry is provided as parameters.
- The Lambda gets by default in addition to user provided environment variables the
SQS_QUEUES_URLS
which is a list with the URL for each of the created queues for convenience.
This module is optional. Lambda is created with W permission for SQS to allow Lambda to add/read/delete messages from queue.
- This module by default, if created has no permissions added.
module "api-gateway-lambda-sqs" {
source = "crisboarna/api-gateway-lambda-sqs/aws"
version = "1.6.0"
# insert the required variables here
}
- Run build process to generate Lambda ZIP file locally to match
lambda_zip_path
variable path - Provide all needed variables from
variables.tf
file or copy paste and change example below - Create/Select Terraform workspace before deployment
- Run
terraform plan -var-file="<.tfvars file>
to check for any errors and see what will be built - Run
terraform apply -var-file="<.tfvars file>
to deploy infrastructure
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 plan -var-file=$1
terraform apply -var-file=$1
module "api_lambda_sqs" {
source = "crisboarna/terraform-aws-api-gateway-lambda-sqs"
version = "v1.6.0"
#Global
region = "eu-west-1"
project = "Awesome Project"
#API Gateway
api_gw_method = "POST"
#Lambda
lambda_function_name = "Awesome Endpoint"
lambda_description = "Awesome HTTP Endpoint Lambda"
lambda_runtime = "nodejs8.10"
lambda_handler = "dist/bin/lambda.handler"
lambda_timeout = 30
lambda_code_s3_bucket = "awesome-project-bucket"
lambda_code_s3_key = "awesome-project.zip"
lambda_code_s3_storage_class = "ONEZONE_IA"
lambda_code_s3_bucket_visibility = "private"
lambda_zip_path = "../../awesome-project.zip"
lambda_memory_size = 256
lambda_vpc_security_group_ids = [aws_security_group.vpc_security_group.id]
lambda_vpc_subnet_ids = [aws_subnet.vpc_subnet_a.id]
lambda_layers = [data.aws_lambda_layer_version.layer.arn]
#SQS
sqs_queue_names = ["SQS_QUEUE_NAME"]
sqs_queue_delay_seconds = [0]
sqs_queue_max_message_sizes = [2046]
sqs_queue_message_retention_seconds = [34565]
sqs_queue_receive_wait_time_seconds = [10]
sqs_queue_fifos = [true]
sqs_queue_content_based_deduplications = [true]
sqs_dead_letter_max_receive_counts = [3]
#Tags
tags = {
project = "Awesome Project"
managedby = "Terraform"
}
#Lambda Environment variables
environment_variables = {
NODE_ENV = "production"
}
}