snippetterraformMinor
How to get VPC id in Terraform module?
Viewed 0 times
modulevpcgethowterraform
Problem
I have a module structure like
when running am getting an error like
can anyone please help ?
module "vpc" {
source = "./modules/vpc"
}
module "prod_subnets" {
source = "./modules/vpc/modules/subnets/production"
}when running am getting an error like
[0m on modules/vpc/modules/subnets/production/production.tf line
162, in resource "aws_subnet" "prod-pub-1b":
14:22:55 162: vpc_id = [4maws_vpc.production_vpc[0m.id
14:22:55 [0m
14:22:55 A managed resource "aws_vpc" "production_vpc" has not been declared in
14:22:55 prod_subnets.can anyone please help ?
Solution
Without seeing the contents of your two modules I'm guessing a bit, but it looks like you have an AWS VPC declared in your
If so, the answer is that the
In your
In your
Then in your existing
Finally, you must then edit the top-level file whose source code you shared in your question to pass the value between the two modules, like this:
vpc module and some subnets declared in your prod_subnets module and you are asking how the configuration of the subnets can get access to the VPC ID.If so, the answer is that the
vpc module must export the VPC ID as an output value and then the prod_subnets module must accept the VPC ID as an input variable.In your
vpc module, you can declare a vpc_id output value like this, for example in a file modules/vpc/outputs.tf:output "vpc_id" {
value = aws_vpc.production_vpc
}In your
prod_subnets module you can declare a vpc_id input variable, for example in a file modules/vpc/modules/subnets/production/outputs.tf:variable "vpc_id" {
type = string
}Then in your existing
modules/vpc/modules/subnets/production/production.tf file, on line 162, change the vpc_id argument for the subnet to refer to that variable:vpc_id = var.vpc_idFinally, you must then edit the top-level file whose source code you shared in your question to pass the value between the two modules, like this:
module "vpc" {
source = "./modules/vpc"
}
module "prod_subnets" {
source = "./modules/vpc/modules/subnets/production"
vpc_id = module.vpc.vpc_id
}module.vpc.vpc_id means to take the value of the vpc_id output value from the vpc module.Code Snippets
output "vpc_id" {
value = aws_vpc.production_vpc
}variable "vpc_id" {
type = string
}vpc_id = var.vpc_idmodule "vpc" {
source = "./modules/vpc"
}
module "prod_subnets" {
source = "./modules/vpc/modules/subnets/production"
vpc_id = module.vpc.vpc_id
}Context
StackExchange DevOps Q#11228, answer score: 3
Revisions (0)
No revisions yet.