Terraform报错:data.aws_subnets不支持vpc_id参数求助
问题排查与修复建议
核心错误修复:aws_subnets 数据块参数错误
你遇到的Unsupported argument错误,根源是Terraform AWS Provider中aws_subnets数据资源不支持直接用vpc_id作为参数,必须通过filter块筛选指定VPC下的子网。
修正后的aws_subnets数据块代码:
data "aws_subnets" "default" { filter { name = "vpc-id" values = [data.aws_vpc.default.id] } }
其他潜在错误修复
除上述核心问题外,代码还有几处必须修正的点,否则会触发新的报错:
1. 缺失实例安全组资源
aws_launch_configuration.example中引用了aws_security_group.instance.id,但你只定义了负载均衡用的安全组,需添加实例专属安全组:
resource "aws_security_group" "instance" { name = "terraform-example-instance" ingress { from_port = var.server_port to_port = var.server_port protocol = "tcp" security_groups = [aws_security_group.alb.id] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } }
2. 子网列表引用方式过时
新版Terraform中,data.aws_subnets.default.*.id这种引用语法已被弃用,需替换为标准属性ids:
- 修正负载均衡的子网配置:
subnets = data.aws_subnets.default.ids
- 修正自动扩缩容组的可用区子网配置:
vpc_zone_identifier = data.aws_subnets.default.ids
3. AMI ID地域匹配检查
你使用的ami-04a7c24c015ef1e4c是首尔地域(ap-northeast-2)的Amazon Linux 2镜像,需确保provider配置的region确实为ap-northeast-2,后续切换地域时需同步更新对应地域的AMI ID。
修复后完整代码
provider "aws" { access_key = "*****************" secret_key = "**********************************" region = "ap-northeast-2" } data "aws_vpc" "default" { default = true } data "aws_subnets" "default" { filter { name = "vpc-id" values = [data.aws_vpc.default.id] } } variable "server_port" { description = "The port the server will use for HTTP requests" type = number default = 80 } resource "aws_security_group" "alb" { name = "terraform-example-alb" ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } } resource "aws_security_group" "instance" { name = "terraform-example-instance" ingress { from_port = var.server_port to_port = var.server_port protocol = "tcp" security_groups = [aws_security_group.alb.id] } 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.default.id health_check { path = "/" protocol = "HTTP" matcher = "200" interval = 15 timeout = 3 healthy_threshold = 2 unhealthy_threshold = 2 } } resource "aws_lb" "example" { name = "terraform-asg-example" load_balancer_type = "application" subnets = data.aws_subnets.default.ids security_groups = [aws_security_group.alb.id] } resource "aws_lb_listener" "http" { load_balancer_arn = aws_lb.example.arn port = 80 protocol = "HTTP" default_action { type = "fixed-response" fixed_response { content_type = "text/plain" message_body = "404: page not found" status_code = 404 } } } resource "aws_lb_listener_rule" "asg" { listener_arn = aws_lb_listener.http.arn priority = 100 condition { path_pattern { values = ["*"] } } action { type = "forward" target_group_arn = aws_lb_target_group.asg.arn } } resource "aws_launch_configuration" "example" { image_id = "ami-04a7c24c015ef1e4c" instance_type = "t2.micro" security_groups = [aws_security_group.instance.id] user_data = <<-EOF #!/bin/bash echo "Hello, World" > index.html nohup busybox httpd -f -p ${var.server_port} & EOF lifecycle { create_before_destroy = true } } resource "aws_autoscaling_group" "example" { launch_configuration = aws_launch_configuration.example.name vpc_zone_identifier = data.aws_subnets.default.ids target_group_arns = [aws_lb_target_group.asg.arn] health_check_type = "ELB" min_size = 2 max_size = 3 tag { key = "Name" value = "terraform-asg-example" propagate_at_launch = true } } output "alb_dns_name" { value = aws_lb.example.dns_name description = "The domain name of the load balancer" }
内容的提问来源于stack exchange,提问作者Sunny
相关产品推荐
相关产品推荐

