Terraform ECS EC2 ASG 如何零任务实例停止

huangapple go评论82阅读模式
英文:

Terraform ECS EC2 ASG how to zero task instance stop

问题

如何在 ECS EC2 Autoscaling 组中自动停止没有任务运行的实例?

当我在 Google 上搜索时,有人告诉我将 termination_policies 更改为 OldestInstance 选项,但这并不起作用。
英文:

autoscaling code

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

  name = "${local.name}-asg"

  security_groups = [module.autoscaling_sg.security_group_id]
  ignore_desired_capacity_changes = true

  image_id = jsondecode(data.aws_ssm_parameter.ecs_optimized_ami.value)["image_id"]
  instance_type = "t4g.small"
  key_name = "dpgg-match"

  user_data = base64encode(local.user_data)
  
  network_interfaces = [
    {
      associate_public_ip_address = true
      delete_on_termination       = true
    }
  ]

  block_device_mappings = [
    {
      device_name = "/dev/xvda"
      ebs = {
        volume_size = 40
        volume_type = "gp2"
      }
    }
  ]

  create_iam_instance_profile = true
  iam_role_name               = local.name
  iam_role_description        = "ECS role for ${local.name}"
  iam_role_policies = {
    AmazonEC2ContainerServiceforEC2Role = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
    AmazonSSMManagedInstanceCore        = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
  }

  vpc_zone_identifier = module.vpc.public_subnets
  health_check_type   = "EC2"
  min_size            = 0
  max_size            = 3
  desired_capacity    = 1

  termination_policies = ["OldestInstance"]

  autoscaling_group_tags = {
    ECS = "True"
    Terraform = "True"
  }

  tags = local.tags
}

How can I automatically stop instances with zero task running in the ECS EC2 Autoscaling Group?

When I searched on Google, I was told to change termination_policies to OldestInstance option, but it was not effective.

答案1

得分: 1

OldestInstance选项告诉ASG终止实例的顺序,但不告诉何时终止。你需要的是ECS容量提供者

> 当你使用带有管理缩放的Auto Scaling组容量提供者时,Amazon ECS会创建两个自定义CloudWatch指标和一个目标跟踪缩放策略,附加到你的Auto Scaling组上。然后,Amazon ECS根据你的任务对集群的负载来管理Auto Scaling组的缩小和扩展操作。

更多详情:https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cluster-auto-scaling.html

英文:

The OldestInstance option tells ASG in which order to terminate instances, but not when. What you are looking for is ECS Capacity Providers:

> When you use an Auto Scaling group capacity provider with managed scaling turned on, Amazon ECS creates two custom CloudWatch metrics and a target tracking scaling policy that attaches to your Auto Scaling group. Amazon ECS then manages the scale-in and scale-out actions of the Auto Scaling group based on the load your tasks put on your cluster.

More details: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cluster-auto-scaling.html

huangapple
  • 本文由 发表于 2023年2月27日 16:51:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定