Hi team,
I 've a root.tf
module "vpc_staging" {
source = "./vpc_staging"
}
module "security-group" {
source = "terraform-aws-modules/security-group/aws"
version = "1.25.0"
name = "sg"
description = "Security group for n/w with needed ports open within VPC"
vpc_id = "${module.vpc_staging.vpc_id}"
}
I can very well access the "${module.vpc_staging.vpc_id}" here in this case.
However when I do.
module "vpc_staging" {
source = "./vpc_staging"
}
module "sg" {
source = "./sg
}
And the security_group/main.tf has the same configuration as earlier case, it doesn't work.
module "security-group" {
source = "terraform-aws-modules/security-group/aws"
version = "1.25.0"
name = "sg"
description = "Security group for n/w with needed ports open within VPC"
vpc_id = "${module.vpc_staging.vpc_id}"
}
I get the error
Error: module 'security-group': unknown module referenced: vpc_staging
Error: module 'security-group': reference to undefined module "vpc_staging"
So how do I interpolate variables across modules keeping my root.tf clean?
If I understand correctly you have a root configuration that uses 2 modules and you want resources from module A to be accessible in module B? In that case you must define values as outputs from module A then pass them in to module B. Variables are not shared globally across modules so values must be defined as outputs or variables for each module.
So in your case in your security-group module should specify vpc_id as a variable. Then in security_group/main.tf would look like:
variable "vpc_id" {}
module "security-group" {
source = "terraform-aws-modules/security-group/aws"
version = "1.25.0"
name = "sg"
description = "Security group for n/w with needed ports open within VPC"
vpc_id = "${var.vpc_id}"
}
Then in your root.tf you pass it in from your vpc module
module "vpc_staging" {
source = "./vpc_staging"
}
module "sg" {
source = "./sg
vpc_id = "${module.vpc_staging.vpc_id}"
}
Thanks @aburnett, it worked.
How to use if I've some data in other modules and I want to use it in main.tf.
For example,
/modules/network.tf contains below data, and I want to use the output of the show-network in other main.tf
netework.tf
data "network_domain" "network" {
network_domain = "${var.network_domain}"
}
output "show-network" {
value = "${data.network_domain. network .network_domains}"
}
main.tf
module "network_domains_data" "getNetworkDomain" {
source = "../module/"
network_domain = "${var.network_domain}"
}
output "listDomains" {
value = // "here I want to use the output of the network.tf"
}
Hello @devshah1012
First of all, you can't have multiple names for a module
To access output available in main.tf, your code should be like this
output "show-network" {
value = "${data.network_domain. network .network_domains}"
}
main.tf
module "network_domains_data" {
source = "../module/"
network_domain = "${var.network_domain}"
}
output "listDomains" {
value = "${module.network_domains_data.show-network}"
}
I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Most helpful comment
If I understand correctly you have a root configuration that uses 2 modules and you want resources from module A to be accessible in module B? In that case you must define values as outputs from module A then pass them in to module B. Variables are not shared globally across modules so values must be defined as outputs or variables for each module.
So in your case in your
security-groupmodule should specifyvpc_idas a variable. Then in security_group/main.tf would look like:Then in your root.tf you pass it in from your vpc module