AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1143908
Accepted
scene
scene
Asked: 2023-09-12 03:29:36 +0800 CST2023-09-12 03:29:36 +0800 CST 2023-09-12 03:29:36 +0800 CST

InvalidConfigurationRequest:负载均衡器无法附加到同一可用区中的多个子网

  • 772
这个问题是从 Stack Overflow 迁移过来的,因为它可以在服务器故障上得到回答。8 小时前迁移 。

我复制粘贴文件的一部分main.tf:

resource "aws_security_group" "servers" {
  name        = "allowservers"
  description = "Allow TCP:8080 inbound traffic to servers"
  vpc_id      = module.vpc.vpc_id

  ingress {
    from_port   = 8080
    to_port     = 8080
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_launch_configuration" "ubuntu" {
  image_id        = "ami-053b0d53c279acc90"
  instance_type   = "t2.micro"
  associate_public_ip_address = true
  security_groups = [aws_security_group.servers.id]

  user_data = <<-EOF
              #!/bin/bash
              echo "Hello, World" > index.html
              nohup busybox httpd -f -p ${var.server_port} &
              EOF
   # Required when using a launch configuration with an ASG.
  lifecycle {
    create_before_destroy = true
  }
}

data "aws_vpc" "my-vpc" {
}

data "aws_subnets" "my-vpc-subnets" {
    filter {
      name = "vpc-id"
      values = [data.aws_vpc.my-vpc.id]
    }
}

resource "aws_autoscaling_group" "ubuntu_scaling" {
  launch_configuration = aws_launch_configuration.ubuntu.name
  vpc_zone_identifier = data.aws_subnets.my-vpc-subnets.ids

  target_group_arns = [aws_lb_target_group.asg.arn]
  health_check_type = "ELB"

  min_size = 2
  max_size = 10

  tag {
    key                 = "Name"
    value               = "terraform-asg-ubuntu"
    propagate_at_launch = true
  }
}

resource "aws_lb" "ubuntu-lb" {
    name = "terraform-asg-ubuntu"
    load_balancer_type = "application"
    subnets = data.aws_subnets.my-vpc-subnets.ids
    security_groups = [aws_security_group.alb.id]
}

resource "aws_lb_listener" "ubuntu-lb-listener" {
    load_balancer_arn = aws_lb.ubuntu-lb.arn
    port = 80
    protocol = "HTTP"

    #By default, return a simple 404 page
    default_action {
      type = "fixed-response"

      fixed_response {
        content_type = "text/plain"
        message_body = "404:ups"
        status_code = 404
      }
    }
}

resource "aws_security_group" "alb" {
  name = "terraform-asg-ubuntu"
  vpc_id   = data.aws_vpc.my-vpc.id
  # Allow inbound HTTP requests
  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  # Allow all outbound requests
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_lb_target_group" "asg" {
  name     = "terraform-asg-example"
  port     = var.server_port
  protocol = "HTTP"
  vpc_id   = data.aws_vpc.my-vpc.id

  health_check {
    path                = "/"
    protocol            = "HTTP"
    matcher             = "200"
    interval            = 15
    timeout             = 3
    healthy_threshold   = 2
    unhealthy_threshold = 2
  }
}

resource "aws_lb_listener_rule" "asg" {
    listener_arn = aws_lb_listener.ubuntu-lb-listener.arn
    priority = 100

    condition {
      path_pattern {
        values = ["*"]
      }
    }
    action {
      type = "forward"
      target_group_arn = aws_lb_target_group.asg.arn
    }
}

当我运行时,它会生成terraform apply以下错误:

│ Error: creating ELBv2 application Load Balancer (terraform-asg-ubuntu): InvalidConfigurationRequest: A load balancer cannot be attached to multiple subnets in the same Availability Zone
│       status code: 400, request id: 44b2abdb-a321-4f1e-bd6f-4b87aa943477
│
│   with aws_lb.ubuntu-lb,
│   on main.tf line 172, in resource "aws_lb" "ubuntu-lb":
│  172: resource "aws_lb" "ubuntu-lb" {

我还在文件中使用vpc模块modules.tf:

module "vpc" {
  source = "terraform-aws-modules/vpc/aws"
  version = "5.0.0"
  name = "my-vpc"
  cidr = "10.0.0.0/16"

  azs             = ["us-east-1a", "us-east-1f"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24"]

  enable_nat_gateway = true
  enable_vpn_gateway = true

  tags = {
    Terraform = "true"
    Environment = "dev"
  }
}

经过大量调试,我仍然不知道如何解决这个问题。

amazon-web-services
  • 1 1 个回答
  • 15 Views

1 个回答

  • Voted
  1. Best Answer
    Marcin
    2023-09-12T11:36:44+08:002023-09-12T11:36:44+08:00

    您应该使用public_subnets以下模块vpc中的内容aws_lb:

    resource "aws_lb" "ubuntu-lb" {
        name = "terraform-asg-ubuntu"
        load_balancer_type = "application"
        subnets = module.vpc.public_subnets
        security_groups = [aws_security_group.alb.id]
    }
    
    • 0

相关问题

  • 网络定价如何在云平台中准确运作?我应该如何避免专门的定价攻击?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve