问题
I have a root Terraform module that declares a VPC module and other modules such as an EC2 instance that is to launch in the VPC.
In the EC2 module, I read the VPC using the aws_vpc
type:
data "aws_vpc" "vpc" {
filter {
name = "tag:Name"
values = [var.name_tag]
}
}
Now this works fine if I declare the modules independently.
But when declaring a root module that declares these other modules separately, I get this failure:
▶ terraform apply
module.cloudwatch.data.aws_ami.ami: Refreshing state...
module.backend.data.aws_vpc.vpc: Refreshing state...
module.backend.data.aws_ami.ami: Refreshing state...
Error: no matching VPC found
on .terraform/modules/backend/main.tf line 1, in data "aws_vpc" "vpc":
1: data "aws_vpc" "vpc" {
So there is a chicken/egg problem here.
I am confused. How can this ever work? If a root module cannot both declare a VPC and then use the aws_vpc
data source later to read it into other modules, what is the use of these data sources? I would appreciate advice on the best practice here. Should I simply not use aws_vpc
and instead read in the VPC ID as an output elsewhere?
回答1:
To me this sounds like you are declaring both a resource like
resource "aws_vpc" "example" {}
AND data-provider like
data "aws_vpc" "example" {}
in order to access something from the data like data.aws_vpc.example.arn
. This is not needed and in fact is causing your error. If both is in the same terraform state, you can simply drop the data "aws_vpc" "example" {}
and refer to the resource by e.g. resource.aws_vpc.example.arn
.
The data provider is actually only needed in cases in which you are referring to a resource that is created somewhere else like something created manually, by a different provisioning engine (or also by terraform, but in a different layer).
回答2:
You have not mentioned your query very specifically.
As far as I understand from your question, you have declared VPC in root module and want to use it's id or arn, etc from this, right?
So, in this scenario, you must have specify the perfect path of your vpc module in (.) format (e.g. module.root.aws_vpc.vpc_name.id) and also you can use
depends_on = [your vpc ]
in your data resource declaration.
NOTE: I don't have required reputation here to suggest you these things as in comments, I may also loos my reputations here by answering you as a answer here.
Request: Please mention more details here, also paste your module tree structure for better understanding.
you can refer this link : https://www.terraform.io/docs/providers/aws/d/vpc.html
来源:https://stackoverflow.com/questions/62213317/terraform-chicken-egg-problem-using-aws-vpc-data-source-in-root-module