diff --git a/builtin/providers/aws/resource_aws_s3_bucket.go b/builtin/providers/aws/resource_aws_s3_bucket.go index 21672198f..11f507fdc 100644 --- a/builtin/providers/aws/resource_aws_s3_bucket.go +++ b/builtin/providers/aws/resource_aws_s3_bucket.go @@ -55,6 +55,12 @@ func resourceAwsS3Bucket() *schema.Resource { Computed: true, }, + "region": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "website_endpoint": &schema.Schema{ Type: schema.TypeString, Optional: true, @@ -149,7 +155,7 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { return err } - // Fetch the region + // Add the region as an attribute location, err := s3conn.GetBucketLocation( &s3.GetBucketLocationInput{ Bucket: aws.String(d.Id()), @@ -162,6 +168,12 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { if location.LocationConstraint != nil { region = *location.LocationConstraint } + if region == "" { + region = "us-east-1" + } + if err := d.Set("region", region); err != nil { + return err + } // Add the hosted zone ID for this bucket's region as an attribute hostedZoneID := s3.HostedZoneIDForRegion(region) diff --git a/builtin/providers/aws/resource_aws_s3_bucket_test.go b/builtin/providers/aws/resource_aws_s3_bucket_test.go index ba0974f68..1d7109d21 100644 --- a/builtin/providers/aws/resource_aws_s3_bucket_test.go +++ b/builtin/providers/aws/resource_aws_s3_bucket_test.go @@ -25,6 +25,8 @@ func TestAccAWSS3Bucket_basic(t *testing.T) { testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), resource.TestCheckResourceAttr( "aws_s3_bucket.bucket", "hosted_zone_id", s3.HostedZoneIDForRegion("us-west-2")), + resource.TestCheckResourceAttr( + "aws_s3_bucket.bucket", "region", "us-west-2"), resource.TestCheckResourceAttr( "aws_s3_bucket.bucket", "website_endpoint", ""), ), diff --git a/website/source/docs/providers/aws/r/s3_bucket.html.markdown b/website/source/docs/providers/aws/r/s3_bucket.html.markdown index 07bc54644..204be87e0 100644 --- a/website/source/docs/providers/aws/r/s3_bucket.html.markdown +++ b/website/source/docs/providers/aws/r/s3_bucket.html.markdown @@ -60,4 +60,5 @@ The following attributes are exported: * `id` - The name of the bucket. * `hosted_zone_id` - The [Route 53 Hosted Zone ID](http://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.