【问题标题】:Add ASG instances in target group via Terraform通过 Terraform 在目标组中添加 ASG 实例
【发布时间】:2018-11-13 14:46:17
【问题描述】:

我有一个 Terraform 脚本,它创建一个启动配置、自动缩放组、一个 ALB、一个目标组和一个侦听器。使用自动缩放组启动实例。

如何在同一脚本中通过 Terraform 在目标组中添加新启动的实例?

resource "aws_launch_configuration" "CF2TF-LC" {
  name                 = "CF2TF-LC"
  depends_on           = ["aws_iam_role_policy_attachment.CF2TF-IAM-PA", "aws_security_group.CF2TF-SG-Web"]
  image_id             = "ami-14c5486b"
  instance_type        = "t2.micro"
  iam_instance_profile = "${aws_iam_instance_profile.CF2TF-IAM-IP.id}"
  key_name             = "CF2TF"
  security_groups      = ["${aws_security_group.CF2TF-SG-Web.id}"]
  user_data            = "${template_file.CF2TF-UserData.rendered}"
}

resource "aws_autoscaling_group" "CF2TF-ASG" {
  name                      = "CF2TF-ASG"
  depends_on                = ["aws_launch_configuration.CF2TF-LC"]
  vpc_zone_identifier       = ["${aws_subnet.CF2TF-Subnet-1a.id}", "${aws_subnet.CF2TF-Subnet-1d.id}"]
  max_size                  = 3
  min_size                  = 2
  health_check_grace_period = 300
  health_check_type         = "EC2"
  desired_capacity          = 2
  force_delete              = true
  launch_configuration      = "${aws_launch_configuration.CF2TF-LC.id}"
}

resource "aws_lb" "CF2TF-ALB" {
  name               = "CF2TF-ALB"
  subnets            = ["${aws_subnet.CF2TF-Subnet-1a.id}", "${aws_subnet.CF2TF-Subnet-1d.id}"]
  internal           = false
  load_balancer_type = "application"
  security_groups    = ["${aws_security_group.CF2TF-SG-Web.id}"]

  tags {
    Name        = "WebSrv"
    Environment = "Dev"
  }
}

resource "aws_lb_target_group" "CF2TF-TargetGroup" {
  name        = "CF2TF-TargetGroup"
  depends_on  = ["aws_vpc.CF2TF-VPC"]
  port        = 80
  protocol    = "HTTP"
  vpc_id      = "${aws_vpc.CF2TF-VPC.id}"
  target_type = "instance"

  health_check {
    interval            = 30
    path                = "/index.html"
    port                = 80
    healthy_threshold   = 5
    unhealthy_threshold = 2
    timeout             = 5
    protocol            = "HTTP"
    matcher             = "200,202"
  }
}

resource "aws_lb_listener" "CF2TF-ALB-Listener" {
  //depends_on = ["aws_lb.CF2TF-ALB.id", "aws_lb_target_group.CF2TF-TargetGroup.id"]
  load_balancer_arn = "${aws_lb.CF2TF-ALB.arn}"
  port              = "80"
  protocol          = "HTTP"

  default_action {
    target_group_arn = "${aws_lb_target_group.CF2TF-TargetGroup.arn}"
    type             = "forward"
  }
}

【问题讨论】:

    标签: amazon-web-services terraform terraform-provider-aws


    【解决方案1】:

    aws_autoscaling_group 资源采用 target_group_arns parameter 将 ASG 注册到目标组,以便所有实例在启动时注册到负载均衡器的目标组,并在终止之前从负载均衡器中正确排出。

    因此,您的 ASG 资源应如下所示:

    resource "aws_autoscaling_group" "CF2TF-ASG" {
      name                      = "CF2TF-ASG"
      depends_on                = ["aws_launch_configuration.CF2TF-LC"]
      vpc_zone_identifier       = ["${aws_subnet.CF2TF-Subnet-1a.id}", "${aws_subnet.CF2TF-Subnet-1d.id}"]
      max_size                  = 3
      min_size                  = 2
      health_check_grace_period = 300
      health_check_type         = "EC2"
      desired_capacity          = 2
      force_delete              = true
      launch_configuration      = "${aws_launch_configuration.CF2TF-LC.id}"
      target_group_arns         = ["${aws_lb_target_group.CF2TF-TargetGroup.arn}"]
    }
    

    【讨论】:

      【解决方案2】:

      aws_autoscaling_group 资源实现了一个参数 target_group_arns 来完全满足您的需要。只需将此行添加到您的自动缩放组资源:

      target_group_arns = "${aws_lb_target_group.CF2TF-TargetGroup.arn}"
      

      【讨论】:

      • :) 太好了!如果满足要求,记得将答案标记为正确
      • 完成...再次感谢
      猜你喜欢
      • 2020-08-31
      • 2020-12-29
      • 2019-06-21
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      相关资源
      最近更新 更多