Terraform: Computed desired_count for aws_ecs_service resource

Created on 2 Feb 2016  ยท  4Comments  ยท  Source: hashicorp/terraform

Hi guys, I'm trying to deploy aws ecs service using terraform, setup is pretty much simple:

resource "aws_ecs_service" "example_ecs_service" {
  name = "example"
  cluster = "${aws_ecs_cluster.example_cluster1.id}"
  task_definition = "${aws_ecs_task_definition.example_task_def.arn}"
  desired_count = 2
  iam_role = "${var.ecs_service_role_arn}"

  load_balancer {
    elb_name = "${aws_elb.example_service_elb.id}"
    container_name = "example"
    container_port = 8443
  }
}

We have lambda function that checks request number on ELB (aws_elb.example_service_elb.id) and changes desired count of ecs service e.g. to 3 in case of requests spike. And when we do terraform apply it resets desired_count to 2 that is specified in terraform configuration. Is there possibility to make terraform to handle desired_count as calculated value e.g.:

  1. when service is created terraform may take option let's say default_desired_count.
  2. when service is updated (e.g. its task def is changed) then its desired_count is not changed by terraform.

Please advise. Thank you!

P.S. I tried not to specify desired_count in aws_ecs_service -- looks like terraform handles it as aws_ecs_service = 0 that definitely not what I'm trying to achieve.

provideaws question

Most helpful comment

Hi,
desiredCount is a required parameter per the API specs:
http://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_CreateService.html

Based on that Terraform cannot make this optional.

However, you can let Terraform initiate the ECS service with desired_count = 1 or something like that and then use the ignore_changes lifecycle parameter:

resource "aws_ecs_service" "example_ecs_service" {
  name = "example"
  cluster = "${aws_ecs_cluster.example_cluster1.id}"
  task_definition = "${aws_ecs_task_definition.example_task_def.arn}"
  desired_count = 2
  iam_role = "${var.ecs_service_role_arn}"

  load_balancer {
    elb_name = "${aws_elb.example_service_elb.id}"
    container_name = "example"
    container_port = 8443
  }

  lifecycle {
    ignore_changes = ["desired_count"]
  }
}

Feel free to submit an issue if that doesn't work for you.

All 4 comments

Hi,
desiredCount is a required parameter per the API specs:
http://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_CreateService.html

Based on that Terraform cannot make this optional.

However, you can let Terraform initiate the ECS service with desired_count = 1 or something like that and then use the ignore_changes lifecycle parameter:

resource "aws_ecs_service" "example_ecs_service" {
  name = "example"
  cluster = "${aws_ecs_cluster.example_cluster1.id}"
  task_definition = "${aws_ecs_task_definition.example_task_def.arn}"
  desired_count = 2
  iam_role = "${var.ecs_service_role_arn}"

  load_balancer {
    elb_name = "${aws_elb.example_service_elb.id}"
    container_name = "example"
    container_port = 8443
  }

  lifecycle {
    ignore_changes = ["desired_count"]
  }
}

Feel free to submit an issue if that doesn't work for you.

@radeksimko Thanks! It's exactly what I need.

Looks like it still doesn't works as expected. Here's how to reproduce:

  1. create ecs service with ignore_changes enabled for desired_count:
resource "aws_ecs_service" "issues_ecs_service" {
  name = "issues_${var.environment_name}"
  cluster = "${aws_ecs_cluster.cluster1.id}"
  task_definition = "${aws_ecs_task_definition.issues_task_def.arn}"
  desired_count = 2
  iam_role = "${var.ecs_service_role_arn}"

  load_balancer {
    elb_name = "${aws_elb.issues_service_elb.id}"
    container_name = "issues"
    container_port = 8443
  }

  lifecycle {
    ignore_changes = [ "desired_count" ]
  }
}
  1. Change ecs desired count via ECS console or API call (we're doing it via AWS lambda).
  2. Try to apply changes into ecs service e.g. set new task definition for it.

Here's the log:

2016/02/02 16:34:57 [DEBUG] root: eval: *terraform.EvalIf
2016/02/02 16:34:57 [DEBUG] root: eval: *terraform.EvalInterpolate
2016/02/02 16:34:57 [DEBUG] root: eval: *terraform.EvalGetProvider
2016/02/02 16:34:57 [DEBUG] root: eval: *terraform.EvalReadState
2016/02/02 16:34:57 [DEBUG] root: eval: *terraform.EvalDiff
2016/02/02 16:34:57 [DEBUG] root: eval: *terraform.EvalReadDiff
2016/02/02 16:34:57 [DEBUG] root: eval: *terraform.EvalCompareDiff
2016/02/02 16:34:57 [ERROR] aws_ecs_service.issues_ecs_service: diffs didn't match
2016/02/02 16:34:57 [ERROR] aws_ecs_service.issues_ecs_service: reason: extra attributes: desired_count
2016/02/02 16:34:57 [ERROR] aws_ecs_service.issues_ecs_service: diff one: *terraform.InstanceDiff{Attributes:map[string]*terraform.ResourceAttrDiff{"task_definition":*terraform.ResourceAttrDiff{Old:"arn:aws:ecs:us-east-1:xxx:task-definition/issues_service_Staging:25", New:"arn:aws:ecs:us-east-1:xxx:task-definition/issues_service_Staging:26", NewComputed:false, NewRemoved:false, NewExtra:interface {}(nil), RequiresNew:false, Type:0x0}}, Destroy:false, DestroyTainted:false}
2016/02/02 16:34:57 [ERROR] aws_ecs_service.issues_ecs_service: diff two: *terraform.InstanceDiff{Attributes:map[string]*terraform.ResourceAttrDiff{"task_definition":*terraform.ResourceAttrDiff{Old:"arn:aws:ecs:us-east-1:xxx:task-definition/issues_service_Staging:25", New:"arn:aws:ecs:us-east-1:xxx:task-definition/issues_service_Staging:26", NewComputed:false, NewRemoved:false, NewExtra:interface {}(nil), RequiresNew:false, Type:0x0}, "desired_count":*terraform.ResourceAttrDiff{Old:"4", New:"2", NewComputed:false, NewRemoved:false, NewExtra:interface {}(nil), RequiresNew:false, Type:0x0}}, Destroy:false, DestroyTainted:false}
2016/02/02 16:34:57 [ERROR] root: eval: *terraform.EvalCompareDiff, err: aws_ecs_service.issues_ecs_service: diffs didn't match during apply. This is a bug with Terraform and should be reported.
2016/02/02 16:34:57 [ERROR] root: eval: *terraform.EvalSequence, err: aws_ecs_service.issues_ecs_service: diffs didn't match during apply. This is a bug with Terraform and should be reported.
2016/02/02 16:34:57 [ERROR] root: eval: *terraform.EvalOpFilter, err: aws_ecs_service.issues_ecs_service: diffs didn't match during apply. This is a bug with Terraform and should be reported.
2016/02/02 16:34:57 [ERROR] root: eval: *terraform.EvalSequence, err: aws_ecs_service.issues_ecs_service: diffs didn't match during apply. This is a bug with Terraform and should be reported.
2016/02/02 16:34:57 [INFO] Exiting eval tree: aws_ecs_service.issues_ecs_service
2016/02/02 16:34:57 [DEBUG] vertex provider.aws (close), got dep: aws_ecs_service.issues_ecs_service
2016/02/02 16:34:57 [DEBUG] vertex root, got dep: provider.aws (close)
2016/02/02 16:34:57 [DEBUG] vertex root, got dep: provisioner.chef
2016/02/02 16:34:57 [DEBUG] vertex root, got dep: var.iam_instance_profile
2016/02/02 16:34:57 [DEBUG] vertex root, got dep: provisioner.file
2016/02/02 16:34:57 [DEBUG] vertex root, got dep: var.availability_zones

'terraform apply' starts working again without issues as soon as I return desired count to initial value (2).

Please let me know if there any options to resolve it. Thank you!

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 have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings