diff --git a/.changelog/21943.txt b/.changelog/21943.txt new file mode 100644 index 00000000000..f9aa7ff28e3 --- /dev/null +++ b/.changelog/21943.txt @@ -0,0 +1,7 @@ +```release-note:bug +resource/aws_cloudfront_distribution: Correct `hosted_zone_id` for AWS China regions +``` + +```release-note:bug +data-source/aws_cloudfront_distribution: Correct `hosted_zone_id` for AWS China regions +``` \ No newline at end of file diff --git a/internal/service/cloudfront/distribution.go b/internal/service/cloudfront/distribution.go index 0b2b5b88ee4..ceb88d33d0f 100644 --- a/internal/service/cloudfront/distribution.go +++ b/internal/service/cloudfront/distribution.go @@ -6,6 +6,7 @@ import ( "time" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/endpoints" "github.com/aws/aws-sdk-go/service/cloudfront" "github.com/hashicorp/aws-sdk-go-base/tfawserr" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -899,6 +900,14 @@ func resourceDistributionRead(d *schema.ResourceData, meta interface{}) error { d.Set("etag", resp.ETag) d.Set("arn", resp.Distribution.ARN) + // override hosted_zone_id from flattenDistributionConfig + region := meta.(*conns.AWSClient).Region + if v, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), region); ok && v.ID() == endpoints.AwsCnPartitionID { + d.Set("hosted_zone_id", cloudFrontCNRoute53ZoneID) + } else { + d.Set("hosted_zone_id", cloudFrontRoute53ZoneID) + } + tags, err := ListTags(conn, d.Get("arn").(string)) if err != nil { return fmt.Errorf("error listing tags for CloudFront Distribution (%s): %s", d.Id(), err) diff --git a/internal/service/cloudfront/distribution_configuration_structure.go b/internal/service/cloudfront/distribution_configuration_structure.go index 778baeedaa0..9035c1d76b1 100644 --- a/internal/service/cloudfront/distribution_configuration_structure.go +++ b/internal/service/cloudfront/distribution_configuration_structure.go @@ -24,6 +24,11 @@ import ( // is used to set the zone_id attribute. const cloudFrontRoute53ZoneID = "Z2FDTNDATAQYW2" +// cloudFrontCNRoute53ZoneID defines the route 53 zone ID for CloudFront in AWS CN. +// This is used to set the zone_id attribute. +// ref: https://docs.amazonaws.cn/en_us/aws/latest/userguide/route53.html +const cloudFrontCNRoute53ZoneID = "Z3RFFRIM2A3IF5" + // Assemble the *cloudfront.DistributionConfig variable. Calls out to various // expander functions to convert attributes and sub-attributes to the various // complex structures which are necessary to properly build the diff --git a/internal/service/cloudfront/distribution_data_source.go b/internal/service/cloudfront/distribution_data_source.go index 540ab205eba..7dc6e6979f6 100644 --- a/internal/service/cloudfront/distribution_data_source.go +++ b/internal/service/cloudfront/distribution_data_source.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/endpoints" "github.com/aws/aws-sdk-go/service/cloudfront" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-aws/internal/conns" @@ -80,6 +81,12 @@ func dataSourceDistributionRead(d *schema.ResourceData, meta interface{}) error d.Set("in_progress_validation_batches", distribution.InProgressInvalidationBatches) d.Set("last_modified_time", aws.String(distribution.LastModifiedTime.String())) d.Set("status", distribution.Status) + region := meta.(*conns.AWSClient).Region + if v, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), region); ok && v.ID() == endpoints.AwsCnPartitionID { + d.Set("hosted_zone_id", cloudFrontCNRoute53ZoneID) + } else { + d.Set("hosted_zone_id", cloudFrontRoute53ZoneID) + } if distributionConfig := distribution.DistributionConfig; distributionConfig != nil { d.Set("enabled", distributionConfig.Enabled) } @@ -92,6 +99,5 @@ func dataSourceDistributionRead(d *schema.ResourceData, meta interface{}) error return fmt.Errorf("error setting tags: %w", err) } - d.Set("hosted_zone_id", cloudFrontRoute53ZoneID) return nil }