Skip to content

Commit

Permalink
Merge pull request #19694 from DrFaust92/r/ecs_ephemral_storage
Browse files Browse the repository at this point in the history
r/ecs_task_definition - add support for `ephemeral_storage`
  • Loading branch information
ewbankkit authored Jun 8, 2021
2 parents debac80 + 6c3eb2e commit 12d5302
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/19694.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_ecs_task_definition: Add support for `ephemeral_storage`.
```
45 changes: 44 additions & 1 deletion aws/resource_aws_ecs_task_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,22 @@ func resourceAwsEcsTaskDefinition() *schema.Resource {
},
ValidateFunc: validateAwsEcsTaskDefinitionContainerDefinitions,
},

"ephemeral_storage": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"size_in_gib": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
ValidateFunc: validation.IntBetween(21, 200),
},
},
},
},
"task_role_arn": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -472,6 +487,10 @@ func resourceAwsEcsTaskDefinitionCreate(d *schema.ResourceData, meta interface{}
input.ProxyConfiguration = expandEcsTaskDefinitionProxyConfiguration(proxyConfigs)
}

if v, ok := d.GetOk("ephemeral_storage"); ok && len(v.([]interface{})) > 0 {
input.EphemeralStorage = expandEcsTaskDefinitionEphemeralStorage(v.([]interface{}))
}

log.Printf("[DEBUG] Registering ECS task definition: %s", input)
out, err := conn.RegisterTaskDefinition(&input)
if err != nil {
Expand Down Expand Up @@ -571,6 +590,9 @@ func resourceAwsEcsTaskDefinitionRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("error setting proxy_configuration: %w", err)
}

if err := d.Set("ephemeral_storage", flattenEcsTaskDefinitionEphemeralStorage(taskDefinition.EphemeralStorage)); err != nil {
return fmt.Errorf("error setting ephemeral_storage: %w", err)
}
return nil
}

Expand Down Expand Up @@ -1066,3 +1088,24 @@ func expandEcsContainerDefinitions(rawDefinitions string) ([]*ecs.ContainerDefin

return definitions, nil
}

func expandEcsTaskDefinitionEphemeralStorage(config []interface{}) *ecs.EphemeralStorage {
configMap := config[0].(map[string]interface{})

es := &ecs.EphemeralStorage{
SizeInGiB: aws.Int64(int64(configMap["size_in_gib"].(int))),
}

return es
}

func flattenEcsTaskDefinitionEphemeralStorage(pc *ecs.EphemeralStorage) []map[string]interface{} {
if pc == nil {
return nil
}

m := make(map[string]interface{})
m["size_in_gib"] = aws.Int64Value(pc.SizeInGiB)

return []map[string]interface{}{m}
}
63 changes: 63 additions & 0 deletions aws/resource_aws_ecs_task_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,38 @@ func TestAccAWSEcsTaskDefinition_Fargate(t *testing.T) {
})
}

func TestAccAWSEcsTaskDefinition_Fargate_ephemeralStorage(t *testing.T) {
var conf ecs.TaskDefinition

tdName := acctest.RandomWithPrefix("tf-acc-td-fargate")
resourceName := "aws_ecs_task_definition.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, ecs.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcsTaskDefinitionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEcsTaskDefinitionFargateEphemeralStorage(tdName, `[{"protocol": "tcp", "containerPort": 8000}]`),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsTaskDefinitionExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "requires_compatibilities.#", "1"),
resource.TestCheckResourceAttr(resourceName, "cpu", "256"),
resource.TestCheckResourceAttr(resourceName, "memory", "512"),
resource.TestCheckResourceAttr(resourceName, "ephemeral_storage.#", "1"),
resource.TestCheckResourceAttr(resourceName, "ephemeral_storage.0.size_in_gib", "30"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateIdFunc: testAccAWSEcsTaskDefinitionImportStateIdFunc(resourceName),
ImportStateVerify: true,
},
},
})
}
func TestAccAWSEcsTaskDefinition_ExecutionRole(t *testing.T) {
var conf ecs.TaskDefinition

Expand Down Expand Up @@ -1401,6 +1433,37 @@ TASK_DEFINITION
`, tdName, portMappings)
}

func testAccAWSEcsTaskDefinitionFargateEphemeralStorage(tdName, portMappings string) string {
return fmt.Sprintf(`
resource "aws_ecs_task_definition" "test" {
family = "%s"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = "256"
memory = "512"
ephemeral_storage {
size_in_gib = 30
}
container_definitions = <<TASK_DEFINITION
[
{
"name": "sleep",
"image": "busybox",
"cpu": 10,
"command": ["sleep","360"],
"memory": 10,
"essential": true,
"portMappings": %s
}
]
TASK_DEFINITION
}
`, tdName, portMappings)
}

func testAccAWSEcsTaskDefinitionExecutionRole(roleName, policyName, tdName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "test" {
Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/ecs_task_definition.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ The following arguments are optional:
* `pid_mode` - (Optional) Process namespace to use for the containers in the task. The valid values are `host` and `task`.
* `placement_constraints` - (Optional) Configuration block for rules that are taken into consideration during task placement. Maximum number of `placement_constraints` is `10`. [Detailed below](#placement_constraints).
* `proxy_configuration` - (Optional) Configuration block for the App Mesh proxy. [Detailed below.](#proxy_configuration)
* `ephemeral_storage` - (Optional) The amount of ephemeral storage to allocate for the task. This parameter is used to expand the total amount of ephemeral storage available, beyond the default amount, for tasks hosted on AWS Fargate. See [Ephemeral Storage](#ephemeral_storage).
* `requires_compatibilities` - (Optional) Set of launch types required by the task. The valid values are `EC2` and `FARGATE`.
* `tags` - (Optional) Key-value map of resource tags. If configured with a provider [`default_tags` configuration block](https://www.terraform.io/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.
* `task_role_arn` - (Optional) ARN of IAM role that allows your Amazon ECS container task to make calls to other AWS services.
Expand Down Expand Up @@ -280,6 +281,10 @@ For more information, see [Specifying an FSX Windows File Server volume in your
* `properties` - (Required) Set of network configuration parameters to provide the Container Network Interface (CNI) plugin, specified a key-value mapping.
* `type` - (Optional) Proxy type. The default value is `APPMESH`. The only supported value is `APPMESH`.

### ephemeral_storage

* `size_in_gib` - (Required) The total amount, in GiB, of ephemeral storage to set for the task. The minimum supported value is `21` GiB and the maximum supported value is `200` GiB.

### inference_accelerator

* `device_name` - (Required) Elastic Inference accelerator device name. The deviceName must also be referenced in a container definition as a ResourceRequirement.
Expand Down

0 comments on commit 12d5302

Please sign in to comment.