Terraform-provider-aws: Unable to define application autoscaling policy for ECS

Created on 23 Oct 2017  ยท  4Comments  ยท  Source: hashicorp/terraform-provider-aws

Terraform Version

Terraform v0.10.7

AWS Provider Version

1.1

The problem

I am trying to get my head around application autoscaling for ECS. I want my service to scale up if CPU utilization is above a certain threshold, for example 80%. After some digging I came up with the following setup:

resource "aws_appautoscaling_target" "ecs_target" {
  max_capacity       = 5
  min_capacity       = "1"
  resource_id        = "service/my-cluster/my-service"
  role_arn           = "my-iam-role-arn"
  scalable_dimension = "ecs:service:DesiredCount"
  service_namespace  = "ecs"
}

resource "aws_appautoscaling_policy" "scale_up" {
  name                    = "my-name"
  resource_id             = "service/my-cluster-name/my-service"
  scalable_dimension      = "ecs:service:DesiredCount"
  service_namespace       = "ecs"

  step_scaling_policy_configuration {
    adjustment_type         = "ChangeInCapacity"
    cooldown                = 60
    metric_aggregation_type = "Maximum"

    step_adjustment {
      metric_interval_upper_bound = 0
      scaling_adjustment = 1
    }
  }
  /* this does not work... */
  target_tracking_scaling_policy_configuration {
    customized_metric_specification {
      namespace = "AWS/ECS"
      metric_name = "CPUUtilization"
      statistic = "Maximum"
      unit = 1
    }
    scale_out_cooldown = 10
    target_value       = 80
  }

  depends_on = ["aws_appautoscaling_target.ecs_target"]
}

Unfortunately this does not work:

aws_appautoscaling_policy.scale_up: Failed to update scaling policy: ValidationException: You must only provide a single scaling policy configuration, which must match the scaling policy type specified.
    status code: 400, request id: <request-id>

As soon I comment out the target_tracking_scaling_policy_configuration block it does not fail anymore, however in AWS under service autoscaling I see the policy created but the following message:

Please configure your alarm for this policy.

How do I configure an alarm for a policy (I want to configure CPUUtilization for namespace AWS/ECS).

Most helpful comment

I don't know how to configure this correctly using Terraform, but here is a correct configuration for ECS autoscaling using CloudFormation, and this may be helpful for you to see what you are missing: https://github.com/nathanpeck/ecs-cloudformation/blob/master/recipes/service.yml#L158-L300

All 4 comments

I don't know how to configure this correctly using Terraform, but here is a correct configuration for ECS autoscaling using CloudFormation, and this may be helpful for you to see what you are missing: https://github.com/nathanpeck/ecs-cloudformation/blob/master/recipes/service.yml#L158-L300

Thanks to @nathanpeck I found a workaround!

Just add the following and remove the target_tracking_scaling_policy_configuration block:

resource "aws_cloudwatch_metric_alarm" "cpu_utilization_high" {
  alarm_name          = "my-alarm-name"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = "2"
  metric_name         = "CPUUtilization"
  namespace           = "AWS/ECS"
  period              = "120"
  statistic           = "Maximum"
  threshold           = "80"

  dimensions {
    ServiceName = "my-service"
  }

  alarm_description = "This metric monitors ecs cpu utilization"
  alarm_actions     = ["${aws_appautoscaling_policy.scale_up.arn}"]
}

2019 update for googlers, I used @bitbrain's config above, but still wasn't working for us. Ended up also needing to provide the cluster name in the dimensions object:

resource "aws_cloudwatch_metric_alarm" "service_cpu_scale_down" {
  alarm_name          = "ServiceCPUScaleDown"
  comparison_operator = "LessThanOrEqualToThreshold"
  evaluation_periods  = "2"
  metric_name         = "CPUUtilization"
  namespace           = "AWS/ECS"
  period              = "60"
  statistic           = "Average"
  threshold           = "20"

  dimensions {
    ClusterName = "my-cluster-name"
    ServiceName = "my-service-name"
  }

  alarm_description = "This metric monitors ecs cpu utilization"
  alarm_actions     = ["${aws_appautoscaling_policy.ecs_service_scale_down.arn}"]
}

Thanks!

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