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

Issue #4540 Add new attribute bucket_regional_domain_name in aws_s3_bucket #4556

Merged
merged 6 commits into from
May 30, 2018
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
30 changes: 30 additions & 0 deletions aws/resource_aws_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/hashcode"
Expand Down Expand Up @@ -51,6 +52,11 @@ func resourceAwsS3Bucket() *schema.Resource {
Computed: true,
},

"bucket_regional_domain_name": {
Type: schema.TypeString,
Computed: true,
},

"arn": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -1078,6 +1084,13 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
return err
}

// Add the bucket_regional_domain_name as an attribute
regionalEndpoint, err := BucketRegionalDomainName(d.Get("bucket").(string), region)
if err != nil {
return err
}
d.Set("bucket_regional_domain_name", regionalEndpoint)

// Add the hosted zone ID for this bucket's region as an attribute
hostedZoneID, err := HostedZoneIDForRegion(region)
if err != nil {
Expand Down Expand Up @@ -1441,6 +1454,23 @@ func bucketDomainName(bucket string) string {
return fmt.Sprintf("%s.s3.amazonaws.com", bucket)
}

func BucketRegionalDomainName(bucket string, region string) (string, error) {
// https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region

for _, partition := range endpoints.DefaultPartitions() {
for _, reg := range partition.Regions() {
if region == reg.ID() {
regionEndpointS3, err := reg.ResolveEndpoint(endpoints.S3ServiceID)
if err != nil {
return "", err
}
return fmt.Sprintf("%s.%s", bucket, strings.TrimPrefix(regionEndpointS3.URL, "https://")), nil
}
}
}
return "", fmt.Errorf("Regional endpoint not found for bucket %s", bucket)
}

func WebsiteEndpoint(bucket string, region string) *S3Website {
domain := WebsiteDomainUrl(region)
return &S3Website{Endpoint: fmt.Sprintf("%s.%s", bucket, domain), Domain: domain}
Expand Down
11 changes: 11 additions & 0 deletions aws/resource_aws_s3_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func TestAccAWSS3Bucket_basic(t *testing.T) {
"aws_s3_bucket.bucket", "bucket", testAccBucketName(rInt)),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "bucket_domain_name", testAccBucketDomainName(rInt)),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "bucket_regional_domain_name", testAccBucketRegionalDomainName(rInt, region)),
),
},
},
Expand Down Expand Up @@ -1331,6 +1333,15 @@ func testAccBucketDomainName(randInt int) string {
return fmt.Sprintf("tf-test-bucket-%d.s3.amazonaws.com", randInt)
}

func testAccBucketRegionalDomainName(randInt int, region string) string {
bucket := fmt.Sprintf("tf-test-bucket-%d", randInt)
regionalEndpoint, err := BucketRegionalDomainName(bucket, region)
if err != nil {
return fmt.Sprintf("Regional endpoint not found for bucket %s", bucket)
}
return regionalEndpoint
}

func testAccWebsiteEndpoint(randInt int) string {
return fmt.Sprintf("tf-test-bucket-%d.s3-website-us-west-2.amazonaws.com", randInt)
}
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/cloudfront_distribution.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ resource "aws_s3_bucket" "b" {

resource "aws_cloudfront_distribution" "s3_distribution" {
origin {
domain_name = "${aws_s3_bucket.b.bucket_domain_name}"
domain_name = "${aws_s3_bucket.b.bucket_regional_domain_name}"
origin_id = "myS3Origin"

s3_origin_config {
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/s3_bucket.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ The following attributes are exported:
* `id` - The name of the bucket.
* `arn` - The ARN of the bucket. Will be of format `arn:aws:s3:::bucketname`.
* `bucket_domain_name` - The bucket domain name. Will be of format `bucketname.s3.amazonaws.com`.
* `bucket_regional_domain_name` - The bucket region-specific domain name. The bucket domain name including the region name, please refer [here](https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region) for format. Note: The AWS CloudFront allows specifying S3 region-specific endpoint when creating S3 origin, it will prevent [redirect issues](https://forums.aws.amazon.com/thread.jspa?threadID=216814) from CloudFront to S3 Origin URL.
* `hosted_zone_id` - The [Route 53 Hosted Zone ID](https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_website_region_endpoints) for this bucket's region.
* `region` - The AWS region this bucket resides in.
* `website_endpoint` - The website endpoint, if the bucket is configured with a website. If not, this will be an empty string.
Expand Down