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

main.tf#L22
resource "aws_api_gateway_domain_name" "missing_security_policy" {
}

resource "aws_api_gateway_domain_name" "empty_security_policy" {
    security_policy = ""
}
main.tf#L22
resource "aws_api_gateway_domain_name" "missing_security_policy" {
}

resource "aws_api_gateway_domain_name" "empty_security_policy" {
    security_policy = ""
}
test_api_gateway_name.tf#L1
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.}}
  #                 ^^^^^^^^^
}

main.tf#L26
resource "aws_api_gateway_domain_name" "missing_security_policy" {
}

resource "aws_api_gateway_domain_name" "empty_security_policy" {
    security_policy = ""
}
main.tf#L22
resource "aws_api_gateway_domain_name" "missing_security_policy" {
}

resource "aws_api_gateway_domain_name" "empty_security_policy" {
    security_policy = ""
}

Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

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.

risk-label

Ensure to use modern TLS protocols

It is better to adopt TLS v1.2+.

Review your AWS API Gateway settings

You can check if the aws_api_gateway_domain_name setting in your .tf file is correct in 3 min with Shisho Cloud.

Parameters

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 the cloudfront_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 the regional_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: The aws_api_gateway_domain_name resource expects dependency on the aws_acm_certificate_validation as only verified certificates can be used. This can be made either explicitly by adding the depends_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.

risk-label

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.

risk-label

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).

risk-label

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.

Review your AWS API Gateway settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

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

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.