Getting the error while attaching aws_alb_target_group_attachment. InvalidTarget: The following targets are not in a running state and cannot be registered
resource "aws_alb" "rancher-ctl-host-alb" {
name = "rancher-ctl-host-alb"
internal = false
load_balancer_type = "application"
ip_address_type = "ipv4"
subnets = ["${data.aws_subnet_ids.vpc_subnets.ids}"]
security_groups = [
"${data.terraform_remote_state.core.rancher-ctl-host-alb-sg-id}"
]
}
resource "aws_alb_listener" "rancher-ctl-host-alb-listener" {
load_balancer_arn = "${aws_alb.rancher-ctl-host-alb.arn}"
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = "${aws_alb_target_group.rancher-ctl-host-target-group.arn}"
}
}
resource "aws_alb_target_group" "rancher-ctl-host-target-group" {
name = "rancher-ctl-host-target-group"
port = 8080
protocol = "HTTP"
vpc_id = "${data.terraform_remote_state.core.default-vpc-id}"
}
resource "aws_alb_target_group_attachment" "rancher-ctl-host-target-group-instances" {
target_group_arn = "${aws_alb_target_group.rancher-ctl-host-target-group.arn}"
target_id = "${aws_spot_instance_request.rancher-ctl-host.*.spot_instance_id[count.index]}"
port = 8080
count = 3
}
aws_alb_target_group_attachment.rancher-ctl-host-target-group-instances[2]: 1 error(s) occurred:
aws_alb_target_group_attachment.rancher-ctl-host-target-group-instances.2: Error registering targets with target group: InvalidTarget: The following targets are not in a running state and cannot be registered: 'i-0791f6bee8a082a10' status code: 400, request id: 990ee63b-3053-11e9-92bd-4d5e8013e613
aws_alb_target_group_attachment.rancher-ctl-host-target-group-instances[0]: 1 error(s) occurred:
aws_alb_target_group_attachment.rancher-ctl-host-target-group-instances.0: Error registering targets with target group: InvalidTarget: The following targets are not in a running state and cannot be registered: 'i-0b8c1f6d35f57c5cb' status code: 400, request id: 990f0dbc-3053-11e9-bbd2-3b6b83537945
aws_alb_target_group_attachment.rancher-ctl-host-target-group-instances[1]: 1 error(s) occurred:
aws_alb_target_group_attachment.rancher-ctl-host-target-group-instances.1: Error registering targets with target group: InvalidTarget: The following targets are not in a running state and cannot be registered: 'i-033d01746af85be03' status code: 400, request id: 990f3424-3053-11e9-aa19-bf12dffa0d2b
In this case, I'd use a launch template (or launch config) resource along with an autoscaling resource that specifies the number of spot instances you want spun up for the target group;
resource "aws_launch_template" "general" {
name = ""
image_id = data.aws_ami.recent_ami.image_id
ram_disk_id = null
instance_type = "m5.large"
key_name = "YOUR-INSTANCE-KEY"
vpc_security_group_ids = [""]
iam_instance_profile = {
iam_instance_profile_name = ""
iam_instance_profile_arn = null
}
market_type = "spot"
ebs = {
volume_size = 30
volume_type = "gp2"
ebs_delete_on_termination = true
encrypted = false
kms_key_id = null
snapshot_id = null
iops = null
}
spot_options = {
block_duration_minutes = null
max_price = "0.1700"
#stop interruption behavior not supported with "one time" spot instance
instance_interruption_behavior = "terminate"
spot_instance_type = "one-time"
# duration can't be set when using with Auto Scaling Group
# valid_until = "2021-12-02T10:00:00-05:00"
valid_until = null
}
user_data = filebase64("${path.module}/files/deploy.sh")
tags = {
Name = "your-launch-template-name",
CreatedBy = "you"
}
}
resource "aws_autoscaling_group" "general" {
name = "your-asg-name"
launch_configuration = null
launch_template = {
# REFERENCE YOUR LAUNCH TEMPLATE HERE:
id = aws_launch_template.general.id
lc_version = "$Latest"
}
min_size = 1
max_size = 3
desired_capacity = 3
health_check_grace_period = 600
health_check_type = "EC2"
default_cooldown = 240
# REFERENCE YOUR TARGET GROUP HERE:
target_group_arns = [aws_alb_target_group.rancher-ctl-host-target-group.arn]
vpc_zone_identifier = local.private_subnets
tags = [
{
key = "Name"
value = "your-asg-name"
propagate_at_launch = true
},
{
key = "Env"
value = "test"
propagate_at_launch = true
},
{
key = "Createdby"
value = "you"
propagate_at_launch = true
}
]
}