'Terraform refer to locals in another directory

I have a directory name "3-tier" In which there are two more directories namely ec2 and vpc. The exact directory structure is as attached below

enter image description here

The main.tf in vpc directory has a locals block defined:

locals {
    owner = var.owner
    environment = var.environment
    Name = "${local.owner}-${local.environment}-${var.vpc_name}"
    common_tags = {
        Owner = local.owner
        Environment = local.environment
        Name = local.Name
    }
}

In the ec2 directory main.tf file I have loaded the module as below

module "vpc_module" {
    source = "../vpc/"
}

How can I refer to the locals present in vpc directory main.tf file from ec2 directory main.tf file?



Solution 1:[1]

How can I refer to the locals present in vpc directory main.tf file from ec2 directory main.tf file?

You can't do this directly. You have to add them as output values to your vpc module. Then you will be able to access them through outputs of your vpc module.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcin