Terraform Autoscaling Group - 为啥没有启动任何实例?

Posted

技术标签:

【中文标题】Terraform Autoscaling Group - 为啥没有启动任何实例?【英文标题】:Terraform Autoscaling Group - Why aren't any instances launced?Terraform Autoscaling Group - 为什么没有启动任何实例? 【发布时间】:2020-11-16 07:44:23 【问题描述】:

我正在尝试设置一个 Application Load Balancer,它指向一个使用 terraform 维护一组 webseevers 的自动缩放组,如下所示:

##################################################################
# Application Load Balancer
##################################################################

module "lb" 
  source  = "terraform-aws-modules/alb/aws"
  version = "~> 5.0"

  name               = "server-load-balancer"
  internal           = false
  load_balancer_type = "application"

  vpc_id          = module.vpc.vpc_id
  security_groups = [module.lb-security-group.this_security_group_id]
  subnets         = module.vpc.public_subnets

  //  # See notes in README (ref: https://github.com/terraform-providers/terraform-provider-aws/issues/7987)
  //  access_logs = 
  //    bucket = module.log_bucket.this_s3_bucket_id
  //  



  http_tcp_listeners = [
    # Forward action is default, either when defined or undefined
    
      port               = 80
      protocol           = "HTTP"
      target_group_index = 0
      # action_type        = "forward"
    ,

  ]



  target_groups = [
    
      # name                 = "server-alb"
      backend_protocol     = "HTTP"
      backend_port         = 8080
      target_type          = "instance"
      deregistration_delay = 10
      vpc_id               = module.vpc.vpc_id
      health_check = 
        enabled             = true
        interval            = 30
        path                = "/"
        port                = "8080"
        healthy_threshold   = 3
        unhealthy_threshold = 3
        timeout             = 6
        protocol            = "HTTP"
        matcher             = "200-299"
      
      tags = 
        InstanceTargetGroupTag = "webservers"
      
    ,
  ]

  tags = 
    Name = "Load Balancer"
  



##################################################################
# Security Groups
##################################################################

module "lb-security-group" 
  source = "terraform-aws-modules/security-group/aws"

  name        = "load-balancer-security-group"
  description = "Security group for the Application Load Balancer on the public subnet."
  vpc_id      = module.vpc.vpc_id

  ingress_cidr_blocks = ["0.0.0.0/0"]
  ingress_rules       = ["ssh-tcp", "http-80-tcp", "https-443-tcp"]
  egress_rules        = ["all-all"]
  tags = 
    Name        = "Security Group"
    Environment = var.environment_tag
  



module "autoscaler-security-group" 
  source = "terraform-aws-modules/security-group/aws"

  name        = "autoscaler-security-group"
  description = "Security group for the autoscaler on the private subnet."
  vpc_id      = module.vpc.vpc_id

  ingress_cidr_blocks = var.private_subnet_cidrs
  ingress_rules       = ["http-8080-tcp", "ssh-tcp", "http-80-tcp", "https-443-tcp"]
  egress_rules = ["all-all"]
  tags = 
    Name        = "Security Group"
    Environment = var.environment_tag
  



##################################################################
# Launch configurations and autoscaling group
##################################################################

module "autoscaler" 

  source  = "terraform-aws-modules/autoscaling/aws"
  version = "~> 3.0"

  name              = "server-autoscaler"
  enable_monitoring = true

  # Launch configuration
  #
  # launch_configuration = "my-existing-launch-configuration" # Use the existing launch configuration
  # create_lc = false # disables creation of launch configuration
  lc_name = "server-lc"

  image_id                     = data.aws_ami.server.id
  instance_type                = var.instance_type
  security_groups              = [module.autoscaler-security-group.this_security_group_id]
  target_group_arns            = module.lb.target_group_arns
  associate_public_ip_address  = false
  recreate_asg_when_lc_changes = true
  force_delete                 = false


  user_data = data.template_file.init.rendered

  root_block_device = [
    
      volume_size = "5"
      volume_type = "gp2"
    ,
  ]

  # Auto scaling group
  asg_name            = "server-asg"
  vpc_zone_identifier = module.vpc.private_subnets
  health_check_type   = "EC2"
  # Time in Millisec
  health_check_grace_period = 300
  min_size                  = 1
  max_size                  = 2
  desired_capacity          = 1
  min_elb_capacity = 1
  wait_for_capacity_timeout = 0

  # service_linked_role_arn   = aws_iam_service_linked_role.autoscaling.arn

  tags = [
    
      key                 = "Environment"
      value               = var.environment_tag
      propagate_at_launch = true
    ,
    
      key                 = "Project"
      value               = var.project
      propagate_at_launch = true
    ,
  ]

如你所见,我指定:

  # Auto scaling group
  asg_name            = "server-asg"
  vpc_zone_identifier = module.vpc.private_subnets
  health_check_type   = "EC2"
  # Time in Millisec
  health_check_grace_period = 300
  min_size                  = 1
  max_size                  = 2
  desired_capacity          = 1
  min_elb_capacity = 1
  wait_for_capacity_timeout = 0

在 autoscaling 组中,我认为这意味着将至少启动和维护 1 个实例,并且 terraform 将等待实例启动并正常运行,然后再继续。但是我发现 terraform 成功完成,但自动缩放组中没有启动任何实例,所以当我转到 URL 时,我收到 503 错误。

我在 AWS 控制台中看到我可以注册目标,但我想用 terraform 做所有事情。我查看了example on the github here,我并没有真正看到他们在做什么不同。

为什么我的自动扩缩组中没有任何实例正在启动?任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

我尝试在我的沙盒环境中启动您的代码。

我注意到,由于您使用的体积小,实例未启动。我在 ASG 控制台中遇到错误:

启动一个新的 EC2 实例。状态原因:大小 5GB 的卷小于 快照“snap-028531a83139c57d1”,预期大小 >= 8GB。启动 EC2 实例失败。

使用 8GB 足以让实例在 ASG 中启动:

  root_block_device = [
    
      volume_size = "8"
      volume_type = "gp2"
    ,
  ]

【讨论】:

@guribe94 我在 EC2 控制台、AutoScaling 组菜单、活动历史记录中找到了它们。我使用旧式控制台,而不是新式控制台。 @guribe94 好消息是您的代码在大小更改后可以正常工作。显然我必须添加很多缺失的数据才能使其正常工作(您的代码不完整),但关键问题是 5GB 大小。

以上是关于Terraform Autoscaling Group - 为啥没有启动任何实例?的主要内容,如果未能解决你的问题,请参考以下文章

将地图转换为terraform aws_autoscaling_group的列表

在 Autoscaling 预配实例上与厨师一起执行食谱

Terraform 在资源名称中使用 count.index

terraform application_security_group_ids 无效或未知密钥

terraform 自动缩放组销毁超时

使用terraform“数据”时如何重新转动多个对象?