diff --git a/.changelog/42658.txt b/.changelog/42658.txt new file mode 100644 index 000000000000..db08727f72ed --- /dev/null +++ b/.changelog/42658.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_cloudwatch_log_group: Add support for `DELIVERY` log group class. +``` diff --git a/internal/service/logs/group.go b/internal/service/logs/group.go index 8c6c7769d3de..64ff9c36cdc9 100644 --- a/internal/service/logs/group.go +++ b/internal/service/logs/group.go @@ -78,6 +78,17 @@ func resourceGroup() *schema.Resource { Optional: true, Default: 0, ValidateFunc: validation.IntInSlice([]int{0, 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1096, 1827, 2192, 2557, 2922, 3288, 3653}), + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + if d.HasChange("log_group_class") { + return false + } + if v, ok := d.GetOk("log_group_class"); ok { + if awstypes.LogGroupClass(v.(string)) == awstypes.LogGroupClassDelivery { + return true + } + } + return false + }, }, names.AttrSkipDestroy: { Type: schema.TypeBool, @@ -113,7 +124,7 @@ func resourceGroupCreate(ctx context.Context, d *schema.ResourceData, meta any) d.SetId(name) - if v, ok := d.GetOk("retention_in_days"); ok { + if v, ok := d.GetOk("retention_in_days"); ok && input.LogGroupClass != awstypes.LogGroupClassDelivery { input := &cloudwatchlogs.PutRetentionPolicyInput{ LogGroupName: aws.String(d.Id()), RetentionInDays: aws.Int32(int32(v.(int))), diff --git a/internal/service/logs/group_test.go b/internal/service/logs/group_test.go index 807f4590c84c..e7b5c76ce95d 100644 --- a/internal/service/logs/group_test.go +++ b/internal/service/logs/group_test.go @@ -331,6 +331,67 @@ func TestAccLogsGroup_skipDestroyInconsistentPlan(t *testing.T) { }) } +// Test whether the log group is successfully created with the DELIVERY log group class when retention_in_days is set. +// Even if retention_in_days is changed in the configuration, the diff should be suppressed and the plan should be empty. +func TestAccLogsGroup_logGroupClassDELIVERY1(t *testing.T) { + ctx := acctest.Context(t) + var v types.LogGroup + rName := acctest.RandomWithPrefix(t, acctest.ResourcePrefix) + resourceName := "aws_cloudwatch_log_group.test" + + acctest.ParallelTest(ctx, t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.LogsServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckLogGroupDestroy(ctx, t), + Steps: []resource.TestStep{ + { + Config: testAccGroupConfig_logGroupClassDEVIVERYWithRetentionInDays(rName, 30), + Check: resource.ComposeTestCheckFunc( + testAccCheckLogGroupExists(ctx, t, resourceName, &v), + // AWS API forces retention_in_days to 2 for DELIVERY log group class + resource.TestCheckResourceAttr(resourceName, "retention_in_days", "2"), + ), + }, + { + Config: testAccGroupConfig_logGroupClassDEVIVERYWithRetentionInDays(rName, 60), + ExpectNonEmptyPlan: false, + PlanOnly: true, + }, + { + Config: testAccGroupConfig_logGroupClassDEVIVERYWithoutRetentionInDays(rName), + ExpectNonEmptyPlan: false, + PlanOnly: true, + }, + }, + }) +} + +// Test whether the log group is successfully created with the DELIVERY log group class when retention_in_days is not set. +func TestAccLogsGroup_logGroupClassDELIVERY2(t *testing.T) { + ctx := acctest.Context(t) + var v types.LogGroup + rName := acctest.RandomWithPrefix(t, acctest.ResourcePrefix) + resourceName := "aws_cloudwatch_log_group.test" + + acctest.ParallelTest(ctx, t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.LogsServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckLogGroupDestroy(ctx, t), + Steps: []resource.TestStep{ + { + Config: testAccGroupConfig_logGroupClassDEVIVERYWithoutRetentionInDays(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckLogGroupExists(ctx, t, resourceName, &v), + // AWS API forces retention_in_days to 2 for DELIVERY log group class + resource.TestCheckResourceAttr(resourceName, "retention_in_days", "2"), + ), + }, + }, + }) +} + func testAccCheckLogGroupExists(ctx context.Context, t *testing.T, n string, v *types.LogGroup) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -483,3 +544,22 @@ resource "aws_cloudwatch_log_group" "test" { } `, rName) } + +func testAccGroupConfig_logGroupClassDEVIVERYWithRetentionInDays(rName string, retentionInDays int) string { + return fmt.Sprintf(` +resource "aws_cloudwatch_log_group" "test" { + name = %[1]q + log_group_class = "DELIVERY" + retention_in_days = %[2]d +} +`, rName, retentionInDays) +} + +func testAccGroupConfig_logGroupClassDEVIVERYWithoutRetentionInDays(rName string) string { + return fmt.Sprintf(` +resource "aws_cloudwatch_log_group" "test" { + name = %[1]q + log_group_class = "DELIVERY" +} +`, rName) +} diff --git a/website/docs/r/cloudwatch_log_group.html.markdown b/website/docs/r/cloudwatch_log_group.html.markdown index 127be0a1ae42..9e0f92cc91ae 100644 --- a/website/docs/r/cloudwatch_log_group.html.markdown +++ b/website/docs/r/cloudwatch_log_group.html.markdown @@ -30,10 +30,10 @@ This resource supports the following arguments: * `name` - (Optional, Forces new resource) The name of the log group. If omitted, Terraform will assign a random, unique name. * `name_prefix` - (Optional, Forces new resource) Creates a unique name beginning with the specified prefix. Conflicts with `name`. * `skip_destroy` - (Optional) Set to true if you do not wish the log group (and any logs it may contain) to be deleted at destroy time, and instead just remove the log group from the Terraform state. -* `log_group_class` - (Optional) Specified the log class of the log group. Possible values are: `STANDARD` or `INFREQUENT_ACCESS`. +* `log_group_class` - (Optional) Specified the log class of the log group. Possible values are: `STANDARD`, `INFREQUENT_ACCESS`, or `DELIVERY`. * `retention_in_days` - (Optional) Specifies the number of days you want to retain log events in the specified log group. Possible values are: 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1096, 1827, 2192, 2557, 2922, 3288, 3653, and 0. - If you select 0, the events in the log group are always retained and never expire. + If you select 0, the events in the log group are always retained and never expire. If `log_group_class` is set to `DELIVERY`, this argument is ignored and `retention_in_days` is forcibly set to 2. * `kms_key_id` - (Optional) The ARN of the KMS Key to use when encrypting log data. Please note, after the AWS KMS CMK is disassociated from the log group, AWS CloudWatch Logs stops encrypting newly ingested data for the log group. All previously ingested data remains encrypted, and AWS CloudWatch Logs requires permissions for the CMK whenever the encrypted data is requested.