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

ECS Fargate Support #2483

Merged
merged 5 commits into from Dec 5, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions aws/resource_aws_ecs_service.go
Expand Up @@ -49,6 +49,13 @@ func resourceAwsEcsService() *schema.Resource {
Optional: true,
},

"launch_type": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Default: "EC2",
},

"iam_role": {
Type: schema.TypeString,
ForceNew: true,
Expand Down Expand Up @@ -206,6 +213,10 @@ func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error
input.Cluster = aws.String(v.(string))
}

if v, ok := d.GetOk("launch_type"); ok {
input.LaunchType = aws.String(v.(string))
}

loadBalancers := expandEcsLoadBalancers(d.Get("load_balancer").(*schema.Set).List())
if len(loadBalancers) > 0 {
log.Printf("[DEBUG] Adding ECS load balancers: %s", loadBalancers)
Expand Down Expand Up @@ -342,6 +353,8 @@ func resourceAwsEcsServiceRead(d *schema.ResourceData, meta interface{}) error {

d.Set("desired_count", service.DesiredCount)

d.Set("launch_type", service.LaunchType)

// Save cluster in the same format
if strings.HasPrefix(d.Get("cluster").(string), "arn:"+meta.(*AWSClient).partition+":ecs:") {
d.Set("cluster", service.ClusterArn)
Expand Down
98 changes: 98 additions & 0 deletions aws/resource_aws_ecs_service_test.go
Expand Up @@ -341,6 +341,24 @@ func TestAccAWSEcsServiceWithPlacementConstraints_emptyExpression(t *testing.T)
})
}

func TestAccAWSEcsServiceWithLaunchTypeFargate(t *testing.T) {
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcsServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEcsServiceWithLaunchTypeFargate(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsServiceExists("aws_ecs_service.main"),
resource.TestCheckResourceAttr("aws_ecs_service.main", "launch_type", "FARGATE"),
),
},
},
})
}

func TestAccAWSEcsServiceWithNetworkConfiguration(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -597,6 +615,86 @@ resource "aws_ecs_service" "mongo" {
`, rInt, rInt)
}

func testAccAWSEcsServiceWithLaunchTypeFargate(rInt int) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of our acceptance tests run in us-west-2 by default and Fargate unfortunately isn't available there yet, so we'll need to pin this test to us-east-1, otherwise it fails:

=== RUN   TestAccAWSEcsServiceWithLaunchTypeFargate
--- FAIL: TestAccAWSEcsServiceWithLaunchTypeFargate (62.17s)
	testing.go:503: Step 0 error: Error applying: 1 error(s) occurred:

		* aws_ecs_service.main: 1 error(s) occurred:

		* aws_ecs_service.main: UnsupportedFeatureException: FARGATE is not supported in this region.
			status code: 400, request id: dc224bee-d9c1-11e7-a440-c32534bb3ba4 "tf-ecs-service-8420937161126328979"

We can do so by adding a provider block to the config here, e.g.

provider "aws" {
  region = "us-east-1"
}

we do that for a couple of other services already for the same reason, like Lightsail.


resource "aws_vpc" "main" {
cidr_block = "10.10.0.0/16"
}

resource "aws_subnet" "main" {
count = 2
cidr_block = "${cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)}"
availability_zone = "${data.aws_availability_zones.available.names[count.index]}"
vpc_id = "${aws_vpc.main.id}"
}

resource "aws_security_group" "allow_all_a" {
name = "allow_all_a_%d"
description = "Allow all inbound traffic"
vpc_id = "${aws_vpc.main.id}"

ingress {
protocol = "6"
from_port = 80
to_port = 8000
cidr_blocks = ["${aws_vpc.main.cidr_block}"]
}
}

resource "aws_security_group" "allow_all_b" {
name = "allow_all_b_%d"
description = "Allow all inbound traffic"
vpc_id = "${aws_vpc.main.id}"

ingress {
protocol = "6"
from_port = 80
to_port = 8000
cidr_blocks = ["${aws_vpc.main.cidr_block}"]
}
}

resource "aws_ecs_cluster" "main" {
name = "tf-ecs-cluster-%d"
}

resource "aws_ecs_task_definition" "mongo" {
family = "mongodb"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = "256"
memory = "512"

container_definitions = <<DEFINITION
[
{
"cpu": 256,
"essential": true,
"image": "mongo:latest",
"memory": 512,
"name": "mongodb",
"networkMode": "awsvpc"
}
]
DEFINITION
}

resource "aws_ecs_service" "main" {
name = "tf-ecs-service-%d"
cluster = "${aws_ecs_cluster.main.id}"
task_definition = "${aws_ecs_task_definition.mongo.arn}"
desired_count = 1
launch_type = "FARGATE"
network_configuration {
security_groups = ["${aws_security_group.allow_all_a.id}", "${aws_security_group.allow_all_b.id}"]
subnets = ["${aws_subnet.main.*.id}"]
}
}
`, rInt, rInt, rInt, rInt)
}

var testAccAWSEcsService_withIamRole = `
resource "aws_ecs_cluster" "main" {
name = "terraformecstest11"
Expand Down
49 changes: 49 additions & 0 deletions aws/resource_aws_ecs_task_definition.go
Expand Up @@ -27,6 +27,12 @@ func resourceAwsEcsTaskDefinition() *schema.Resource {
Computed: true,
},

"cpu": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"family": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -59,6 +65,18 @@ func resourceAwsEcsTaskDefinition() *schema.Resource {
ForceNew: true,
},

"execution_role_arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"memory": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"network_mode": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -109,6 +127,13 @@ func resourceAwsEcsTaskDefinition() *schema.Resource {
},
},
},

"requires_compatibilities": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
Expand Down Expand Up @@ -155,6 +180,18 @@ func resourceAwsEcsTaskDefinitionCreate(d *schema.ResourceData, meta interface{}
input.TaskRoleArn = aws.String(v.(string))
}

if v, ok := d.GetOk("execution_role_arn"); ok {
input.ExecutionRoleArn = aws.String(v.(string))
}

if v, ok := d.GetOk("cpu"); ok {
input.Cpu = aws.String(v.(string))
}

if v, ok := d.GetOk("memory"); ok {
input.Memory = aws.String(v.(string))
}

if v, ok := d.GetOk("network_mode"); ok {
input.NetworkMode = aws.String(v.(string))
}
Expand Down Expand Up @@ -185,6 +222,10 @@ func resourceAwsEcsTaskDefinitionCreate(d *schema.ResourceData, meta interface{}
input.PlacementConstraints = pc
}

if v, ok := d.GetOk("requires_compatibilities"); ok && v.(*schema.Set).Len() > 0 {
input.RequiresCompatibilities = expandStringList(v.(*schema.Set).List())
}

log.Printf("[DEBUG] Registering ECS task definition: %s", input)
out, err := conn.RegisterTaskDefinition(&input)
if err != nil {
Expand Down Expand Up @@ -231,12 +272,20 @@ func resourceAwsEcsTaskDefinitionRead(d *schema.ResourceData, meta interface{})
}

d.Set("task_role_arn", taskDefinition.TaskRoleArn)
d.Set("execution_role_arn", taskDefinition.ExecutionRoleArn)
d.Set("cpu", taskDefinition.Cpu)
d.Set("memory", taskDefinition.Memory)
d.Set("network_mode", taskDefinition.NetworkMode)
d.Set("volumes", flattenEcsVolumes(taskDefinition.Volumes))
if err := d.Set("placement_constraints", flattenPlacementConstraints(taskDefinition.PlacementConstraints)); err != nil {
log.Printf("[ERR] Error setting placement_constraints for (%s): %s", d.Id(), err)
}

d.Set("requires_compatibilities", taskDefinition.RequiresCompatibilities)
if err := d.Set("requires_compatibilities", flattenStringList(taskDefinition.RequiresCompatibilities)); err != nil {
log.Printf("[ERR] Error setting requires_compatibilities for (%s): %s", d.Id(), err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any particular reason why we should silently ignore this error? Also why we're setting the same field twice? I think the one above won't work as we're passing []*string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No actual reason I just copied the code from the lines above. Will return the error.

}

return nil
}

Expand Down
135 changes: 135 additions & 0 deletions aws/resource_aws_ecs_task_definition_test.go
Expand Up @@ -181,6 +181,48 @@ func TestAccAWSEcsTaskDefinition_arrays(t *testing.T) {
})
}

func TestAccAWSEcsTaskDefinition_Fargate(t *testing.T) {
var conf ecs.TaskDefinition
familyName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcsTaskDefinitionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEcsTaskDefinitionFargate(familyName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.fargate", &conf),
resource.TestCheckResourceAttr("aws_ecs_task_definition.fargate", "requires_compatibilities.#", "1"),
resource.TestCheckResourceAttr("aws_ecs_task_definition.fargate", "cpu", "256"),
resource.TestCheckResourceAttr("aws_ecs_task_definition.fargate", "memory", "512"),
),
},
},
})
}

func TestAccAWSEcsTaskDefinition_ExecutionRole(t *testing.T) {
var conf ecs.TaskDefinition
familyName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcsTaskDefinitionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEcsTaskDefinitionExecutionRole(familyName, rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.fargate", &conf),
),
},
},
})
}

func testAccCheckEcsTaskDefinitionRecreated(t *testing.T,
before, after *ecs.TaskDefinition) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -553,6 +595,99 @@ TASK_DEFINITION
`, familyName)
}

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

func testAccAWSEcsTaskDefinitionExecutionRole(familyName string, rInt int) string {
return fmt.Sprintf(`
resource "aws_iam_role" "role" {
name = "test-role-%d"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ecs-tasks.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}

resource "aws_iam_policy" "policy" {
name = "test-policy-%d"
description = "A test policy"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}
EOF
}

resource "aws_iam_role_policy_attachment" "test-attach" {
role = "${aws_iam_role.role.name}"
policy_arn = "${aws_iam_policy.policy.arn}"
}

resource "aws_ecs_task_definition" "fargate" {
family = "%s"
execution_role_arn = "${aws_iam_role.role.arn}"
container_definitions = <<TASK_DEFINITION
[
{
"name": "sleep",
"image": "busybox",
"cpu": 10,
"command": ["sleep","360"],
"memory": 10,
"essential": true
}
]
TASK_DEFINITION
}
`, rInt, rInt, familyName)
}

var testAccAWSEcsTaskDefinitionWithScratchVolume = `
resource "aws_ecs_task_definition" "sleep" {
family = "terraform-acc-sc-volume-test"
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/ecs_service.html.markdown
Expand Up @@ -70,6 +70,7 @@ Load balancers support the following:
* `target_group_arn` - (Required for ALB) The ARN of the ALB target group to associate with the service.
* `container_name` - (Required) The name of the container to associate with the load balancer (as it appears in a container definition).
* `container_port` - (Required) The port on the container to associate with the load balancer.
* `launch_type` - (Optional) The launch type on which to run your service. The valid values are `EC2` and `FARGATE`. Defaults to `EC2`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you write defaults to EC2, we actually do not set any defaults. Is there something I'm missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AWS API default to API because otherwise, it would have been a breaking change for them. Might be better not the mix AWS specifics into the Terraform documentation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AWS API default to API

Not sure to get it here: Is API a typo or really a valid value? can not find anything on that.

Also, we already have default values in a lot of places, whether those are booleans, floats, etc, in order to mimic the AWS API correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a typo, I meant EC2. So you would suggest adopting the AWS default into the Schema? What about the requires_compatibilities field of r/aws_ecs_task_definition, it defaults to ["EC2"]


## placement_strategy

Expand Down