From bca801f608275dae0729e5ab31a83a6ef3cfda3a Mon Sep 17 00:00:00 2001 From: Daniel Flores Date: Thu, 17 Oct 2024 20:57:34 +0000 Subject: [PATCH 01/10] Add availability_zone_rebalancing to aws_ecs_service --- .changelog/TBD.txt | 3 + internal/service/ecs/service.go | 22 +++++ internal/service/ecs/service_test.go | 100 +++++++++++++++++++++++ website/docs/r/ecs_service.html.markdown | 1 + 4 files changed, 126 insertions(+) create mode 100644 .changelog/TBD.txt diff --git a/.changelog/TBD.txt b/.changelog/TBD.txt new file mode 100644 index 000000000000..3780ddb23021 --- /dev/null +++ b/.changelog/TBD.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_ecs_service: Add availability_zone_rebalancing argument +``` \ No newline at end of file diff --git a/internal/service/ecs/service.go b/internal/service/ecs/service.go index 67cbdf69861c..18ba86a3a20c 100644 --- a/internal/service/ecs/service.go +++ b/internal/service/ecs/service.go @@ -553,6 +553,17 @@ func resourceService() *schema.Resource { }, }, }, + "availability_zone_rebalancing": { + Type: schema.TypeString, + Optional: true, + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + if new == "" { + return true + } + return old == new + }, + ValidateDiagFunc: enum.Validate[awstypes.AvailabilityZoneRebalancing](), + }, names.AttrCapacityProviderStrategy: { Type: schema.TypeSet, Optional: true, @@ -1149,6 +1160,10 @@ func resourceServiceCreate(ctx context.Context, d *schema.ResourceData, meta int input.DeploymentConfiguration.Alarms = expandAlarms(v.([]interface{})[0].(map[string]interface{})) } + if v, ok := d.GetOk("availability_zone_rebalancing"); ok { + input.AvailabilityZoneRebalancing = awstypes.AvailabilityZoneRebalancing(v.(string)) + } + if v, ok := d.GetOk("cluster"); ok { input.Cluster = aws.String(v.(string)) } @@ -1288,6 +1303,7 @@ func resourceServiceRead(ctx context.Context, d *schema.ResourceData, meta inter } d.SetId(aws.ToString(service.ServiceArn)) + d.Set("availability_zone_rebalancing", service.AvailabilityZoneRebalancing) if err := d.Set(names.AttrCapacityProviderStrategy, flattenCapacityProviderStrategyItems(service.CapacityProviderStrategy)); err != nil { return sdkdiag.AppendErrorf(diags, "setting capacity_provider_strategy: %s", err) } @@ -1397,6 +1413,12 @@ func resourceServiceUpdate(ctx context.Context, d *schema.ResourceData, meta int } } + if d.HasChange("availability_zone_rebalancing") { + if v, ok := d.GetOk("availability_zone_rebalancing"); ok { + input.AvailabilityZoneRebalancing = awstypes.AvailabilityZoneRebalancing(v.(string)) + } + } + if d.HasChange(names.AttrCapacityProviderStrategy) { input.CapacityProviderStrategy = expandCapacityProviderStrategyItems(d.Get(names.AttrCapacityProviderStrategy).(*schema.Set)) } diff --git a/internal/service/ecs/service_test.go b/internal/service/ecs/service_test.go index ac630ade3eb8..a4e65009e596 100644 --- a/internal/service/ecs/service_test.go +++ b/internal/service/ecs/service_test.go @@ -103,6 +103,7 @@ func TestAccECSService_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "alarms.#", "0"), resource.TestCheckResourceAttr(resourceName, "service_registries.#", "0"), resource.TestCheckResourceAttr(resourceName, "scheduling_strategy", "REPLICA"), + resource.TestCheckResourceAttr(resourceName, "availability_zone_rebalancing", "DISABLED"), ), }, @@ -113,6 +114,7 @@ func TestAccECSService_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "alarms.#", "0"), resource.TestCheckResourceAttr(resourceName, "service_registries.#", "0"), resource.TestCheckResourceAttr(resourceName, "scheduling_strategy", "REPLICA"), + resource.TestCheckResourceAttr(resourceName, "availability_zone_rebalancing", "DISABLED"), ), }, }, @@ -1732,6 +1734,43 @@ func TestAccECSService_executeCommand(t *testing.T) { }) } +func TestAccECSService_AvailabilityZoneRebalancing(t *testing.T) { + ctx := acctest.Context(t) + var service awstypes.Service + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_ecs_service.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.ECSServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckServiceDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccServiceConfig_availabilityZoneRebalancing(rName, "ENABLED"), + Check: resource.ComposeTestCheckFunc( + testAccCheckServiceExists(ctx, resourceName, &service), + resource.TestCheckResourceAttr(resourceName, "availability_zone_rebalancing", string(awstypes.AvailabilityZoneRebalancingEnabled)), + ), + }, + { + Config: testAccServiceConfig_availabilityZoneRebalancing_nullUpdate(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckServiceExists(ctx, resourceName, &service), + resource.TestCheckResourceAttr(resourceName, "availability_zone_rebalancing", string(awstypes.AvailabilityZoneRebalancingEnabled)), + ), + }, + { + Config: testAccServiceConfig_availabilityZoneRebalancing(rName, "DISABLED"), + Check: resource.ComposeTestCheckFunc( + testAccCheckServiceExists(ctx, resourceName, &service), + resource.TestCheckResourceAttr(resourceName, "availability_zone_rebalancing", string(awstypes.AvailabilityZoneRebalancingDisabled)), + ), + }, + }, + }) +} + func testAccCheckServiceDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { conn := acctest.Provider.Meta().(*conns.AWSClient).ECSClient(ctx) @@ -5050,3 +5089,64 @@ resource "aws_ecs_service" "test" { } `, rName) } + +func testAccServiceConfig_availabilityZoneRebalancing(rName string, serviceRebalancing string) string { + return fmt.Sprintf(` + resource "aws_ecs_service" "test" { + name = %[1]q + cluster = aws_ecs_cluster.test.id + task_definition = aws_ecs_task_definition.test.arn + availability_zone_rebalancing = %[2]q + } + + resource "aws_ecs_cluster" "test" { + name = %[1]q + } + + resource "aws_ecs_task_definition" "test" { + family = %[1]q + + container_definitions = < Date: Tue, 22 Oct 2024 21:21:58 +0000 Subject: [PATCH 02/10] Set avilability_zone_rebalancing default to DISABLED --- internal/service/ecs/service.go | 11 +++-------- internal/service/ecs/service_test.go | 2 +- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/internal/service/ecs/service.go b/internal/service/ecs/service.go index 18ba86a3a20c..34c1912cb9ef 100644 --- a/internal/service/ecs/service.go +++ b/internal/service/ecs/service.go @@ -554,14 +554,9 @@ func resourceService() *schema.Resource { }, }, "availability_zone_rebalancing": { - Type: schema.TypeString, - Optional: true, - DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { - if new == "" { - return true - } - return old == new - }, + Type: schema.TypeString, + Optional: true, + Default: awstypes.AvailabilityZoneRebalancingDisabled, ValidateDiagFunc: enum.Validate[awstypes.AvailabilityZoneRebalancing](), }, names.AttrCapacityProviderStrategy: { diff --git a/internal/service/ecs/service_test.go b/internal/service/ecs/service_test.go index a4e65009e596..0d9d1fc79e05 100644 --- a/internal/service/ecs/service_test.go +++ b/internal/service/ecs/service_test.go @@ -1757,7 +1757,7 @@ func TestAccECSService_AvailabilityZoneRebalancing(t *testing.T) { Config: testAccServiceConfig_availabilityZoneRebalancing_nullUpdate(rName), Check: resource.ComposeTestCheckFunc( testAccCheckServiceExists(ctx, resourceName, &service), - resource.TestCheckResourceAttr(resourceName, "availability_zone_rebalancing", string(awstypes.AvailabilityZoneRebalancingEnabled)), + resource.TestCheckResourceAttr(resourceName, "availability_zone_rebalancing", string(awstypes.AvailabilityZoneRebalancingDisabled)), ), }, { From 4e0cd56facfaf9295bb2653460cd9a693b18ae79 Mon Sep 17 00:00:00 2001 From: Daniel Flores Date: Tue, 22 Oct 2024 21:36:31 +0000 Subject: [PATCH 03/10] nit: documentation update --- website/docs/r/ecs_service.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/ecs_service.html.markdown b/website/docs/r/ecs_service.html.markdown index 7cd4ce20c343..402a22916d41 100644 --- a/website/docs/r/ecs_service.html.markdown +++ b/website/docs/r/ecs_service.html.markdown @@ -127,7 +127,7 @@ The following arguments are required: The following arguments are optional: * `alarms` - (Optional) Information about the CloudWatch alarms. [See below](#alarms). -* `availability_zone_rebalancing` - TBD +* `availability_zone_rebalancing` - (Optional) ECS automatically redistributes tasks within a service across Availability Zones (AZs) to mitigate the risk of impaired application availability due to underlying infrastructure failures and task lifecycle activities. The valid values are `ENABLED` AND `DISABLED`. Defaults to `DISABLED`. * `capacity_provider_strategy` - (Optional) Capacity provider strategies to use for the service. Can be one or more. These can be updated without destroying and recreating the service only if `force_new_deployment = true` and not changing from 0 `capacity_provider_strategy` blocks to greater than 0, or vice versa. See below. Conflicts with `launch_type`. * `cluster` - (Optional) ARN of an ECS cluster. * `deployment_circuit_breaker` - (Optional) Configuration block for deployment circuit breaker. See below. From 859238ec758a1edfd7967c0585a9b21ffc74ce2b Mon Sep 17 00:00:00 2001 From: Daniel Flores Date: Wed, 23 Oct 2024 14:25:43 +0000 Subject: [PATCH 04/10] nit: Add DISABLED -> ENABLED test step --- internal/service/ecs/service_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/service/ecs/service_test.go b/internal/service/ecs/service_test.go index 0d9d1fc79e05..5dea06db2380 100644 --- a/internal/service/ecs/service_test.go +++ b/internal/service/ecs/service_test.go @@ -1767,6 +1767,13 @@ func TestAccECSService_AvailabilityZoneRebalancing(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "availability_zone_rebalancing", string(awstypes.AvailabilityZoneRebalancingDisabled)), ), }, + { + Config: testAccServiceConfig_availabilityZoneRebalancing(rName, "ENABLED"), + Check: resource.ComposeTestCheckFunc( + testAccCheckServiceExists(ctx, resourceName, &service), + resource.TestCheckResourceAttr(resourceName, "availability_zone_rebalancing", string(awstypes.AvailabilityZoneRebalancingEnabled)), + ), + }, }, }) } From e4ea7f97f56b295dd1dfa9ac44419618b29b2945 Mon Sep 17 00:00:00 2001 From: Daniel Flores Date: Wed, 23 Oct 2024 14:29:58 +0000 Subject: [PATCH 05/10] nit: Documentation proofreading --- website/docs/r/ecs_service.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/ecs_service.html.markdown b/website/docs/r/ecs_service.html.markdown index 402a22916d41..3e94ff12742e 100644 --- a/website/docs/r/ecs_service.html.markdown +++ b/website/docs/r/ecs_service.html.markdown @@ -127,7 +127,7 @@ The following arguments are required: The following arguments are optional: * `alarms` - (Optional) Information about the CloudWatch alarms. [See below](#alarms). -* `availability_zone_rebalancing` - (Optional) ECS automatically redistributes tasks within a service across Availability Zones (AZs) to mitigate the risk of impaired application availability due to underlying infrastructure failures and task lifecycle activities. The valid values are `ENABLED` AND `DISABLED`. Defaults to `DISABLED`. +* `availability_zone_rebalancing` - (Optional) ECS automatically redistributes tasks within a service across Availability Zones (AZs) to mitigate the risk of impaired application availability due to underlying infrastructure failures and task lifecycle activities. The valid values are `ENABLED` and `DISABLED`. Defaults to `DISABLED`. * `capacity_provider_strategy` - (Optional) Capacity provider strategies to use for the service. Can be one or more. These can be updated without destroying and recreating the service only if `force_new_deployment = true` and not changing from 0 `capacity_provider_strategy` blocks to greater than 0, or vice versa. See below. Conflicts with `launch_type`. * `cluster` - (Optional) ARN of an ECS cluster. * `deployment_circuit_breaker` - (Optional) Configuration block for deployment circuit breaker. See below. From 6fb36814ce94792cde64ad6993f0f9c4fc0768b5 Mon Sep 17 00:00:00 2001 From: Daniel Flores Date: Wed, 23 Oct 2024 20:33:17 +0000 Subject: [PATCH 06/10] nit: Formatting tests and changelog --- .changelog/TBD.txt | 2 +- internal/service/ecs/service_test.go | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.changelog/TBD.txt b/.changelog/TBD.txt index 3780ddb23021..4eccf09939f9 100644 --- a/.changelog/TBD.txt +++ b/.changelog/TBD.txt @@ -1,3 +1,3 @@ ```release-note:enhancement -resource/aws_ecs_service: Add availability_zone_rebalancing argument +resource/aws_ecs_service: Add `availability_zone_rebalancing` argument ``` \ No newline at end of file diff --git a/internal/service/ecs/service_test.go b/internal/service/ecs/service_test.go index 5dea06db2380..479157683919 100644 --- a/internal/service/ecs/service_test.go +++ b/internal/service/ecs/service_test.go @@ -5099,13 +5099,6 @@ resource "aws_ecs_service" "test" { func testAccServiceConfig_availabilityZoneRebalancing(rName string, serviceRebalancing string) string { return fmt.Sprintf(` - resource "aws_ecs_service" "test" { - name = %[1]q - cluster = aws_ecs_cluster.test.id - task_definition = aws_ecs_task_definition.test.arn - availability_zone_rebalancing = %[2]q - } - resource "aws_ecs_cluster" "test" { name = %[1]q } @@ -5125,17 +5118,18 @@ func testAccServiceConfig_availabilityZoneRebalancing(rName string, serviceRebal ] DEFINITION } - `, rName, serviceRebalancing) -} -func testAccServiceConfig_availabilityZoneRebalancing_nullUpdate(rName string) string { - return fmt.Sprintf(` resource "aws_ecs_service" "test" { name = %[1]q cluster = aws_ecs_cluster.test.id task_definition = aws_ecs_task_definition.test.arn + availability_zone_rebalancing = %[2]q } + `, rName, serviceRebalancing) +} +func testAccServiceConfig_availabilityZoneRebalancing_nullUpdate(rName string) string { + return fmt.Sprintf(` resource "aws_ecs_cluster" "test" { name = %[1]q } @@ -5155,5 +5149,11 @@ func testAccServiceConfig_availabilityZoneRebalancing_nullUpdate(rName string) s ] DEFINITION } + + resource "aws_ecs_service" "test" { + name = %[1]q + cluster = aws_ecs_cluster.test.id + task_definition = aws_ecs_task_definition.test.arn + } `, rName) } From 3c81a74ae6f93a05ec4cabbeba52c941983aeae2 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 20 Nov 2024 16:48:37 -0600 Subject: [PATCH 07/10] chore: linter --- internal/service/ecs/service_test.go | 60 ++++++++++++++-------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/internal/service/ecs/service_test.go b/internal/service/ecs/service_test.go index 479157683919..eb5204eb39a6 100644 --- a/internal/service/ecs/service_test.go +++ b/internal/service/ecs/service_test.go @@ -5099,14 +5099,14 @@ resource "aws_ecs_service" "test" { func testAccServiceConfig_availabilityZoneRebalancing(rName string, serviceRebalancing string) string { return fmt.Sprintf(` - resource "aws_ecs_cluster" "test" { - name = %[1]q - } - - resource "aws_ecs_task_definition" "test" { - family = %[1]q - - container_definitions = < Date: Wed, 20 Nov 2024 16:49:53 -0600 Subject: [PATCH 08/10] rename CHANGELOG file --- .changelog/{TBD.txt => 40225.txt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .changelog/{TBD.txt => 40225.txt} (100%) diff --git a/.changelog/TBD.txt b/.changelog/40225.txt similarity index 100% rename from .changelog/TBD.txt rename to .changelog/40225.txt From a93bcf68881079ea0add13478ac5b299e6a9426d Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 20 Nov 2024 17:09:18 -0600 Subject: [PATCH 09/10] aws_ecs_service: add availability_zone_rebalancing attribute to datasource --- internal/service/ecs/service_data_source.go | 5 +++++ internal/service/ecs/service_data_source_test.go | 1 + 2 files changed, 6 insertions(+) diff --git a/internal/service/ecs/service_data_source.go b/internal/service/ecs/service_data_source.go index 99355fc437e8..660082ed6595 100644 --- a/internal/service/ecs/service_data_source.go +++ b/internal/service/ecs/service_data_source.go @@ -27,6 +27,10 @@ func dataSourceService() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "availability_zone_rebalancing": { + Type: schema.TypeString, + Computed: true, + }, "cluster_arn": { Type: schema.TypeString, Required: true, @@ -69,6 +73,7 @@ func dataSourceServiceRead(ctx context.Context, d *schema.ResourceData, meta int arn := aws.ToString(service.ServiceArn) d.SetId(arn) d.Set(names.AttrARN, arn) + d.Set("availability_zone_rebalancing", service.AvailabilityZoneRebalancing) d.Set("cluster_arn", service.ClusterArn) d.Set("desired_count", service.DesiredCount) d.Set("launch_type", service.LaunchType) diff --git a/internal/service/ecs/service_data_source_test.go b/internal/service/ecs/service_data_source_test.go index 813a9de3dd76..7c25c05ec2af 100644 --- a/internal/service/ecs/service_data_source_test.go +++ b/internal/service/ecs/service_data_source_test.go @@ -28,6 +28,7 @@ func TestAccECSServiceDataSource_basic(t *testing.T) { Config: testAccServiceDataSourceConfig_basic(rName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(resourceName, names.AttrID, dataSourceName, names.AttrARN), + resource.TestCheckResourceAttrPair(resourceName, "availability_zone_rebalancing", dataSourceName, "availability_zone_rebalancing"), resource.TestCheckResourceAttrPair(resourceName, "desired_count", dataSourceName, "desired_count"), resource.TestCheckResourceAttrPair(resourceName, "launch_type", dataSourceName, "launch_type"), resource.TestCheckResourceAttrPair(resourceName, "scheduling_strategy", dataSourceName, "scheduling_strategy"), From 920bc66cf8cd743e07a30b4b8d6130d0c2b54a71 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Wed, 20 Nov 2024 17:10:29 -0600 Subject: [PATCH 10/10] tweak CHANGELOG entry --- .changelog/40225.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.changelog/40225.txt b/.changelog/40225.txt index 4eccf09939f9..d73833fa0aea 100644 --- a/.changelog/40225.txt +++ b/.changelog/40225.txt @@ -1,3 +1,7 @@ ```release-note:enhancement -resource/aws_ecs_service: Add `availability_zone_rebalancing` argument +resource/aws_ecs_service: Add `availability_zone_rebalancing` attribute +``` + +```release-note:enhancement +data-source/aws_ecs_service: Add `availability_zone_rebalancing` attribute ``` \ No newline at end of file