簡體   English   中英

由 auto-scaling 組中的啟動模板創建的 ec2 實例未在目標組中注冊

[英]ec2 instances created by a launch template in auto-scaling group are not being registered with the target group

這些是我的 alb 配置,以備不時之需。 跳下來查看問題的實質。

resource "aws_autoscaling_attachment" "asg_attachment" {
  autoscaling_group_name = aws_autoscaling_group.web.id
  lb_target_group_arn   = aws_lb_target_group.main.arn
}

resource "aws_lb" "main" {

  name               = "test-${var.env}-alb"
  internal           = false
  load_balancer_type = "application"

  security_groups = [aws_security_group.elb.id]
  subnets         = [aws_subnet.main1.id, aws_subnet.main2.id]

  tags = {
    Name        = "${var.project_name}-${var.env}-alb"
    Project     = var.project_name
    Environment = var.env
    ManagedBy   = "terraform"
  }
}

resource "aws_lb_target_group" "main" {
  name                 = "${var.project_name}-${var.env}-alb-tg"
  port                 = 80
  protocol             = "HTTP"
  vpc_id               = aws_vpc.main.id
  deregistration_delay = 30
  health_check {
    interval = 10
    matcher  = "200-299"
    path     = "/"
  }
 
}

resource "aws_lb_listener" "main" {
 
  load_balancer_arn = aws_lb.main.arn
  protocol          = "HTTPS"
  port              = "443"
  ssl_policy        = "ELBSecurityPolicy-TLS-1-2-2017-01"
  certificate_arn   = aws_acm_certificate.main.arn
 
  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.main.arn
  }

}

============

我有一個通過 terraform 創建的自動縮放組:

resource "aws_autoscaling_group" "web" {
  vpc_zone_identifier = [aws_subnet.main1.id]

  launch_template {
    id = aws_launch_template.web.id
    version = "$Latest"
  }
  min_size             = 1
  max_size             = 10

  lifecycle {
    create_before_destroy = true
  }
}

啟動模板如下所示:

resource "aws_launch_template" "web" {
  name_prefix            = "${var.project_name}-${var.env}-autoscale-web-"
  image_id               = var.web_ami
  instance_type          = "t3.small"
  key_name               = var.key_name
  vpc_security_group_ids = [aws_security_group.webserver.id]
}

如果我使用啟動配置而不是啟動模板,它會起作用:

resource "aws_launch_configuration" "web" {
  image_id      = var.web_ami
  instance_type = "t3.small"
  key_name = var.key_name
  security_groups = [aws_security_group.webserver.id]
  root_block_device {
    volume_size = 8 # GB
    volume_type = "gp3"
  }
}

使用啟動配置時,添加了 autoscaling_group 中的一行:

launch_configuration = aws_launch_configuration.web.name

並且刪除了 launch_template 部分。

aws_launch_configuration 已棄用,所以我想使用 launch_template。

一切正常; 實例旋轉起來,我可以連接到它並且它通過了健康檢查。 問題是 EC2 實例不會自動注冊到目標組。 當我手動將其注冊到目標組時,一切正常。

如何讓使用啟動模板啟動的 EC2 實例自動添加到目標組?

結果 aws_autoscaling_attachment 也被棄用了,我需要添加:

target_group_arns = [aws_lb_target_group.main.arn]

到我的 aws_autoscaling_group。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM