Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a different cloudfront hosted_zone_id if in AWS CN #21943

Merged
merged 4 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/21943.txt
Original file line number Diff line number Diff line change
@@ -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
```
9 changes: 9 additions & 0 deletions internal/service/cloudfront/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion internal/service/cloudfront/distribution_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}