Skip to content

Commit

Permalink
Merge pull request #35792 from ilkansh8/ecs-task-definition-secret-or…
Browse files Browse the repository at this point in the history
…dering

resource/aws_ecs_task_definition: prevent spurious secret diffs
  • Loading branch information
ewbankkit authored Feb 13, 2024
2 parents bbea073 + 300ca52 commit 811c087
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/35792.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_ecs_task_definition: Fix perpetual `container_definitions` diffs when `Secrets` are ordered differently
```
2 changes: 2 additions & 0 deletions internal/service/ecs/task_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func ResourceTaskDefinition() *schema.Resource {
// but they still show in the plan if some other property changes).
orderedCDs, _ := expandContainerDefinitions(v.(string))
containerDefinitions(orderedCDs).OrderEnvironmentVariables()
containerDefinitions(orderedCDs).OrderSecrets()
unnormalizedJson, _ := flattenContainerDefinitions(orderedCDs)
json, _ := structure.NormalizeJsonString(unnormalizedJson)
return json
Expand Down Expand Up @@ -613,6 +614,7 @@ func resourceTaskDefinitionRead(ctx context.Context, d *schema.ResourceData, met
// (diff is suppressed if the environment variables haven't changed, but they still show in the plan if
// some other property changes).
containerDefinitions(taskDefinition.ContainerDefinitions).OrderEnvironmentVariables()
containerDefinitions(taskDefinition.ContainerDefinitions).OrderSecrets()

defs, err := flattenContainerDefinitions(taskDefinition.ContainerDefinitions)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions internal/service/ecs/task_definition_equivalency.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type containerDefinitions []*ecs.ContainerDefinition
func (cd containerDefinitions) Reduce(isAWSVPC bool) error {
// Deal with fields which may be re-ordered in the API
cd.OrderEnvironmentVariables()
cd.OrderSecrets()

for i, def := range cd {
// Deal with special fields which have defaults
Expand Down Expand Up @@ -112,3 +113,11 @@ func (cd containerDefinitions) OrderEnvironmentVariables() {
})
}
}

func (cd containerDefinitions) OrderSecrets() {
for _, def := range cd {
sort.Slice(def.Secrets, func(i, j int) bool {
return aws.StringValue(def.Secrets[i].Name) < aws.StringValue(def.Secrets[j].Name)
})
}
}

0 comments on commit 811c087

Please sign in to comment.