diff --git a/builtin/providers/aws/resource_aws_s3_bucket.go b/builtin/providers/aws/resource_aws_s3_bucket.go index 9eb8c9adb0a5..30fea3d5f822 100644 --- a/builtin/providers/aws/resource_aws_s3_bucket.go +++ b/builtin/providers/aws/resource_aws_s3_bucket.go @@ -50,6 +50,12 @@ func resourceAwsS3Bucket() *schema.Resource { ForceNew: false, }, + "website_endpoint": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "tags": tagsSchema(), }, } @@ -118,6 +124,15 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { } } + // Add website_endpoint as an output + endpoint, err := websiteEndpoint(s3conn, d) + if err != nil { + return err + } + if err := d.Set("website_endpoint", endpoint); err != nil { + return err + } + tagSet, err := getTagSetS3(s3conn, d.Id()) if err != nil { return err @@ -184,3 +199,31 @@ func updateWebsite(s3conn *s3.S3, d *schema.ResourceData) error { return nil } + +func websiteEndpoint(s3conn *s3.S3, d *schema.ResourceData) (endpoint string, err error) { + // If the bucket doess't have a website configuration, return an empty endpoint + if !d.Get("website").(bool) { + return + } + + bucket := d.Get("bucket").(string) + + // Lookup the region for this bucket + location, err := s3conn.GetBucketLocation(&s3.GetBucketLocationInput{Bucket: aws.String(bucket)}) + if err != nil { + return + } + var region string + if location.LocationConstraint != nil { + region = *location.LocationConstraint + } + + // Default to us-east-1 if the bucket doesn't have a region + if region == "" { + region = "us-east-1" + } + + endpoint = fmt.Sprintf("%s.s3-website-%s.amazonaws.com", bucket, region) + + return +}