Skip to content

Commit 323c2dc

Browse files
authored
Merge pull request #23816 from hashicorp/s3-bucket-acceleration-status
r/s3_bucket: make `acceleration status` configurable
2 parents 7ad4497 + 7b84f21 commit 323c2dc

File tree

5 files changed

+143
-5
lines changed

5 files changed

+143
-5
lines changed

.changelog/23816.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/aws_s3_bucket: Update `acceleration_status` parameter to be configurable. Please refer to the documentation for details on drift detection and potential conflicts when configuring this parameter with the standalone `aws_s3_bucket_accelerate_configuration` resource.
3+
```

internal/service/s3/bucket.go

+27-4
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,11 @@ func ResourceBucket() *schema.Resource {
378378
},
379379

380380
"acceleration_status": {
381-
Type: schema.TypeString,
382-
Computed: true,
383-
Deprecated: "Use the aws_s3_bucket_accelerate_configuration resource instead",
381+
Type: schema.TypeString,
382+
Optional: true,
383+
Computed: true,
384+
Deprecated: "Use the aws_s3_bucket_accelerate_configuration resource instead",
385+
ValidateFunc: validation.StringInSlice(s3.BucketAccelerateStatus_Values(), false),
384386
},
385387

386388
"request_payer": {
@@ -762,6 +764,12 @@ func resourceBucketUpdate(d *schema.ResourceData, meta interface{}) error {
762764
}
763765
}
764766

767+
if d.HasChange("acceleration_status") {
768+
if err := resourceBucketInternalAccelerationUpdate(conn, d); err != nil {
769+
return fmt.Errorf("error updating S3 Bucket (%s) Acceleration Status: %w", d.Id(), err)
770+
}
771+
}
772+
765773
if d.HasChange("acl") && !d.IsNewResource() {
766774
if err := resourceBucketInternalACLUpdate(conn, d); err != nil {
767775
return fmt.Errorf("error updating S3 Bucket (%s) ACL: %w", d.Id(), err)
@@ -1000,7 +1008,7 @@ func resourceBucketRead(d *schema.ResourceData, meta interface{}) error {
10001008

10011009
// Amazon S3 Transfer Acceleration might not be supported in the region
10021010
if err != nil && !tfawserr.ErrCodeEquals(err, ErrCodeMethodNotAllowed, ErrCodeUnsupportedArgument, ErrCodeNotImplemented) {
1003-
return fmt.Errorf("error getting S3 Bucket acceleration configuration: %w", err)
1011+
return fmt.Errorf("error getting S3 Bucket (%s) accelerate configuration: %w", d.Id(), err)
10041012
}
10051013

10061014
if accelerate, ok := accelerateResponse.(*s3.GetBucketAccelerateConfigurationOutput); ok {
@@ -1482,6 +1490,21 @@ func normalizeRegion(region string) string {
14821490

14831491
////////////////////////////////////////// Argument-Specific Update Functions //////////////////////////////////////////
14841492

1493+
func resourceBucketInternalAccelerationUpdate(conn *s3.S3, d *schema.ResourceData) error {
1494+
input := &s3.PutBucketAccelerateConfigurationInput{
1495+
Bucket: aws.String(d.Id()),
1496+
AccelerateConfiguration: &s3.AccelerateConfiguration{
1497+
Status: aws.String(d.Get("acceleration_status").(string)),
1498+
},
1499+
}
1500+
1501+
_, err := verify.RetryOnAWSCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) {
1502+
return conn.PutBucketAccelerateConfiguration(input)
1503+
})
1504+
1505+
return err
1506+
}
1507+
14851508
func resourceBucketInternalACLUpdate(conn *s3.S3, d *schema.ResourceData) error {
14861509
acl := d.Get("acl").(string)
14871510
if acl == "" {

internal/service/s3/bucket_accelerate_configuration_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,66 @@ func TestAccS3BucketAccelerateConfiguration_disappears(t *testing.T) {
109109
})
110110
}
111111

112+
func TestAccS3BucketAccelerateConfiguration_migrate_noChange(t *testing.T) {
113+
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
114+
resourceName := "aws_s3_bucket_accelerate_configuration.test"
115+
bucketResourceName := "aws_s3_bucket.test"
116+
117+
resource.ParallelTest(t, resource.TestCase{
118+
PreCheck: func() { acctest.PreCheck(t) },
119+
ErrorCheck: acctest.ErrorCheck(t, s3.EndpointsID),
120+
ProviderFactories: acctest.ProviderFactories,
121+
CheckDestroy: testAccCheckBucketAccelerateConfigurationDestroy,
122+
Steps: []resource.TestStep{
123+
{
124+
Config: testAccBucketConfig_withAcceleration(rName, s3.BucketAccelerateStatusEnabled),
125+
Check: resource.ComposeAggregateTestCheckFunc(
126+
testAccCheckBucketExists(bucketResourceName),
127+
resource.TestCheckResourceAttr(bucketResourceName, "acceleration_status", s3.BucketAccelerateStatusEnabled),
128+
),
129+
},
130+
{
131+
Config: testAccBucketAccelerateConfigurationBasicConfig(rName, s3.BucketAccelerateStatusEnabled),
132+
Check: resource.ComposeAggregateTestCheckFunc(
133+
testAccCheckBucketAccelerateConfigurationExists(resourceName),
134+
resource.TestCheckResourceAttrPair(resourceName, "bucket", bucketResourceName, "id"),
135+
resource.TestCheckResourceAttr(resourceName, "status", s3.BucketAccelerateStatusEnabled),
136+
),
137+
},
138+
},
139+
})
140+
}
141+
142+
func TestAccS3BucketAccelerateConfiguration_migrate_withChange(t *testing.T) {
143+
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
144+
resourceName := "aws_s3_bucket_accelerate_configuration.test"
145+
bucketResourceName := "aws_s3_bucket.test"
146+
147+
resource.ParallelTest(t, resource.TestCase{
148+
PreCheck: func() { acctest.PreCheck(t) },
149+
ErrorCheck: acctest.ErrorCheck(t, s3.EndpointsID),
150+
ProviderFactories: acctest.ProviderFactories,
151+
CheckDestroy: testAccCheckBucketAccelerateConfigurationDestroy,
152+
Steps: []resource.TestStep{
153+
{
154+
Config: testAccBucketConfig_withAcceleration(rName, s3.BucketAccelerateStatusEnabled),
155+
Check: resource.ComposeAggregateTestCheckFunc(
156+
testAccCheckBucketExists(bucketResourceName),
157+
resource.TestCheckResourceAttr(bucketResourceName, "acceleration_status", s3.BucketAccelerateStatusEnabled),
158+
),
159+
},
160+
{
161+
Config: testAccBucketAccelerateConfigurationBasicConfig(rName, s3.BucketAccelerateStatusSuspended),
162+
Check: resource.ComposeAggregateTestCheckFunc(
163+
testAccCheckBucketAccelerateConfigurationExists(resourceName),
164+
resource.TestCheckResourceAttrPair(resourceName, "bucket", bucketResourceName, "id"),
165+
resource.TestCheckResourceAttr(resourceName, "status", s3.BucketAccelerateStatusSuspended),
166+
),
167+
},
168+
},
169+
})
170+
}
171+
112172
func testAccCheckBucketAccelerateConfigurationDestroy(s *terraform.State) error {
113173
conn := acctest.Provider.Meta().(*conns.AWSClient).S3Conn
114174

internal/service/s3/bucket_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/aws/aws-sdk-go/aws"
1313
"github.com/aws/aws-sdk-go/aws/endpoints"
1414
"github.com/aws/aws-sdk-go/service/cloudformation"
15+
"github.com/aws/aws-sdk-go/service/cloudfront"
1516
"github.com/aws/aws-sdk-go/service/s3"
1617
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
1718
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
@@ -225,6 +226,43 @@ func TestAccS3Bucket_Basic_forceDestroyWithObjectLockEnabled(t *testing.T) {
225226
})
226227
}
227228

229+
func TestAccS3Bucket_Basic_acceleration(t *testing.T) {
230+
bucketName := sdkacctest.RandomWithPrefix("tf-test-bucket")
231+
resourceName := "aws_s3_bucket.test"
232+
233+
resource.ParallelTest(t, resource.TestCase{
234+
PreCheck: func() {
235+
acctest.PreCheck(t)
236+
acctest.PreCheckPartitionHasService(cloudfront.EndpointsID, t)
237+
},
238+
ErrorCheck: acctest.ErrorCheck(t, s3.EndpointsID),
239+
Providers: acctest.Providers,
240+
CheckDestroy: testAccCheckBucketDestroy,
241+
Steps: []resource.TestStep{
242+
{
243+
Config: testAccBucketConfig_withAcceleration(bucketName, s3.BucketAccelerateStatusEnabled),
244+
Check: resource.ComposeTestCheckFunc(
245+
testAccCheckBucketExists(resourceName),
246+
resource.TestCheckResourceAttr(resourceName, "acceleration_status", s3.BucketAccelerateStatusEnabled),
247+
),
248+
},
249+
{
250+
ResourceName: resourceName,
251+
ImportState: true,
252+
ImportStateVerify: true,
253+
ImportStateVerifyIgnore: []string{"force_destroy"},
254+
},
255+
{
256+
Config: testAccBucketConfig_withAcceleration(bucketName, s3.BucketAccelerateStatusSuspended),
257+
Check: resource.ComposeTestCheckFunc(
258+
testAccCheckBucketExists(resourceName),
259+
resource.TestCheckResourceAttr(resourceName, "acceleration_status", s3.BucketAccelerateStatusSuspended),
260+
),
261+
},
262+
},
263+
})
264+
}
265+
228266
// Test TestAccS3Bucket_disappears is designed to fail with a "plan
229267
// not empty" error in Terraform, to check against regressions.
230268
// See https://github.com/hashicorp/terraform/pull/2925
@@ -1275,6 +1313,15 @@ resource "aws_s3_bucket" "bucket" {
12751313
`, bucketName)
12761314
}
12771315

1316+
func testAccBucketConfig_withAcceleration(bucketName, acceleration string) string {
1317+
return fmt.Sprintf(`
1318+
resource "aws_s3_bucket" "test" {
1319+
bucket = %[1]q
1320+
acceleration_status = %[2]q
1321+
}
1322+
`, bucketName, acceleration)
1323+
}
1324+
12781325
func testAccBucketConfig_withACL(bucketName, acl string) string {
12791326
return fmt.Sprintf(`
12801327
resource "aws_s3_bucket" "bucket" {

website/docs/r/s3_bucket.html.markdown

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Provides a S3 bucket resource.
1212

1313
-> This functionality is for managing S3 in an AWS Partition. To manage [S3 on Outposts](https://docs.aws.amazon.com/AmazonS3/latest/dev/S3onOutposts.html), see the [`aws_s3control_bucket`](/docs/providers/aws/r/s3control_bucket.html) resource.
1414

15+
~> **NOTE on S3 Bucket Accelerate Configuration:** S3 Bucket Accelerate can be configured in either the standalone resource [`aws_s3_bucket_accelerate_configuration`](s3_bucket_accelerate_configuration.html)
16+
or with the deprecated parameter `acceleration_status` in the resource `aws_s3_bucket`.
17+
Configuring with both will cause inconsistencies and may overwrite configuration.
18+
1519
~> **NOTE on S3 Bucket canned ACL Configuration:** S3 Bucket canned ACL can be configured in either the standalone resource [`aws_s3_bucket_acl`](s3_bucket_acl.html.markdown)
1620
or with the deprecated parameter `acl` in the resource `aws_s3_bucket`.
1721
Configuring with both will cause inconsistencies and may overwrite configuration.
@@ -113,6 +117,8 @@ The following arguments are supported:
113117

114118
* `bucket` - (Optional, Forces new resource) The name of the bucket. If omitted, Terraform will assign a random, unique name. Must be lowercase and less than or equal to 63 characters in length. A full list of bucket naming rules [may be found here](https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html).
115119
* `bucket_prefix` - (Optional, Forces new resource) Creates a unique bucket name beginning with the specified prefix. Conflicts with `bucket`. Must be lowercase and less than or equal to 37 characters in length. A full list of bucket naming rules [may be found here](https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html).
120+
* `acceleration_status` - (Optional, **Deprecated**) Sets the accelerate configuration of an existing bucket. Can be `Enabled` or `Suspended`. Cannot be used in `cn-north-1` or `us-gov-west-1`. Terraform will only perform drift detection if a configuration value is provided.
121+
Use the resource [`aws_s3_bucket_accelerate_configuration`](s3_bucket_accelerate_configuration.html) instead.
116122
* `acl` - (Optional, **Deprecated**) The [canned ACL](https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl) to apply. Valid values are `private`, `public-read`, `public-read-write`, `aws-exec-read`, `authenticated-read`, and `log-delivery-write`. Defaults to `private`. Conflicts with `grant`. Terraform will only perform drift detection if a configuration value is provided. Use the resource [`aws_s3_bucket_acl`](s3_bucket_acl.html.markdown) instead.
117123
* `grant` - (Optional, **Deprecated**) An [ACL policy grant](https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#sample-acl). See [Grant](#grant) below for details. Conflicts with `acl`. Terraform will only perform drift detection if a configuration value is provided. Use the resource [`aws_s3_bucket_acl`](s3_bucket_acl.html.markdown) instead.
118124
* `force_destroy` - (Optional, Default:`false`) A boolean that indicates all objects (including any [locked objects](https://docs.aws.amazon.com/AmazonS3/latest/dev/object-lock-overview.html)) should be deleted from the bucket so that the bucket can be destroyed without error. These objects are *not* recoverable.
@@ -147,7 +153,6 @@ The `object_lock_configuration` configuration block supports the following argum
147153
In addition to all arguments above, the following attributes are exported:
148154

149155
* `id` - The name of the bucket.
150-
* `acceleration_status` - (Optional) The accelerate configuration status of the bucket. Not available in `cn-north-1` or `us-gov-west-1`.
151156
* `arn` - The ARN of the bucket. Will be of format `arn:aws:s3:::bucketname`.
152157
* `bucket_domain_name` - The bucket domain name. Will be of format `bucketname.s3.amazonaws.com`.
153158
* `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.

0 commit comments

Comments
 (0)