diff --git a/builtin/providers/aws/resource_aws_s3_bucket.go b/builtin/providers/aws/resource_aws_s3_bucket.go index 170f88970c11..2804ae8dd424 100644 --- a/builtin/providers/aws/resource_aws_s3_bucket.go +++ b/builtin/providers/aws/resource_aws_s3_bucket.go @@ -31,6 +31,11 @@ func resourceAwsS3Bucket() *schema.Resource { ForceNew: true, }, + "policy": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + }, + "website": &schema.Schema{ Type: schema.TypeList, Optional: true, @@ -121,8 +126,14 @@ func resourceAwsS3BucketUpdate(d *schema.ResourceData, meta interface{}) error { return err } - if err := resourceAwsS3BucketWebsiteUpdate(s3conn, d); err != nil { - return err + if d.HasChange("website") { + if err := resourceAwsS3BucketPolicyUpdate(s3conn, d); err != nil { + return err + } + + if err := resourceAwsS3BucketWebsiteUpdate(s3conn, d); err != nil { + return err + } } return resourceAwsS3BucketRead(d, meta) @@ -228,11 +239,37 @@ func resourceAwsS3BucketDelete(d *schema.ResourceData, meta interface{}) error { return nil } -func resourceAwsS3BucketWebsiteUpdate(s3conn *s3.S3, d *schema.ResourceData) error { - if !d.HasChange("website") { - return nil +func resourceAwsS3BucketPolicyUpdate(s3conn *s3.S3, d *schema.ResourceData) error { + bucket := d.Get("bucket").(string) + policy := d.Get("policy").(string) + + if policy != "" { + input := &s3.PutBucketPolicyInput{ + Bucket: aws.String(bucket), + Policy: aws.String(policy), + } + + log.Printf("[DEBUG] S3 put bucket policy: %s", input) + + _, err := s3conn.PutBucketPolicy(input) + if err != nil { + return fmt.Errorf("Error putting S3 policy: %s", err) + } + } else { + input := &s3.DeleteBucketPolicyInput{ + Bucket: aws.String(bucket), + } + + _, err := s3conn.DeleteBucketPolicy(input) + if err != nil { + return fmt.Errorf("Error deleting S3 policy: %s", err) + } } + return nil +} + +func resourceAwsS3BucketWebsiteUpdate(s3conn *s3.S3, d *schema.ResourceData) error { ws := d.Get("website").([]interface{}) if len(ws) == 1 { 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 78e0938f828e..30f6d333b238 100644 --- a/website/source/docs/providers/aws/r/s3_bucket.html.markdown +++ b/website/source/docs/providers/aws/r/s3_bucket.html.markdown @@ -46,6 +46,7 @@ The following arguments are supported: * `bucket` - (Required) The name of the bucket. * `acl` - (Optional) The [canned ACL](http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl) to apply. Defaults to "private". +* `policy` - (Optional) A valid [bucket policy](http://docs.aws.amazon.com/AmazonS3/latest/dev/example-bucket-policies.html) JSON document. * `tags` - (Optional) A mapping of tags to assign to the bucket. * `website` - (Optional) A website object (documented below).