我复制粘贴文件的一部分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"
}
}
经过大量调试,我仍然不知道如何解决这个问题。
您应该使用
public_subnets
以下模块vpc
中的内容aws_lb
: