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

Terraform Autoscaling Group - Why aren't any instances launced?

我正在尝试设置一个指向自动缩放组的应用程序负载均衡器,该组维护一组使用 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

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

我在 AWS 控制台中看到我可以注册目标,但我想使用 Terraform 完成所有操作。我查看了 example on the github here,并没有真正看到他们所做的任何不同。

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

我试图在我的沙盒环境中启动你的代码。

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

Launching a new EC2 instance. Status Reason: Volume of size 5GB is smaller than snapshot 'snap-028531a83139c57d1', expect size >= 8GB. Launching EC2 instance failed.

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

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