首页 > 解决方案 > 使用 terraform 的 AWS Autoscaling 配置出错

问题描述

我正在尝试使用 AWS Autoscaling and Launch 配置设置自动缩放环境。

下面是我用于启动配置的 tfvar

config_name = "name"
image_id = "ami-test"
instance_type = "c4.large"
key_name = "EC2-key"
security_groups = ["sg-123456789",
    "sg-123456789099"]
associate_public_ip_address = false
enable_monitoring = true
ebs_optimized = true
root_size = 10
root_volume_type = "standard"
root_encrypted = true
device_name = "/dev/sdf"
ebs_volume = 30
ebs_delete = true
ebs_encrypted = true
ebs_volume_type = "gp2"
iam_instance_profile = "arn:aws:iam::1234567890:instance-profile/EC2ROLE"

这是创建一个没有任何问题的配置,从控制台创建的配置和这个 tfvar 执行几乎相似。

下面是自动缩放组的 tfvars。

scaling_name = "EC2-Scaling"
vpc_zone_identifier = ["subnet-123456789", "subnet-asdfghfjk"]
max_size = 2
min_size = 1
health_check_type = "ELB"
launch_configuration = "name"
termination_policies = ["NewestInstance",
    "OldestLaunchConfiguration"]
enabled_metrics = ["GroupInServiceCapacity",
    "GroupMaxSize",
    "GroupTotalCapacity",
    "GroupTotalInstances",
    "GroupMinSize"]
health_check_grace_period = 300
policy_name = "autoscaling_policy"

在控制台中签入时,这也显示得很好。但是,当伸缩组尝试启动实例时,它会抛出如下错误。

Launching a new EC2 instance: i-21358239842. Status Reason: Instance became unhealthy while waiting for instance to be in InService state. Termination Reason: Client.InternalError: Client error on launch

请指出我正在做的一些错误,或者我错过了什么。

正如评论中指出的,这是资源类。

resource "aws_launch_configuration" "launch_configuration" {
  name = var.config_name
  image_id = var.image_id
  instance_type = var.instance_type
  key_name = var.key_name
  security_groups = var.security_groups
  associate_public_ip_address = var.associate_public_ip_address
  enable_monitoring = var.enable_monitoring
  ebs_optimized = var.ebs_optimized
  
  root_block_device {
    volume_size = var.root_size
    volume_type = var.root_volume_type
    encrypted = var.root_encrypted
  }
  
  ebs_block_device {
    device_name = var.device_name
    volume_size = var.ebs_volume
    delete_on_termination = var.ebs_delete
    encrypted = var.ebs_encrypted
    volume_type = var.ebs_volume_type
  }
  iam_instance_profile  = var.iam_instance_profile
}


resource "aws_autoscaling_group" "autoscaling" {
  name = var.scaling_name
  vpc_zone_identifier        = var.vpc_zone_identifier  
  max_size = var.max_size
  min_size = var.min_size
  health_check_type = var.health_check_type
  launch_configuration = var.launch_configuration
  termination_policies = var.termination_policies
  enabled_metrics = var.enabled_metrics
  
  instance_refresh {
    strategy = "Rolling"
  }
  
  health_check_grace_period = var.health_check_grace_period
  wait_for_capacity_timeout = 0 ##Skips waiting for capacity and proceeds to create a scaling group
}

resource "aws_autoscaling_policy" "dynamic_scaling" {
  name                   = var.policy_name
  adjustment_type        = "ChangeInCapacity"
  autoscaling_group_name = aws_autoscaling_group.autoscaling.name
  policy_type            = "TargetTrackingScaling"

  target_tracking_configuration {
    predefined_metric_specification {
      predefined_metric_type = "ASGAverageCPUUtilization"
    }
    target_value = 40.0
  }
}

目前我正在考虑在这两种解决方案中的任何一种中解决这个问题。

正如@Arun K 所提到的,设置带有运行状况检查的 ALB 以将请求转发到自动缩放组或环运行状况检查到此

标签: amazon-web-servicesamazon-ec2terraformautoscaling

解决方案


来自 aws_autoscaling_group 的 terraform 手册:

wait_for_capacity_timeout(默认值:“10m”)Terraform 在超时前等待 ASG 实例健康的最长持续时间。(另请参阅下面的等待容量。)将此设置为“0”会导致 Terraform 跳过所有容量等待行为。

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group

从 ec2 错误来看,我认为它是不健康的,因为它还不能通信0 秒对于 ec2 实例从初始化到 inService 的时间太短,将在 terraform 中触发“aws_autoscaling_group”资源后进行检查。如果我是 Web 用户(或健康检查)点击当前正在初始化的 ec2 实例,我会得到 500,而不是 500-but-ec2-will-be-span-up-soon-try-again-in-一分钟。在resource "aws_autoscaling_group" "autoscaling"中,尝试给它一个值:

wait_for_capacity_timeout = 300 

我根据您的其他值设置了它:

health_check_grace_period = 300

所以这个值意味着它将在 ec2 实例发出服务信号后等待 300 秒,然后再进行健康检查。


推荐阅读