hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.62k stars 9k forks source link

When linking capacity provider to an ASG, a scaling policy is not created #12534

Open evgenibi opened 4 years ago

evgenibi commented 4 years ago

Community Note

Terraform Version

Terraform v0.12.21

Affected Resource(s)

Terraform Configuration Files

# Copy-paste your Terraform configurations here - for large Terraform configs,
# please use a service like Dropbox and share a link to the ZIP file. For
# security, you can also encrypt the files using our GPG public key: https://keybase.io/hashicorp

Debug Output

Panic Output

Expected Behavior

When manually creating a capacity provider and linking it to the ASG, a scaling policy is created:

Policy type: Target Tracking scaling Execute policy when: As required to maintain metric CapacityProviderReservation (namespace: AWS/ECS/ManagedScaling, dimensions: CapacityProviderName = test,ClusterName = test, statistic: Average, unit: ) at 100 Take the action: Add or remove instances as required Instances need: 300 seconds to warm up after scaling Disable scale-in: No

Actual Behavior

Scaling policy is empty and is not scaling out because of that when running a task

Steps to Reproduce

  1. Create LC
  2. Create ASG
  3. Create Capacity Provider and link it:

auto_scaling_group_provider { auto_scaling_group_arn = aws_autoscaling_group.bender_64_asg.arn managed_termination_protection = "ENABLED"

managed_scaling {
  maximum_scaling_step_size = 10
  minimum_scaling_step_size = 1
  status = "ENABLED"
  target_capacity = 100
}

}

Important Factoids

References

evgenibi commented 4 years ago

Any suggestions for this one?

peter-boekelheide-ah commented 4 years ago

A workaround is to create it yourself. Here's my example:

resource "aws_autoscaling_policy" "autoscale_policy" {
  autoscaling_group_name = aws_autoscaling_group.main_autoscaling_group.name
  policy_type = "TargetTrackingScaling"
  name = "${aws_autoscaling_group.main_autoscaling_group.name}_auto_scaling_policy"
  target_tracking_configuration {
    target_value = 100
    customized_metric_specification {
      metric_name = "CapacityProviderReservation"
      namespace = "AWS/ECS/ManagedScaling"
      statistic = "Average"
      metric_dimension {
        name = "CapacityProviderName"
        value = aws_ecs_capacity_provider.main_cap_provider.name
      }
      metric_dimension {
        value = aws_ecs_cluster.main_cluster.name
        name = "ClusterName"
      }
    }
    disable_scale_in = false
  }
}

This does seem to make scaling out work, but scaling in still isn't working for me (possibly due to #12582).

evgenibi commented 4 years ago

Any love for this one?

srolel commented 3 years ago

also interested in a solution to this, I created the policy but scale-in doesn't work as @peter-boekelheide-ah mentioned. I do see the AmazonECSManaged tag on the instances so it doesn't appear to be that.

edit: disabling scale-in protection on the instances makes them get terminated, but that isn't compatible with the capacity provider...

hlarsen commented 2 years ago

i think in some situations it is creating the scaling policy. i was setting up a private module and had added an aws_autoscaling_policy because one wasn't present on my ASG.

after i was happy with the setup from creating the module i used it again for another one and this time i do have a scaling policy attached to the ASG with a name of AutoScaling-ECSManagedAutoScalingPlan-xxxxx with a configuration matching the code in in my aws_ecs_capacity_provider resource. i also got an error creating the aws_autoscaling_policy resources because the AWS managed ones were already present.

in the module i ended up with the capacity provider depending on the asg so maybe the asg needs to be created before the capacity provider for the managed scaling policy to be created. note that the managed scaling policy does not appear to be tracked in the terraform state.

i've also (unsuccessfully) been trying to get the managed policy re-generated on my original setup - so far turning managed scaling off and back on for the capacity provider hasn't done it, and i'll need to update the running services to try deleting and recreating the capacity provider itself.

edit:

deleting the capacity provider from the cluster and re-adding it via the web ui does create the managed scaling policy in the existing ASG, and updating the capacity provider managed_scaling will update the managed scaling policy (that is not tracked by terraform). trying to re-add the capacity provider via terraform creates it, however it does not 'register' with the cluster or create the scaling policy.

rmccarthy-ellevation commented 11 months ago

I'm running into this issue. Is there going to be a fix for this?