Terraform-provider-aws: aws_ecs_service/desired_count must be optional

Created on 13 Jun 2017  ยท  4Comments  ยท  Source: hashicorp/terraform-provider-aws

_This issue was originally opened by @BrunoBonacci as hashicorp/terraform#9690. It was migrated here as part of the provider split. The original body of the issue is below._


The desired_count in the aws_ecs_service appears to be required. However the AWS api mandate this parameter only for the _creation_ of a service. For the update part of a service the desired_count must be optional.

The reason being is that the desired_count is subject to automatic modification via the auto scaling policies. Mandate this value causes the service to _overwrite_ the value which was set by the auto-scaling policies.

Assume the case that you have a service which is created with a desired_count = 3. Then thanks to the auto scaling policies the service is scaled out to a desired_count = 10. At this point the terraform script still contains desired_count = 3 and if you were to apply the changes the desired_count would be _forcefully reset_ to its initial value of 3.

~ aws_ecs_service.ecs_service
    desired_count: "10" => "3"

And if I try to remove the desired_count = 3 from the script then it will set it to 0.

~ aws_ecs_service.ecs_service
    desired_count: "10" => "0"

By forcefully setting a lower number of desired_count it will tell the ECS service to KILL some of the excess containers to meet the desired number.
This would obviously cause some service disruption as suddenly big part of the container fleet will disappear.

The desired_count is required for manual scaling, but it is harmful/dangerous to use in conjunction to autoscaling. Like for the EC2 autoscaling should be entirely optional to use this value.
The solution suggested Is to introduce a new option for the aws_ecs_service called initial_count which will be passed as desired_count only when the resource is created and never during an update.

This is currently a big problem for those who want to use ECS in conjunction with the autoscaling policies as there is no alternative solution.

Terraform Version

  • Terraform v0.7.7

    Affected Resource(s)

Please list the resources as a list, for example:

  • aws_ecs_service

Terraform Configuration Files

resource "aws_ecs_service" "mongo" {
  name = "mongodb"
  cluster = "${aws_ecs_cluster.foo.id}"
  task_definition = "${aws_ecs_task_definition.mongo.arn}"

  desired_count = 3

  iam_role = "${aws_iam_role.foo.arn}"
  depends_on = ["aws_iam_role_policy.foo"]

  load_balancer {
    elb_name = "${aws_elb.foo.name}"
    container_name = "mongo"
    container_port = 8080
  }
}

Expected Behavior

The solution suggested Is to introduce a new option for the aws_ecs_service called initial_count which will be passed as desired_count only when the resource is created and never during an update.

Actual Behavior

The desired_count KILLS all excess containers causing application downtime.

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. create a ECS cluster
  2. create a container service
  3. define autoscaling policies
  4. let the policies scale your system under load
  5. re-apply the script
  6. notice desired_count is reset to the initial value and excess container killed.
bug servicecs

Most helpful comment

We use the below configuration to set it initially and ignore changes, which generically is available on any Terraform resource and attribute:

resource "aws_ecs_service" "example" {
  ...
  lifecycle {
    ignore_changes = ["desired_count"]
  }
  ...
}

Documentation: https://www.terraform.io/docs/configuration/resources.html#ignore_changes

All 4 comments

+1

This is an issue for us.

We use the below configuration to set it initially and ignore changes, which generically is available on any Terraform resource and attribute:

resource "aws_ecs_service" "example" {
  ...
  lifecycle {
    ignore_changes = ["desired_count"]
  }
  ...
}

Documentation: https://www.terraform.io/docs/configuration/resources.html#ignore_changes

The aws_ecs_service resource documentation has been updated to include the ignore_changes configuration that allows this problem to be solved (works generically with all Terraform resources/attributes): https://www.terraform.io/docs/providers/aws/r/ecs_service.html#ignoring-changes-to-desired-count

resource "aws_ecs_service" "example" {
  # ... other configurations ...

  # Example: Create service with 2 instances to start
  desired_count = 2

  # Optional: Allow external changes without Terraform plan difference
  lifecycle {
    ignore_changes = ["desired_count"]
  }
}

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

Was this page helpful?
0 / 5 - 0 ratings