I am using Terraform to provision an application load balancer (ALB) and an autoscaling group (ASG). I have a target group set up, and the ALB forwards requests to that target group. However, my instances launching in my ASG are not automatically registered with the target group. I added the following line to my configuration:
target_group_arns = [aws_lb_target_group.example-tg.arn]
However, after I execute 'terraform apply', I look at the infrastructure that has been provisioned, and I still need to manually register my instance with the target group.
When I execute 'terraform apply', I get the following error:
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"
}
Below is my full configuration for reference:
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 }