我有一个根 Terraform 模块,它声明了 VPC 模块和其他模块,例如要在 VPC 中启动的 EC2 实例。
\n\n在 EC2 模块中,我使用以下类型读取 VPC aws_vpc:
data "aws_vpc" "vpc" {\n filter {\n name = "tag:Name"\n values = [var.name_tag]\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n现在,如果我独立声明模块,则效果很好。
\n\n但是当声明一个单独声明这些其他模块的根模块时,我遇到了这个失败:
\n\n\xe2\x96\xb6 terraform apply\nmodule.cloudwatch.data.aws_ami.ami: Refreshing state...\nmodule.backend.data.aws_vpc.vpc: Refreshing state...\nmodule.backend.data.aws_ami.ami: Refreshing state...\n\nError: no matching VPC found\n\n on .terraform/modules/backend/main.tf line 1, in data "aws_vpc" "vpc":\n 1: data "aws_vpc" "vpc" {\nRun Code Online (Sandbox Code Playgroud)\n\n所以这里就存在先有鸡还是先有蛋的问题。
\n\n我很困惑。这怎么可能行得通呢?如果一个根模块不能既声明一个VPC,又不能在aws_vpc以后使用该数据源将其读入其他模块,那么这些数据源有什么用呢?我希望得到有关最佳实践的建议。我是否应该不使用aws_vpcVPC ID,而是将其作为输出读取到其他地方?
terraform ×1