How to create Resource: aws_api_gateway_domain_name

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.

Example Usage

An end-to-end example of a REST API configured with OpenAPI can be found in the /examples/api-gateway-rest-api-openapi directory within the GitHub repository.

Edge Optimized (ACM Certificate)

resource "aws_api_gateway_domain_name" "example" {
certificate_arn = aws_acm_certificate_validation.example.certificate_arn
domain_name = "api.example.com"
}
# Example DNS record using Route53.
# Route53 is not specifically required; any DNS host can be used.
resource "aws_route53_record" "example" {
name = aws_api_gateway_domain_name.example.domain_name
type = "A"
zone_id = aws_route53_zone.example.id
alias {
evaluate_target_health = true
name = aws_api_gateway_domain_name.example.cloudfront_domain_name
zone_id = aws_api_gateway_domain_name.example.cloudfront_zone_id
}
}

Edge Optimized (IAM Certificate)

resource "aws_api_gateway_domain_name" "example" {
domain_name = "api.example.com"
certificate_name = "example-api"
certificate_body = file("${path.module}/example.com/example.crt")
certificate_chain = file("${path.module}/example.com/ca.crt")
certificate_private_key = file("${path.module}/example.com/example.key")
}
# Example DNS record using Route53.
# Route53 is not specifically required; any DNS host can be used.
resource "aws_route53_record" "example" {
zone_id = aws_route53_zone.example.id # See aws_route53_zone for how to create this
name = aws_api_gateway_domain_name.example.domain_name
type = "A"
alias {
name = aws_api_gateway_domain_name.example.cloudfront_domain_name
zone_id = aws_api_gateway_domain_name.example.cloudfront_zone_id
evaluate_target_health = true
}
}

Regional (ACM Certificate)

resource "aws_api_gateway_domain_name" "example" {
domain_name = "api.example.com"
regional_certificate_arn = aws_acm_certificate_validation.example.certificate_arn
endpoint_configuration {
types = ["REGIONAL"]
}
}
# Example DNS record using Route53.
# Route53 is not specifically required; any DNS host can be used.
resource "aws_route53_record" "example" {
name = aws_api_gateway_domain_name.example.domain_name
type = "A"
zone_id = aws_route53_zone.example.id
alias {
evaluate_target_health = true
name = aws_api_gateway_domain_name.example.regional_domain_name
zone_id = aws_api_gateway_domain_name.example.regional_zone_id
}
}

Regional (IAM Certificate)

resource "aws_api_gateway_domain_name" "example" {
certificate_body = file("${path.module}/example.com/example.crt")
certificate_chain = file("${path.module}/example.com/ca.crt")
certificate_private_key = file("${path.module}/example.com/example.key")
domain_name = "api.example.com"
regional_certificate_name = "example-api"
endpoint_configuration {
types = ["REGIONAL"]
}
}
# Example DNS record using Route53.
# Route53 is not specifically required; any DNS host can be used.
resource "aws_route53_record" "example" {
name = aws_api_gateway_domain_name.example.domain_name
type = "A"
zone_id = aws_route53_zone.example.id
alias {
evaluate_target_health = true
name = aws_api_gateway_domain_name.example.regional_domain_name
zone_id = aws_api_gateway_domain_name.example.regional_zone_id
}
}

Argument Reference

The following arguments are supported:

  • domain_name - (Required) Fully-qualified domain name to register.
  • endpoint_configuration - (Optional) Configuration block defining API endpoint information including type. See below.
  • mutual_tls_authentication - (Optional) Mutual TLS authentication configuration for the domain name. See below.
  • ownership_verification_certificate_arn - (Optional) ARN of the AWS-issued certificate used to validate custom domain ownership (when certificate_arn is issued via an ACM Private CA or mutual_tls_authentication is configured with an ACM-imported certificate.)
  • security_policy - (Optional) Transport Layer Security (TLS) version + cipher suite for this DomainName. Valid values are TLS_1_0 and TLS_1_2. Must be configured to perform drift detection.
  • tags - (Optional) Key-value map of resource tags. If configured with a provider default_tags configuration block present, tags with matching keys will overwrite those defined at the provider-level.

When referencing an AWS-managed certificate, the following arguments are supported:

  • certificate_arn - (Optional) ARN for an AWS-managed certificate. AWS Certificate Manager is the only supported source. Used when an edge-optimized domain name is desired. Conflicts with certificate_name, certificate_body, certificate_chain, certificate_private_key, regional_certificate_arn, and regional_certificate_name.
  • regional_certificate_arn - (Optional) ARN for an AWS-managed certificate. AWS Certificate Manager is the only supported source. Used when a regional domain name is desired. Conflicts with certificate_arn, certificate_name, certificate_body, certificate_chain, and certificate_private_key.

When uploading a certificate, the following arguments are supported:

  • certificate_body - (Optional) Certificate issued for the domain name being registered, in PEM format. Only valid for EDGE endpoint configuration type. Conflicts with certificate_arn, regional_certificate_arn, and regional_certificate_name.
  • certificate_chain - (Optional) Certificate for the CA that issued the certificate, along with any intermediate CA certificates required to create an unbroken chain to a certificate trusted by the intended API clients. Only valid for EDGE endpoint configuration type. Conflicts with certificate_arn, regional_certificate_arn, and regional_certificate_name.
  • certificate_name - (Optional) Unique name to use when registering this certificate as an IAM server certificate. Conflicts with certificate_arn, regional_certificate_arn, and regional_certificate_name. Required if certificate_arn is not set.
  • certificate_private_key - (Optional) Private key associated with the domain certificate given in certificate_body. Only valid for EDGE endpoint configuration type. Conflicts with certificate_arn, regional_certificate_arn, and regional_certificate_name.
  • regional_certificate_name - (Optional) User-friendly name of the certificate that will be used by regional endpoint for this domain name. Conflicts with certificate_arn, certificate_name, certificate_body, certificate_chain, and certificate_private_key.

endpoint_configuration

  • types - (Required) List of endpoint types. This resource currently only supports managing a single value. Valid values: EDGE or REGIONAL. If unspecified, defaults to EDGE. Must be declared as REGIONAL in non-Commercial partitions. Refer to the documentation for more information on the difference between edge-optimized and regional APIs.

mutual_tls_authentication

  • truststore_uri - (Required) Amazon S3 URL that specifies the truststore for mutual TLS authentication, for example, s3://bucket-name/key-name. The truststore can contain certificates from public or private certificate authorities. To update the truststore, upload a new version to S3, and then update your custom domain name to use the new version.
  • truststore_version - (Optional) Version of the S3 object that contains the truststore. To specify a version, you must have versioning enabled for the S3 bucket.

Attributes Reference

In addition to all arguments above, the following attributes are exported:

  • arn - ARN of domain name.
  • certificate_upload_date - Upload date associated with the domain certificate.
  • cloudfront_domain_name - Hostname created by Cloudfront to represent the distribution that implements this domain name mapping.
  • cloudfront_zone_id - For convenience, the hosted zone ID (Z2FDTNDATAQYW2) that can be used to create a Route53 alias record for the distribution.
  • id - Internal identifier assigned to this domain name by API Gateway.
  • regional_domain_name - Hostname for the custom domain's regional endpoint.
  • regional_zone_id - Hosted zone ID that can be used to create a Route53 alias record for the regional endpoint.
  • tags_all - Map of tags assigned to the resource, including those inherited from the provider default_tags configuration block.

Import

API Gateway domain names can be imported using their name, e.g.,

$ terraform import aws_api_gateway_domain_name.example dev.example.com

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rakesh Tripathi

Rakesh Tripathi

Consulting Engineer, Software Developer, Infra, Quora