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

r/ecs - Add support for FARGATE Windows Containers and ARM based Containers #22016

Merged
merged 3 commits into from
Dec 7, 2021
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
3 changes: 3 additions & 0 deletions .changelog/22016.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/ecs: Add `runtime_platform` argument in support of Fargate for ECS Windows containers
```
77 changes: 77 additions & 0 deletions internal/service/ecs/task_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ func ResourceTaskDefinition() *schema.Resource {
ValidateFunc: validation.StringInSlice(ecs.NetworkMode_Values(), false),
},

"runtime_platform": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"operating_system_family": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(ecs.OSFamily_Values(), false),
},
"cpu_architecture": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(ecs.CPUArchitecture_Values(), false),
},
},
},
},

"volume": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -485,6 +508,11 @@ func resourceTaskDefinitionCreate(d *schema.ResourceData, meta interface{}) erro
input.RequiresCompatibilities = flex.ExpandStringSet(v.(*schema.Set))
}

runtimePlatformConfigs := d.Get("runtime_platform").([]interface{})
if len(runtimePlatformConfigs) > 0 && runtimePlatformConfigs[0] != nil {
input.RuntimePlatform = expandEcsTaskDefinitionRuntimePlatformConfiguration(runtimePlatformConfigs)
}

proxyConfigs := d.Get("proxy_configuration").([]interface{})
if len(proxyConfigs) > 0 {
input.ProxyConfiguration = expandEcsTaskDefinitionProxyConfiguration(proxyConfigs)
Expand Down Expand Up @@ -589,6 +617,10 @@ func resourceTaskDefinitionRead(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("error setting requires_compatibilities: %w", err)
}

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

if err := d.Set("proxy_configuration", flattenProxyConfiguration(taskDefinition.ProxyConfiguration)); err != nil {
return fmt.Errorf("error setting proxy_configuration: %w", err)
}
Expand All @@ -613,6 +645,32 @@ func flattenPlacementConstraints(pcs []*ecs.TaskDefinitionPlacementConstraint) [
return results
}

func flattenRuntimePlatform(rp *ecs.RuntimePlatform) []map[string]interface{} {
if rp == nil {
return nil
}

os := aws.StringValue(rp.OperatingSystemFamily)
cpu := aws.StringValue(rp.CpuArchitecture)

if os == "" && cpu == "" {
return nil
}

config := make(map[string]interface{})

if os != "" {
config["operating_system_family"] = os
}
if cpu != "" {
config["cpu_architecture"] = cpu
}

return []map[string]interface{}{
config,
}
}

func flattenProxyConfiguration(pc *ecs.ProxyConfiguration) []map[string]interface{} {
if pc == nil {
return nil
Expand Down Expand Up @@ -768,6 +826,25 @@ func expandEcsTaskDefinitionPlacementConstraints(constraints []interface{}) ([]*
return pc, nil
}

func expandEcsTaskDefinitionRuntimePlatformConfiguration(runtimePlatformConfig []interface{}) *ecs.RuntimePlatform {
config := runtimePlatformConfig[0]

configMap := config.(map[string]interface{})
ecsProxyConfig := &ecs.RuntimePlatform{}

os := configMap["operating_system_family"].(string)
if os != "" {
ecsProxyConfig.OperatingSystemFamily = aws.String(os)
}

osFamily := configMap["cpu_architecture"].(string)
if osFamily != "" {
ecsProxyConfig.CpuArchitecture = aws.String(osFamily)
}

return ecsProxyConfig
}

func expandEcsTaskDefinitionProxyConfiguration(proxyConfigs []interface{}) *ecs.ProxyConfiguration {
proxyConfig := proxyConfigs[0]
configMap := proxyConfig.(map[string]interface{})
Expand Down
167 changes: 167 additions & 0 deletions internal/service/ecs/task_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,104 @@ func TestAccECSTaskDefinition_withDockerVolumeMinimal(t *testing.T) {
})
}

func TestAccECSTaskDefinition_withRuntimePlatform(t *testing.T) {
var def ecs.TaskDefinition

tdName := sdkacctest.RandomWithPrefix("tf-acc-td-with-runtime-platform")
resourceName := "aws_ecs_task_definition.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, ecs.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckTaskDefinitionDestroy,
Steps: []resource.TestStep{
{
Config: testAccTaskDefinitionWithRuntimePlatformMinimalConfig(tdName),
Check: resource.ComposeTestCheckFunc(
testAccCheckTaskDefinitionExists(resourceName, &def),
ChrisMcKee marked this conversation as resolved.
Show resolved Hide resolved
resource.TestCheckResourceAttr(resourceName, "runtime_platform.#", "1"),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "runtime_platform.*", map[string]string{
"operating_system_family": "LINUX",
"cpu_architecture": "X86_64",
}),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateIdFunc: testAccTaskDefinitionImportStateIdFunc(resourceName),
ImportStateVerify: true,
},
},
})
}

func TestAccECSTaskDefinition_Fargate_withRuntimePlatform(t *testing.T) {
var def ecs.TaskDefinition

tdName := sdkacctest.RandomWithPrefix("tf-acc-td-with-runtime-platform")
resourceName := "aws_ecs_task_definition.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, ecs.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckTaskDefinitionDestroy,
Steps: []resource.TestStep{
{
Config: testAccFargateTaskDefinitionWithRuntimePlatformMinimalConfig(tdName, true, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckTaskDefinitionExists(resourceName, &def),
resource.TestCheckResourceAttr(resourceName, "runtime_platform.#", "1"),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "runtime_platform.*", map[string]string{
"operating_system_family": "WINDOWS_SERVER_2019_CORE",
"cpu_architecture": "X86_64",
}),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateIdFunc: testAccTaskDefinitionImportStateIdFunc(resourceName),
ImportStateVerify: true,
},
},
})
}

func TestAccECSTaskDefinition_Fargate_withRuntimePlatformWithoutArch(t *testing.T) {
var def ecs.TaskDefinition

tdName := sdkacctest.RandomWithPrefix("tf-acc-td-with-runtime-platform")
resourceName := "aws_ecs_task_definition.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, ecs.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckTaskDefinitionDestroy,
Steps: []resource.TestStep{
{
Config: testAccFargateTaskDefinitionWithRuntimePlatformMinimalConfig(tdName, false, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckTaskDefinitionExists(resourceName, &def),
ChrisMcKee marked this conversation as resolved.
Show resolved Hide resolved
resource.TestCheckResourceAttr(resourceName, "runtime_platform.#", "1"),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "runtime_platform.*", map[string]string{
"operating_system_family": "WINDOWS_SERVER_2019_CORE",
}),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateIdFunc: testAccTaskDefinitionImportStateIdFunc(resourceName),
ImportStateVerify: true,
},
},
})
}

func TestAccECSTaskDefinition_withEFSVolumeMinimal(t *testing.T) {
var def ecs.TaskDefinition

Expand Down Expand Up @@ -1587,6 +1685,75 @@ TASK_DEFINITION
`, tdName)
}

func testAccTaskDefinitionWithRuntimePlatformMinimalConfig(tdName string) string {
return fmt.Sprintf(`
resource "aws_ecs_task_definition" "test" {
family = %[1]q
container_definitions = <<TASK_DEFINITION
[
{
"name": "sleep",
"image": "busybox",
"cpu": 10,
"command": ["sleep","360"],
"memory": 10,
"essential": true
}
]
TASK_DEFINITION
runtime_platform {
operating_system_family = "LINUX"
cpu_architecture = "X86_64"
}
}
`, tdName)
}

func testAccFargateTaskDefinitionWithRuntimePlatformMinimalConfig(tdName string, architecture bool, osFamily bool) string {

var arch string
if architecture {
arch = `cpu_architecture = "X86_64"`
} else {
arch = ``
}

var os string
if osFamily {
os = `operating_system_family = "WINDOWS_SERVER_2019_CORE"`
} else {
os = ``
}

return fmt.Sprintf(`
resource "aws_ecs_task_definition" "test" {
family = %[1]q
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
cpu = 1024
memory = 2048

runtime_platform {
%[3]s
%[2]s
}

container_definitions = <<TASK_DEFINITION
[
{
"name": "iis",
"image": "mcr.microsoft.com/windows/servercore/iis",
"cpu": 1024,
"memory": 2048,
"essential": true
}
]
TASK_DEFINITION

}
`, tdName, arch, os)
}

func testAccTaskDefinitionWithTaskScopedDockerVolume(tdName string) string {
return fmt.Sprintf(`
resource "aws_ecs_task_definition" "test" {
Expand Down
Loading