Skip to content

Commit

Permalink
Merge pull request #27263 from mtt88/b-aws_ecs_task_definition-null-c…
Browse files Browse the repository at this point in the history
…ontainer-definition

resource/aws_ecs_task_definition - prevent panic on null container definition
  • Loading branch information
ewbankkit authored Oct 17, 2022
2 parents 8f48c22 + 012d5b2 commit 7e14921
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/27263.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_ecs_task_definition: Prevent panic when supplying a `null` value in `container_definitions`
```
6 changes: 6 additions & 0 deletions internal/service/ecs/task_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,12 @@ func expandContainerDefinitions(rawDefinitions string) ([]*ecs.ContainerDefiniti
return nil, fmt.Errorf("Error decoding JSON: %s", err)
}

for i, c := range definitions {
if c == nil {
return nil, fmt.Errorf("invalid container definition supplied at index (%d)", i)
}
}

return definitions, nil
}

Expand Down
46 changes: 46 additions & 0 deletions internal/service/ecs/task_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,23 @@ func TestAccECSTaskDefinition_inferenceAccelerator(t *testing.T) {
})
}

func TestAccECSTaskDefinition_invalidContainerDefinition(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, ecs.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckTaskDefinitionDestroy,
Steps: []resource.TestStep{
{
Config: testAccTaskDefinitionConfig_invalidContainerDefinition(rName),
ExpectError: regexp.MustCompile(`invalid container definition supplied at index \(1\)`),
},
},
})
}

func testAccTaskDefinitionConfig_proxyConfiguration(rName string, containerName string, proxyType string,
ignoredUid string, ignoredGid string, appPorts string, proxyIngressPort string, proxyEgressPort string,
egressIgnoredPorts string, egressIgnoredIPs string) string {
Expand Down Expand Up @@ -2616,3 +2633,32 @@ resource "aws_fsx_windows_file_system" "test" {
}
`)
}

func testAccTaskDefinitionConfig_invalidContainerDefinition(rName string) string {
return fmt.Sprintf(`
resource "aws_ecs_task_definition" "test" {
family = %[1]q
container_definitions = <<TASK_DEFINITION
[
{
"cpu": 10,
"command": ["sleep", "10"],
"entryPoint": ["/"],
"essential": true,
"image": "mongodb",
"memory": 128,
"name": "mongodb",
"portMappings": [
{
"containerPort": 28017,
"hostPort": 28017
}
]
},
null
]
TASK_DEFINITION
}
`, rName)
}

0 comments on commit 7e14921

Please sign in to comment.