AWS Terraform 向目标组注册目标时出错

AWS Terraform Error registering targets with target group

附加时出现错误 aws_alb_target_group_attachment。 InvalidTarget: 以下目标未处于 运行 状态,无法注册

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 个错误:

在这种情况下,我将使用启动模板(或启动配置)资源以及自动缩放资源,指定您希望为目标组启动的 Spot 实例的数量;

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
    }
  ]
}