terraform-aws-modules / terraform-aws-autoscaling

Terraform module to create AWS Auto Scaling resources 🇺🇦
https://registry.terraform.io/modules/terraform-aws-modules/autoscaling/aws
Apache License 2.0
287 stars 552 forks source link

Insufficient configuration to set up Scaling Policy - related to ECS over EC2 #235

Closed sebastianarca closed 10 months ago

sebastianarca commented 1 year ago

I used this module to create an autoscaling group following the recommendations of the examples, to assign it as Capacity Provider of an ECS cluster. Due to a misconception I wanted to create an autoscaling policy, when I created it, the default one was deleted.

When I wanted to revert the changes, it was not possible to use the "scaling_policies" configurations provided by the module because it only accepts a "metric_dimension", to solve the problem I had to create a resource "aws_autoscaling_policy" manually.

Note that when creating the autoscaling group the first time, no policy was set and one was assigned automatically. Subsequent modifications were made by importing the state of the existing policy.

I leave the module configuration below.

module "autoscaling" {
  source  = "terraform-aws-modules/autoscaling/aws"
  version = "~> 6.10"

  for_each = {
    # On-demand instances
    ex-1 = {
      instance_type              = local.cluster_config.instance_type
      use_mixed_instances_policy = false // https://docs.aws.amazon.com/autoscaling/ec2/APIReference/API_MixedInstancesPolicy.html
      mixed_instances_policy     = {}
      user_data                  = <<-EOT
        #!/bin/bash
        cat <<'EOF' >> /etc/ecs/ecs.config
        ECS_CLUSTER=${local.cluster_config.name}
        ECS_LOGLEVEL=error
        ECS_CONTAINER_INSTANCE_TAGS=${jsonencode(local.tags)}
        ECS_ENABLE_TASK_IAM_ROLE=true
        EOF
      EOT
    }
  }

  force_delete                    = true
  capacity_rebalance              = false
  enable_monitoring               = false  

  vpc_zone_identifier           = local.network_config.vpc_zone_identifier

# ####
# Why is it not possible to configure the interface in private mode and it is necessary to assign a public IP?
# https://docs.aws.amazon.com/AmazonECS/latest/userguide/service-configure-network.html
# ####
  network_interfaces = [
    {
      associate_public_ip_address = true
      delete_on_termination       = true
      device_index                = 0
      description                 = "ECS-Cluster_${local.cluster_config.name}" 
    }
  ]
  key_name                        = local.cluster_config.key_name
  launch_template_use_name_prefix = false
  use_name_prefix                 = false
  name                            = "ECS-Cluster_${local.cluster_config.name}"
  launch_template_name            = "ECSLaunchTemplate_${local.cluster_config.name}"
  image_id                        = "ami-0c42c53ff255097bc" // al2023-ami-ecs-hvm-2023.0.20230509-kernel-6.1-x86_64 // jsondecode(data.aws_ssm_parameter.ecs_optimized_ami.value)["image_id"]
  instance_type                   = each.value.instance_type

  security_groups                 = local.network_config.security_groups
  user_data                       = base64encode(each.value.user_data)
  ignore_desired_capacity_changes = false

  create_iam_instance_profile     = true
  iam_role_name                   = "ecsInstanceRole_${local.cluster_config.name}"
  iam_role_description            = "ECS role for ${local.cluster_config.name}"
  iam_role_use_name_prefix        = false

  iam_role_policies               = {
    AmazonEC2ContainerServiceforEC2Role = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
  }

  health_check_type               = "EC2"
  min_size                        = local.cluster_config.min_size
  max_size                        = local.cluster_config.max_size
  # desired_capacity                = local.cluster_config.desired_capacity

  # https://github.com/hashicorp/terraform-provider-aws/issues/12582
  autoscaling_group_tags          = {
    AmazonECSManaged    = true
    Name                = "ECS-Instance_${local.cluster_config.name}"
  }

  # Required for  managed_termination_protection = "ENABLED"
  protect_from_scale_in           = true

  # Spot instances
  use_mixed_instances_policy      = each.value.use_mixed_instances_policy
  mixed_instances_policy          = each.value.mixed_instances_policy

  tags                            = local.tags
}
# ##
# This autoscaling configuration is required according to AWS documentation.
# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cluster-auto-scaling.html
# ##
resource "aws_autoscaling_policy" "this" {
  name                   = "ECSManagedAutoScalingPolicy"
  policy_type            = "TargetTrackingScaling"

  autoscaling_group_name = "ECS-Cluster_${local.cluster_config.name}"

  target_tracking_configuration {
    customized_metric_specification {
      metric_dimension {
        name  = "CapacityProviderName"
        value = aws_ecs_capacity_provider.this.name
      }
      metric_dimension {
        name  = "ClusterName"
        value = local.cluster_config.name
      }

      metric_name = "CapacityProviderReservation"
      namespace   = "AWS/ECS/ManagedScaling"
      statistic   = "Average"
    }

    target_value = 100
  }
}

The configuration applied within the module that I applied to cause the deletion was the following:

scaling_policies = {
    avg-cpu-policy_instances = {
      policy_type               = "TargetTrackingScaling"
      estimated_instance_warmup = 300
      target_tracking_configuration = {
        # Politica preferida
        predefined_metric_specification = {
          predefined_metric_type = "ASGAverageCPUUtilization"
        }
        target_value = local.cluster_config.cpu_target_autoscaling
      }
    },
  }
github-actions[bot] commented 11 months ago

This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days

github-actions[bot] commented 10 months ago

This issue was automatically closed because of stale in 10 days

github-actions[bot] commented 9 months ago

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.