Hey,
I have a two folder structure named as “alb” and “cloudfront” I have to use the output value of “alb” dns_name in “cloudfront” main.tf file as input, because I have to update the dns value in cloudfront distribution.
Welcome to the Terraform forums!
See the following example for AWS that passes variables from the parent to the child and from the child to the parent and other modules.
Parent calls module aws_network_vpc and passes values aws_vpc_block and aws_vpc_tag_name:
module "aws_network_vpc" {
source = "./modules/aws/network/vpc"
aws_vpc_block = var.aws_vpc_block
aws_vpc_tag_name = var.aws_vpc_tag_name
}
Module accepts values from calling parent
variable "aws_vpc_tag_name" {
description = "Name of the VPC"
}
variable "aws_vpc_block" {
description = "Private IP block for the VPC in CIDR format"
}
Actual module uses vars:
resource "aws_vpc" "default" {
cidr_block = var.aws_vpc_block
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = var.aws_vpc_tag_name
}
}
Actual module exports values to calling parent:
output "id" {
value = aws_vpc.default.id
}
output "vpc_main_route_table_id" {
value = aws_vpc.default.main_route_table_id
}
Exported values are acceded with construction module.name_of_module.variable, example for our previous module aws_network_vpc we need the exported ID of the VPC to create an Internet Gateway.
Parent uses values exported from module:
#Create an Internet GW
module "aws_internet_gw" {
source = "./modules/aws/network/internet_gateway"
vpc_id = module.aws_network_vpc.id
name = var.aws_internet_gw_name
}
Full example and code at: