Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_ecs_task_definition: prevent spurious environment variable diffs #11463

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions aws/ecs_task_definition_equivalency.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func EcsContainerDefinitionsAreEquivalent(def1, def2 string, isAWSVPC bool) (boo
type containerDefinitions []*ecs.ContainerDefinition

func (cd containerDefinitions) Reduce(isAWSVPC bool) error {
// Deal with fields which may be re-ordered in the API
bflad marked this conversation as resolved.
Show resolved Hide resolved
cd.OrderEnvironmentVariables()

for i, def := range cd {
// Deal with special fields which have defaults
if def.Cpu != nil && *def.Cpu == 0 {
Expand All @@ -76,11 +79,6 @@ func (cd containerDefinitions) Reduce(isAWSVPC bool) error {
}
}

// Deal with fields which may be re-ordered in the API
sort.Slice(def.Environment, func(i, j int) bool {
return aws.StringValue(def.Environment[i].Name) < aws.StringValue(def.Environment[j].Name)
})

// Create a mutable copy
defCopy, err := copystructure.Copy(def)
if err != nil {
Expand All @@ -103,3 +101,11 @@ func (cd containerDefinitions) Reduce(isAWSVPC bool) error {
}
return nil
}

func (cd containerDefinitions) OrderEnvironmentVariables() {
for _, def := range cd {
sort.Slice(def.Environment, func(i, j int) bool {
return aws.StringValue(def.Environment[i].Name) < aws.StringValue(def.Environment[j].Name)
})
}
}
13 changes: 12 additions & 1 deletion aws/resource_aws_ecs_task_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ func resourceAwsEcsTaskDefinition() *schema.Resource {
Required: true,
ForceNew: true,
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
// Sort the lists of environment variables as they are serialized to state, so we won't get
// spurious reorderings in plans (diff is suppressed if the environment variables haven't changed,
// but they still show in the plan if some other property changes).
orderedCDs, _ := expandEcsContainerDefinitions(v.(string))
containerDefinitions(orderedCDs).OrderEnvironmentVariables()
unnormalizedJson, _ := flattenEcsContainerDefinitions(orderedCDs)
json, _ := structure.NormalizeJsonString(unnormalizedJson)
return json
},
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
Expand Down Expand Up @@ -477,6 +483,11 @@ func resourceAwsEcsTaskDefinitionRead(d *schema.ResourceData, meta interface{})
d.Set("family", taskDefinition.Family)
d.Set("revision", taskDefinition.Revision)

// Sort the lists of environment variables as they come in, so we won't get spurious reorderings in plans
// (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()

defs, err := flattenEcsContainerDefinitions(taskDefinition.ContainerDefinitions)
if err != nil {
return err
Expand Down