Terraform导入嵌套模块中AWS路由表失败,请求技术指导
解决Terraform导入嵌套模块中路由表的问题
问题原因
你执行的导入命令使用了根模块的资源地址aws_route_table.main,但实际aws_route_table.main定义在嵌套模块module.aws.module.route_table里,Terraform找不到对应地址,因此报错。
正确的导入步骤
使用嵌套模块的资源地址执行导入
在项目根目录运行以下命令:terraform import module.aws.module.route_table.aws_route_table.main rtb-12345这个地址对应你的模块层级:根模块 →
aws模块 →route_table模块 →aws_route_table.main资源。确保数据源配置正确
你的route_table/main.tf中使用了data.aws_vpc.default.id,需要保证这个数据源能正确获取到现有VPC的ID,否则导入后执行terraform plan会出现资源不匹配的错误。可以补充数据源配置:# 在route_table/main.tf中添加 data "aws_vpc" "default" { # 方式1:通过VPC名称过滤 filter { name = "tag:Name" values = ["你的现有VPC名称"] } # 方式2:直接指定VPC ID(更可靠) # vpc_id = "vpc-xxxxxxxxx" }验证导入结果
导入完成后,执行terraform plan检查配置与现有资源是否匹配。如果输出显示No changes. Your infrastructure matches the configuration.,说明导入成功,此时就可以在aws_route_table.main中添加VPC对等连接的路由了:resource "aws_route_table" "main" { vpc_id = data.aws_vpc.default.id route { cidr_block = "另一个VPC的CIDR段" vpc_peering_connection_id = aws_vpc_peering_connection.your_peering_connection.id } }
内容的提问来源于stack exchange,提问作者J_3507
相关产品推荐
相关产品推荐

