Skip to content

Instantly share code, notes, and snippets.

@fillup
Created March 30, 2017 17:49
Show Gist options
  • Save fillup/af06c64f06943dfda6621898c866d48a to your computer and use it in GitHub Desktop.
Save fillup/af06c64f06943dfda6621898c866d48a to your computer and use it in GitHub Desktop.
Issue with Terraform aws_ecs_service when using placement_strategy

Below is the series of Terraform commands run and the relevant snippets of output from each:

resource "aws_ecs_service" "service" {
   name = "${var.service_name}"
   cluster = "${var.cluster_id}"
   desired_count = "${var.desired_count}"
   iam_role = "${aws_iam_role.ecsServiceRole.arn}"
   depends_on = ["aws_iam_role_policy.ecsServiceRolePolicy", "aws_alb_listener.https"]

   placement_strategy {
     type = "spread"
     field = "instanceId"
   }

   load_balancer {
     target_group_arn = "${aws_alb_target_group.tg.arn}"
     container_name = "${var.lb_container_name}"
     container_port = "${var.lb_container_port}"
   }

   # Track the latest ACTIVE revision
   task_definition = "${aws_ecs_task_definition.td.family}:${max("${aws_ecs_task_definition.td.revision}", "${data.aws_ecs_task_definition.td.revision}")}"
 }

$ terraform apply

...
module.ecsservice.aws_ecs_service.service: Creating...
  cluster:                                  "" => "arn:aws:ecs:us-east-1:123456789012:cluster/terraform-testing"
  deployment_maximum_percent:               "" => "200"
  deployment_minimum_healthy_percent:       "" => "100"
  desired_count:                            "" => "3"
  iam_role:                                 "" => "arn:aws:iam::123456789012:role/ecsServiceRole"
  load_balancer.#:                          "" => "1"
  load_balancer.987343868.container_name:   "" => "app"
  load_balancer.987343868.container_port:   "" => "80"
  load_balancer.987343868.elb_name:         "" => ""
  load_balancer.987343868.target_group_arn: "" => "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/tg-doorman/f82e209888079421"
  name:                                     "" => "doorman"
  placement_strategy.#:                     "" => "1"
  placement_strategy.3946258308.field:      "" => "instanceId"
  placement_strategy.3946258308.type:       "" => "spread"
  task_definition:                          "" => "doorman-staging:18"
...

$ terraform plan

...
-/+ module.ecsservice.aws_ecs_service.service
  cluster:                                  "arn:aws:ecs:us-east-1:123456789012:cluster/terraform-testing" => "arn:aws:ecs:us-east-1:123456789012:cluster/terraform-testing"
  deployment_maximum_percent:               "200" => "200"
  deployment_minimum_healthy_percent:       "100" => "100"
  desired_count:                            "3" => "3"
  iam_role:                                 "arn:aws:iam::123456789012:role/ecsServiceRole" => "arn:aws:iam::123456789012:role/ecsServiceRole"
  load_balancer.#:                          "1" => "1"
  load_balancer.987343868.container_name:   "app" => "app"
  load_balancer.987343868.container_port:   "80" => "80"
  load_balancer.987343868.elb_name:         "" => ""
  load_balancer.987343868.target_group_arn: "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/tg-doorman/f82e209888079421" => "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/tg-doorman/f82e209888079421"
  name:                                     "doorman" => "doorman"
  placement_strategy.#:                     "1" => "1"
  placement_strategy.1676812570.field:      "instanceid" => "" (forces new resource)
  placement_strategy.1676812570.type:       "spread" => "" (forces new resource)
  placement_strategy.3946258308.field:      "" => "instanceId" (forces new resource)
  placement_strategy.3946258308.type:       "" => "spread" (forces new resource)
  task_definition:                          "doorman-staging:18" => "doorman-staging:18"

Plan: 1 to add, 0 to change, 1 to destroy.

$ terraform apply

...
module.ecsservice.aws_ecs_service.service: Destroying... (ID: arn:aws:e.../doorman)
module.ecsservice.aws_ecs_service.service: Still destroying... (ID: arn:aws:e.../doorman, 10s elapsed)
module.ecsservice.aws_ecs_service.service: Still destroying... (ID: arn:aws:e.../doorman, 1m20s elapsed)
module.ecsservice.aws_ecs_service.service: Destruction complete
module.ecsservice.aws_ecs_service.service: Creating...
  cluster:                                  "" => "arn:aws:ecs:us-east-1:123456789012:cluster/terraform-testing"
  deployment_maximum_percent:               "" => "200"
  deployment_minimum_healthy_percent:       "" => "100"
  desired_count:                            "" => "3"
  iam_role:                                 "" => "arn:aws:iam::123456789012:role/ecsServiceRole"
  load_balancer.#:                          "" => "1"
  load_balancer.987343868.container_name:   "" => "app"
  load_balancer.987343868.container_port:   "" => "80"
  load_balancer.987343868.elb_name:         "" => ""
  load_balancer.987343868.target_group_arn: "" => "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/tg-doorman/f82e209888079421"
  name:                                     "" => "doorman"
  placement_strategy.#:                     "" => "1"
  placement_strategy.3946258308.field:      "" => "instanceId"
  placement_strategy.3946258308.type:       "" => "spread"
  task_definition:                          "" => "doorman-staging:18"
module.ecsservice.aws_ecs_service.service: Creation complete (ID: arn:aws:e.../doorman)

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.

$ terraform plan

...
-/+ module.ecsservice.aws_ecs_service.service
    cluster:                                  "arn:aws:ecs:us-east-1:123456789012:cluster/terraform-testing" => "arn:aws:ecs:us-east-1:123456789012:cluster/terraform-testing"
    deployment_maximum_percent:               "200" => "200"
    deployment_minimum_healthy_percent:       "100" => "100"
    desired_count:                            "3" => "3"
    iam_role:                                 "arn:aws:iam::123456789012:role/ecsServiceRole" => "arn:aws:iam::123456789012:role/ecsServiceRole"
    load_balancer.#:                          "1" => "1"
    load_balancer.987343868.container_name:   "app" => "app"
    load_balancer.987343868.container_port:   "80" => "80"
    load_balancer.987343868.elb_name:         "" => ""
    load_balancer.987343868.target_group_arn: "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/tg-doorman/f82e209888079421" => "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/tg-doorman/f82e209888079421"
    name:                                     "doorman" => "doorman"
    placement_strategy.#:                     "1" => "1"
    placement_strategy.1676812570.field:      "instanceid" => "" (forces new resource)
    placement_strategy.1676812570.type:       "spread" => "" (forces new resource)
    placement_strategy.3946258308.field:      "" => "instanceId" (forces new resource)
    placement_strategy.3946258308.type:       "" => "spread" (forces new resource)
    task_definition:                          "doorman-staging:18" => "doorman-staging:18"

Plan: 1 to add, 0 to change, 1 to destroy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment