AWS API Gateway v2 (HTTP/Websocket) Terraform module
Terraform module which creates API Gateway v2 resources with HTTP/Websocket capabilities.
This Terraform module is part of serverless.tf framework, which aims to simplify all operations when working with the serverless in Terraform.
Usage
HTTP API Gateway
module "api_gateway" {
source = "terraform-aws-modules/apigateway-v2/aws"
name = "dev-http"
description = "My awesome HTTP API Gateway"
protocol_type = "HTTP"
cors_configuration = {
allow_headers = ["content-type", "x-amz-date", "authorization", "x-api-key", "x-amz-security-token", "x-amz-user-agent"]
allow_methods = ["*"]
allow_origins = ["*"]
}
# Custom domain
domain_name = "terraform-aws-modules.modules.tf"
# Access logs
stage_access_log_settings = {
create_log_group = true
log_group_retention_in_days = 7
format = jsonencode({
context = {
domainName = "$context.domainName"
integrationErrorMessage = "$context.integrationErrorMessage"
protocol = "$context.protocol"
requestId = "$context.requestId"
requestTime = "$context.requestTime"
responseLength = "$context.responseLength"
routeKey = "$context.routeKey"
stage = "$context.stage"
status = "$context.status"
error = {
message = "$context.error.message"
responseType = "$context.error.responseType"
}
identity = {
sourceIP = "$context.identity.sourceIp"
}
integration = {
error = "$context.integration.error"
integrationStatus = "$context.integration.integrationStatus"
}
}
})
}
# Authorizer(s)
authorizers = {
"azure" = {
authorizer_type = "JWT"
identity_sources = ["$request.header.Authorization"]
name = "azure-auth"
jwt_configuration = {
audience = ["d6a38afd-45d6-4874-d1aa-3c5c558aqcc2"]
issuer = "https://sts.windows.net/aaee026e-8f37-410e-8869-72d9154873e4/"
}
}
}
# Routes & Integration(s)
routes = {
"POST /" = {
integration = {
uri = "arn:aws:lambda:eu-west-1:052235179155:function:my-function"
payload_format_version = "2.0"
timeout_milliseconds = 12000
}
}
"GET /some-route-with-authorizer" = {
authorizer_key = "azure"
integration = {
type = "HTTP_PROXY"
uri = "some url"
}
}
"GET /some-route-with-iam" = {
authorization_type = "AWS_IAM"
integration = {
uri = "arn:aws:lambda:eu-west-1:052235179155:function:my-function"
}
}
"$default" = {
integration = {
uri = "arn:aws:lambda:eu-west-1:052235179155:function:my-default-function"
}
}
}
tags = {
Environment = "dev"
Terraform = "true"
}
}
Multiple Subdomains
API Gateway v2 supports wildcard custom domains which allow users to map multiple subdomains to the same API Gateway. This is useful when you have multiple customers and you want to provide them with a custom domain for their API endpoint and possibly use that for header based routing/rules.
module "api_gateway" {
source = "terraform-aws-modules/apigateway-v2/aws"
...
domain_name = "*.mydomain.com"
subdomains = ["customer1", "customer2"]
...
}
This will create records that allow users to access the API Gateway using the following subdomains:
customer1.mydomain.comcustomer2.mydomain.com
Specific Hosted Zone
If you want to create the domain name in a specific hosted zone, you can use the hosted_zone_name input parameter:
module "api_gateway" {
source = "terraform-aws-modules/apigateway-v2/aws"
...
hosted_zone_name = "api.mydomain.com"
domain_name = "prod.api.mydomain.com"
...
}
Conditional Creation
The following values are provided to toggle on/off creation of the associated resources as desired:
module "api_gateway" {
source = "terraform-aws-modules/apigateway-v2/aws"
# Disable creation of the API and all resources
create = false
# Disable creation of the domain name and API mapping
create_domain_name = false
# Disable creation of Route53 alias record(s) for the custom domain
create_domain_records = false
# Disable creation of the ACM certificate for the custom domain
create_certificate = false
# Disable creation of the routes and integrations
create_routes_and_integrations = false
# Disable creation of the stage
create_stage = false
# ... omitted
}
Examples
- Complete HTTP - Create API Gateway, authorizer, domain name, stage and other resources in various combinations
- HTTP with VPC Link - Create API Gateway with VPC link and integration with resources in VPC (eg. ALB)
- Websocket - Create Websocket API
Authors
Module managed by Anton Babenko. Check out serverless.tf to learn more about doing serverless with Terraform.
Please reach out to Betajob if you are looking for commercial support for your Terraform, AWS, or serverless project.
License
Apache 2 Licensed. See LICENSE for full details.