首页 > 解决方案 > 通过 Terraform 向 ASG 注册目标

问题描述

我正在使用 Terraform 来配置应用程序负载均衡器 (ALB) 和自动缩放组 (ASG)。我设置了一个目标组,ALB 将请求转发到该目标组。但是,在我的 ASG 中启动的实例不会自动向目标组注册。我在配置中添加了以下行:

target_group_arns         = [aws_lb_target_group.example-tg.arn]

但是,在我执行 'terraform apply' 之后,我查看了已配置的基础架构,我仍然需要手动将我的实例注册到目标组。

当我执行“terraform apply”时,出现以下错误:

Error: "foobar3-terraform-test": Waiting up to 10m0s: Need at least 1 healthy instances in ASG, have 0. Most recent activity: {
  ActivityId: "e8e5c84d-93ff-6047-147e-b7f935edd18a",
  AutoScalingGroupName: "foobar3-terraform-test",
  Cause: "At 2020-05-14T13:34:14Z a user request update of AutoScalingGroup constraints to min: 1, max: 4, desired: 1 changing the desired capacity from 0 to 1.  At 2020-05-14T13:34:43Z an instance was started in response to a difference between desired and actual capacity, increasing the capacity from 0 to 1.",
  Description: "Launching a new EC2 instance: i-05ccb2f6f952bef7c",
  Details: "{\"Subnet ID\":\"subnet-0e1090a9a5ced6513\",\"Availability Zone\":\"us-east-1a\"}",
  Progress: 40,
  StartTime: 2020-05-14 13:34:45.72 +0000 UTC,
  StatusCode: "MidLifecycleAction"
}

以下是我的完整配置供参考:

  1 provider "aws" {
  2   region = "us-east-1"
  3 }
  4
  5 resource "aws_vpc" "example" {
  6   cidr_block = "10.0.0.0/16"
  7 }
  8
  9 resource "aws_security_group" "http" {
 10   name        = "http"
 11   description = "Allow HTTP inbound traffic"
 12   vpc_id      = aws_vpc.example.id
 13
 14   ingress {
 15     description = "TLS from anywhere"
 16     from_port   = 80
 17     to_port     = 80
 18     protocol    = "tcp"
 19     cidr_blocks = ["0.0.0.0/0"]
 20   }
 21
 22   egress {
 23     from_port   = 0
 24     to_port     = 0
 25     protocol    = "-1"
 26     cidr_blocks = ["0.0.0.0/0"]
 27   }
 28 }
 29
 30 resource "aws_internet_gateway" "igw" {
 31   vpc_id = aws_vpc.example.id
 32 }
 33
 34 resource "aws_default_route_table" "route_to_internet" {
 35   default_route_table_id = aws_vpc.example.default_route_table_id
 36   route {
 37     cidr_block = "0.0.0.0/0"
 38     gateway_id = aws_internet_gateway.igw.id
 39   }
 40 }
 41
 42 resource "aws_subnet" "example_subnet_1" {
 43   vpc_id               = aws_vpc.example.id
 44   cidr_block           = "10.0.1.0/24"
 45   availability_zone_id = "use1-az1"
 46 }
 47
 48 resource "aws_subnet" "example_subnet_2" {
 49   vpc_id               = aws_vpc.example.id
 50   cidr_block           = "10.0.2.0/24"
 51   availability_zone_id = "use1-az2"
 52 }
 53
 54 resource "aws_lb" "example-alb" {
 55   name               = "example-alb"
 56   internal           = false
 57   load_balancer_type = "application"
 58   security_groups    = [aws_security_group.http.id]
 59   subnets            = [aws_subnet.example_subnet_1.id, aws_subnet.example_subnet_2.id]
 60
 61   enable_deletion_protection = false
 62 }
 63
 64 resource "aws_lb_target_group" "example-tg" {
 65   name     = "example-tg"
 66   port     = 80
 67   protocol = "HTTP"
 68   vpc_id   = aws_vpc.example.id
 69 }
 70
 71 resource "aws_lb_listener" "alb-listener" {
 72   load_balancer_arn = aws_lb.example-alb.id
 73   port              = "80"
 74   protocol          = "HTTP"
 75
 76   default_action {
 77     type             = "forward"
 78     target_group_arn = aws_lb_target_group.example-tg.arn
 79   }
 80 }
 81
 82 resource "aws_autoscaling_attachment" "asg_attachment" {
 83   autoscaling_group_name = aws_autoscaling_group.bar.name
 84   alb_target_group_arn   = aws_lb_target_group.example-tg.arn
 85 }
 86
 87 resource "aws_launch_configuration" "example-lc" {
 88   name                        = "terraform-lc"
 89   image_id                    = "ami-0323c3dd2da7fb37d"
 90   instance_type               = "t2.micro"
 91   associate_public_ip_address = true
 92   user_data                   = "#!/usr/bin/env bash\nsudo amazon-linux-extras enable nginx1.12\nsudo yum -y install nginx\nsudo systemctl start nginx"
 93   security_groups             = [aws_security_group.http.id]
 94   key_name                    = "tf_example"
 95 }
 96
 97 resource "aws_autoscaling_group" "bar" {
 98   name                      = "foobar3-terraform-test"
 99   max_size                  = 4
100   min_size                  = 1
101   health_check_grace_period = 300
102   desired_capacity          = 1
103   force_delete              = true
104   launch_configuration      = aws_launch_configuration.example-lc.name
105   target_group_arns         = [aws_lb_target_group.example-tg.arn]
106   vpc_zone_identifier       = [aws_subnet.example_subnet_1.id, aws_subnet.ex    ample_subnet_2.id]
107
108   initial_lifecycle_hook {
109     name                 = "foobar"
110     default_result       = "CONTINUE"
111     heartbeat_timeout    = 2000
112     lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"
113   }
114
115   tag {
116     key                 = "name"
117     value               = "example-instance"
118     propagate_at_launch = true
119   }
120 }                      

标签: amazon-web-servicesterraform

解决方案


您在自动缩放资源块中缺少以下 health_check_type。您必须提及您使用的是 EC2 健康检查还是 ELB 健康检查。由于您的负载均衡器没有运行状况检查配置。应该是EC2。(健康检查基于实例的状态检查)

health_check_type = "EC2"

https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html


推荐阅读