AWS API Gateway Domain Name
This page shows how to write Terraform and CloudFormation for API Gateway Domain Name and write them securely.
aws_api_gateway_domain_name (Terraform)
The Domain Name in API Gateway can be configured in Terraform with the resource name aws_api_gateway_domain_name
. The following sections describe 5 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_api_gateway_domain_name" "missing_security_policy" {
}
resource "aws_api_gateway_domain_name" "empty_security_policy" {
security_policy = ""
}
resource "aws_api_gateway_domain_name" "missing_security_policy" {
}
resource "aws_api_gateway_domain_name" "empty_security_policy" {
security_policy = ""
}
resource "aws_api_gateway_domain_name" "weak_ssl_protocol" {
domain_name = "api.example.com"
security_policy = "TLS_1_0" # Noncompliant {{Change this configuration to use a stronger protocol.}}
# ^^^^^^^^^
}
resource "aws_api_gateway_domain_name" "missing_security_policy" {
}
resource "aws_api_gateway_domain_name" "empty_security_policy" {
security_policy = ""
}
resource "aws_api_gateway_domain_name" "missing_security_policy" {
}
resource "aws_api_gateway_domain_name" "empty_security_policy" {
security_policy = ""
}
Security Best Practices for aws_api_gateway_domain_name
There is 1 setting in aws_api_gateway_domain_name that should be taken care of for security reasons. The following section explain an overview and example code.
Ensure to use modern TLS protocols
It is better to adopt TLS v1.2+.
Parameters
-
arn
optional computed - string -
certificate_arn
optional - string -
certificate_body
optional - string -
certificate_chain
optional - string -
certificate_name
optional - string -
certificate_private_key
optional - string -
certificate_upload_date
optional computed - string -
cloudfront_domain_name
optional computed - string -
cloudfront_zone_id
optional computed - string -
domain_name
required - string -
id
optional computed - string -
regional_certificate_arn
optional - string -
regional_certificate_name
optional - string -
regional_domain_name
optional computed - string -
regional_zone_id
optional computed - string -
security_policy
optional computed - string -
tags
optional - map from string to string -
endpoint_configuration
list block-
types
required - list of string
-
-
mutual_tls_authentication
list block-
truststore_uri
required - string -
truststore_version
optional - string
-
Explanation in Terraform Registry
Registers a custom domain name for use with AWS API Gateway. Additional information about this functionality can be found in the API Gateway Developer Guide. This resource just establishes ownership of and the TLS settings for a particular domain name. An API can be attached to a particular path under the registered domain name using the
aws_api_gateway_base_path_mapping
resource. API Gateway domains can be defined as either 'edge-optimized' or 'regional'. In an edge-optimized configuration, API Gateway internally creates and manages a CloudFront distribution to route requests on the given hostname. In addition to this resource it's necessary to create a DNS record corresponding to the given domain name which is an alias (either Route53 alias or traditional CNAME) to the Cloudfront domain name exported in thecloudfront_domain_name
attribute. In a regional configuration, API Gateway does not create a CloudFront distribution to route requests to the API, though a distribution can be created if needed. In either case, it is necessary to create a DNS record corresponding to the given domain name which is an alias (either Route53 alias or traditional CNAME) to the regional domain name exported in theregional_domain_name
attribute.Note: API Gateway requires the use of AWS Certificate Manager (ACM) certificates instead of Identity and Access Management (IAM) certificates in regions that support ACM. Regions that support ACM can be found in the Regions and Endpoints Documentation. To import an existing private key and certificate into ACM or request an ACM certificate, see the
aws_acm_certificate
resource. Note: Theaws_api_gateway_domain_name
resource expects dependency on theaws_acm_certificate_validation
as only verified certificates can be used. This can be made either explicitly by adding thedepends_on = [aws_acm_certificate_validation.cert]
attribute. Or implicitly by referring certificate ARN from the validation resource where it will be available after the resource creation:regional_certificate_arn = aws_acm_certificate_validation.cert.certificate_arn
. Note: All arguments including the private key will be stored in the raw state as plain-text. Read more about sensitive data in state.
Tips: Best Practices for The Other AWS API Gateway Resources
In addition to the aws_api_gateway_method_settings, AWS API Gateway has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.
aws_api_gateway_method_settings
Ensure that API Gateway stage-level cache is encrypted
It is better to enable the stage-level cache encryption which reduces the risk of data leakage.
aws_api_gateway_stage
Ensure to enable access logging of your API Gateway stage (v1)
It is better to enable the access logging of your API Gateway stage (v1).
aws_api_gateway_method
Ensure that your API Gateway method blocks unwanted access
It is better that the API Gateway method does not allow public access.
AWS::ApiGateway::DomainName (CloudFormation)
The DomainName in ApiGateway can be configured in CloudFormation with the resource name AWS::ApiGateway::DomainName
. The following sections describe how to use the resource and its parameters.
Example Usage from GitHub
An example could not be found in GitHub.
Parameters
-
DomainName
optional - String -
EndpointConfiguration
optional - EndpointConfiguration -
MutualTlsAuthentication
optional - MutualTlsAuthentication -
CertificateArn
optional - String -
RegionalCertificateArn
optional - String -
OwnershipVerificationCertificateArn
optional - String -
SecurityPolicy
optional - String -
Tags
optional - List of Tag
Explanation in CloudFormation Registry
The
AWS::ApiGateway::DomainName
resource specifies a custom domain name for your API in API Gateway.You can use a custom domain name to provide a URL that's more intuitive and easier to recall. For more information about using custom domain names, see Set up Custom Domain Name for an API in API Gateway in the API Gateway Developer Guide.
Frequently asked questions
What is AWS API Gateway Domain Name?
AWS API Gateway Domain Name is a resource for API Gateway of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.
Where can I find the example code for the AWS API Gateway Domain Name?
For Terraform, the georgearce24/aquasecurity-trivy, aquasecurity/trivy and SonarSource/sonar-iac source code examples are useful. See the Terraform Example section for further details.