diff --git a/.changelog/38870.txt b/.changelog/38870.txt index 0d2d1b938fb6..3f5b57bf65f7 100644 --- a/.changelog/38870.txt +++ b/.changelog/38870.txt @@ -1,2 +1,3 @@ ```release-note:bug -resource/aws_ecs_task_definition: Remove `null`s from `container_definition` array fields \ No newline at end of file +resource/aws_ecs_task_definition: Remove `null`s from `container_definition` array fields +``` \ No newline at end of file diff --git a/.changelog/38872.txt b/.changelog/38872.txt new file mode 100644 index 000000000000..377f86506a50 --- /dev/null +++ b/.changelog/38872.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_ecs_task_definition: Fix perpetual `container_definitions` diffs on `healthCheck`'s default values +``` \ No newline at end of file diff --git a/internal/service/ecs/container_definitions.go b/internal/service/ecs/container_definitions.go index 7c71b88841da..18bcd4df83b3 100644 --- a/internal/service/ecs/container_definitions.go +++ b/internal/service/ecs/container_definitions.go @@ -54,11 +54,25 @@ func (cd containerDefinitions) reduce(isAWSVPC bool) { // Compact any sparse lists. cd.compactArrays() + // Deal with special fields which have defaults. + // See https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#container_definitions. for i, def := range cd { - // Deal with special fields which have defaults. if def.Essential == nil { cd[i].Essential = aws.Bool(true) } + + if hc := def.HealthCheck; hc != nil { + if hc.Interval == nil { + hc.Interval = aws.Int32(30) + } + if hc.Retries == nil { + hc.Retries = aws.Int32(3) + } + if hc.Timeout == nil { + hc.Timeout = aws.Int32(5) + } + } + for j, pm := range def.PortMappings { if pm.Protocol == awstypes.TransportProtocolTcp { cd[i].PortMappings[j].Protocol = "" diff --git a/internal/service/ecs/container_definitions_test.go b/internal/service/ecs/container_definitions_test.go index abd43d8de9cb..dbefbab1bed7 100644 --- a/internal/service/ecs/container_definitions_test.go +++ b/internal/service/ecs/container_definitions_test.go @@ -624,3 +624,76 @@ func TestContainerDefinitionsAreEquivalent_sparseArrays(t *testing.T) { t.Fatal("Expected definitions to be equal.") } } + +func TestContainerDefinitionsAreEquivalent_healthCheck(t *testing.T) { + t.Parallel() + + cfgRepresentation := ` +[ + { + "cpu": 512, + "environment": [], + "healthCheck": { + "command": [ + "CMD-SHELL", + "curl -f http://localhost:8080/health || exit 1" + ] + }, + "image": "nginx", + "memory": 2048, + "name": "nginx", + "startTimeout": 10, + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "foo-bar-e196c99", + "awslogs-region": "region-1", + "awslogs-stream-prefix": "nginx" + } + } + } +]` + + apiRepresentation := ` +[ + { + "cpu": 512, + "environment": [], + "essential": true, + "healthCheck": { + "command": [ + "CMD-SHELL", + "curl -f http://localhost:8080/health || exit 1" + ], + "interval": 30, + "retries": 3, + "timeout": 5 + }, + "image": "nginx", + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "foo-bar-e196c99", + "awslogs-region": "region-1", + "awslogs-stream-prefix": "nginx" + } + }, + "memory": 2048, + "mountPoints": [], + "name": "nginx", + "portMappings": [], + "startTimeout": 10, + "systemControls": [], + "volumesFrom": [] + } +] +` + + equal, err := containerDefinitionsAreEquivalent(cfgRepresentation, apiRepresentation, false) + if err != nil { + t.Fatal(err) + } + if !equal { + t.Fatal("Expected definitions to be equal.") + } +}