From 25244b6684c78209dd5d297347875ed7c9ffd995 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 16 Oct 2024 09:04:21 -0500 Subject: [PATCH 1/3] aws_dynamodb_table: add basic test for payPerRequest billing mode --- internal/service/dynamodb/table_test.go | 43 +++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/internal/service/dynamodb/table_test.go b/internal/service/dynamodb/table_test.go index 99c74290918..2a930bdbe6b 100644 --- a/internal/service/dynamodb/table_test.go +++ b/internal/service/dynamodb/table_test.go @@ -846,6 +846,45 @@ func TestAccDynamoDBTable_BillingModeGSI_provisionedToPayPerRequest(t *testing.T }) } +func TestAccDynamoDBTable_BillingMode_payPerRequestBasic(t *testing.T) { + ctx := acctest.Context(t) + var conf awstypes.TableDescription + resourceName := "aws_dynamodb_table.test" + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.DynamoDBServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckTableDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccTableConfig_billingPayPerRequest(rName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckInitialTableExists(ctx, resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "billing_mode", string(awstypes.BillingModePayPerRequest)), + resource.TestCheckResourceAttr(resourceName, "read_capacity", acctest.Ct0), + resource.TestCheckResourceAttr(resourceName, "write_capacity", acctest.Ct0), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccTableConfig_billingPayPerRequestGSI(rName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckInitialTableExists(ctx, resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "billing_mode", string(awstypes.BillingModePayPerRequest)), + resource.TestCheckResourceAttr(resourceName, "read_capacity", acctest.Ct0), + resource.TestCheckResourceAttr(resourceName, "write_capacity", acctest.Ct0), + ), + }, + }, + }) +} + func TestAccDynamoDBTable_onDemandThroughput(t *testing.T) { ctx := acctest.Context(t) var conf awstypes.TableDescription @@ -1781,8 +1820,8 @@ func TestAccDynamoDBTable_restoreCrossRegion(t *testing.T) { testAccCheckInitialTableExists(ctx, resourceName, &conf), resource.TestCheckResourceAttr(resourceName, names.AttrName, rName), resource.TestCheckResourceAttr(resourceNameRestore, names.AttrName, rNameRestore), - acctest.MatchResourceAttrRegionalARNRegion(resourceName, names.AttrARN, "dynamodb", acctest.Region(), regexache.MustCompile(`table/XXX`)), - acctest.MatchResourceAttrRegionalARNRegion(resourceNameRestore, names.AttrARN, "dynamodb", acctest.AlternateRegion(), regexache.MustCompile(`table/YYY`)), + acctest.MatchResourceAttrRegionalARNRegion(resourceName, names.AttrARN, "dynamodb", acctest.Region(), regexache.MustCompile(`table/.+$`)), + acctest.MatchResourceAttrRegionalARNRegion(resourceNameRestore, names.AttrARN, "dynamodb", acctest.AlternateRegion(), regexache.MustCompile(`table/.+$`)), ), }, { From 2a142adf377810b6bbc7aafbb9296ba7081ba133 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 16 Oct 2024 11:04:12 -0500 Subject: [PATCH 2/3] aws_dynamodb_table: skip payPerRequest updates if throughput not specified --- internal/service/dynamodb/table.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/internal/service/dynamodb/table.go b/internal/service/dynamodb/table.go index 1a2483034cd..b2486c5b43a 100644 --- a/internal/service/dynamodb/table.go +++ b/internal/service/dynamodb/table.go @@ -272,9 +272,10 @@ func resourceTable() *schema.Resource { ForceNew: true, }, "read_capacity": { - Type: schema.TypeInt, - Optional: true, - Computed: true, + Type: schema.TypeInt, + Optional: true, + Computed: true, + ConflictsWith: []string{"on_demand_throughput"}, }, "replica": { Type: schema.TypeSet, @@ -483,9 +484,10 @@ func resourceTable() *schema.Resource { DiffSuppressFunc: verify.SuppressMissingOptionalConfigurationBlock, }, "write_capacity": { - Type: schema.TypeInt, - Computed: true, - Optional: true, + Type: schema.TypeInt, + Computed: true, + Optional: true, + ConflictsWith: []string{"on_demand_throughput"}, }, }, } @@ -1079,7 +1081,7 @@ func resourceTableUpdate(ctx context.Context, d *schema.ResourceData, meta inter // update only on-demand throughput indexes when switching to PAY_PER_REQUEST if newBillingMode == awstypes.BillingModePayPerRequest { for _, gsiUpdate := range gsiUpdates { - if gsiUpdate.Update.OnDemandThroughput == nil { + if gsiUpdate.Update == nil || (gsiUpdate.Update != nil && gsiUpdate.Update.OnDemandThroughput == nil) { continue } @@ -1736,7 +1738,7 @@ func updateDiffGSI(oldGsi, newGsi []interface{}, billingMode awstypes.BillingMod } otherAttributesChanged := nonKeyAttributesChanged || !reflect.DeepEqual(oldAttributes, newAttributes) - if capacityChanged && !otherAttributesChanged { + if capacityChanged && !otherAttributesChanged && billingMode == awstypes.BillingModeProvisioned { update := awstypes.GlobalSecondaryIndexUpdate{ Update: &awstypes.UpdateGlobalSecondaryIndexAction{ IndexName: aws.String(idxName), @@ -1744,7 +1746,7 @@ func updateDiffGSI(oldGsi, newGsi []interface{}, billingMode awstypes.BillingMod }, } ops = append(ops, update) - } else if onDemandThroughputChanged && !otherAttributesChanged { + } else if onDemandThroughputChanged && !otherAttributesChanged && billingMode == awstypes.BillingModePayPerRequest { update := awstypes.GlobalSecondaryIndexUpdate{ Update: &awstypes.UpdateGlobalSecondaryIndexAction{ IndexName: aws.String(idxName), From a1363129b43f9e54c78e2cb6dc4a0b5e141d1392 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 16 Oct 2024 11:10:43 -0500 Subject: [PATCH 3/3] add CHANGELOG entry --- .changelog/39752.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/39752.txt diff --git a/.changelog/39752.txt b/.changelog/39752.txt new file mode 100644 index 00000000000..f29d7910cbd --- /dev/null +++ b/.changelog/39752.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_dynamodb_table: Fix crash when `billing_mode` is set to `PAY_PER_REQUEST` without `global_secondary_index` updates +``` \ No newline at end of file