Terragrunt: Fetching output of nested terraform source

Hello, I’m trying to grep/fetch the output of nested terraform source

terraform    {
   source  = "../${path_relative_from_include()}/../Resources/Network//${path_relative_to_include()}"
}

Since I don’t have the name of module, in used way like module.vpc I’m not able to get the output of my nested block, as well I can not do simple:

output "vpc_id" {
    value = module.vpc.id
}

So, can someone help me to get, output of nested block? At the end I would like to pass the output to inputs block, so my other network related resources could use the vpc_id, something like this:


terragrunt.hcl

terraform    {
   source  = "../${path_relative_from_include()}/../Resources/Network//${path_relative_to_include()}"
}


inputs     = {
   expecting_id = nested.block.id
}

As far as I know, you cannot pull values from a nested source. The common pattern is to pass-through outputs from a deeper module, to the outer module. Here’s what I think you’re trying to do. Please let me know if I’m misunderstanding.

./terragrunt.hcl

dependency "vpc_with_ext" {
  config_path = "../vpc_with_extensions"
}

terraform {
  source = "path/to/module/that/needs/vpc_id"
}

inputs = {
  vpc_id = dependency.vpc_with_ext.outputs.vpc_id
}

vpc_with_extensions/main.tf

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

# other resources, etc

output "vpc_id" {
  value = module.vpc.vpc_id
}

vpc/main.tf

# declare provider

resource "aws_vpc" "my_vpc" {
  # omitted for brevity
}

output "vpc_id" {
  value = my_vpc.vpc_id
}

Hi, @rhobot thank you, for your replay.

Not exactly, what I really want to do, is to basically get output of module that were created by following block:

terraform    {
   source  = "../${path_relative_from_include()}/../Resources/Network//${path_relative_to_include()}"
}

And if you’re saying, that I can not in any of ways pull the output of block (block is above), maybe there’re some workarounds? Of course with out changing directory and file hierarchy?

I would be glad to get any help

I think what you’re asking must be possible, but I am missing the picture. Could you share your code, redacting anything private/sensitive, so that I can see exactly what you’re talking about? Please include as much as you can.

Hello, @rhobot yes, sure let me show my project


To begin with, the file structure

├── local.tf
├── main.tf
├── maps.tf
├── provider.tf
└── terragrunt.hcl

main.tf contains all network related resources, which are expecting the vpc_id
Network resources such as:

  • Subnets
  • Gateways
  • Route Tables
  • Access Control Lists
  • Security Groups

Here is the content of terragrunt.hcl

terraform    {
   source     = "../${path_relative_from_include()}/../Resources/Network//${path_relative_to_include()}"
}

locals       {
   # * Main Terragrunt
   inherits    = read_terragrunt_config(find_in_parent_folders("init.hcl"))


   # * Common Variables
   region      = local.inherits.locals.consolidate.zone.one.region
   owners      = local.inherits.locals.owners
   states      = local.inherits.locals.states
   tables      = local.inherits.locals.tables
   account     = local.inherits.locals.account
   outline     = local.inherits.locals.outline

   bucket    = "${local.account}--${local.owners}-storage--${local.states}--${local.region}"
   dynamo    = "${local.account}--${local.owners}-dynamodb--${local.tables}--${local.region}"
   tfpath    = "terraform/${local.states}/${local.outline}/terraform.tfstate"
}

include      {
   # * Include Other Options
   path        = find_in_parent_folders()
}


inputs     = {
     vpc_id = unkown <== Need real vpc id
}

@unity-unity Thanks for the additional info. I would have expected you to have a folder structure more like this: please see the diagram for terragrunt’s path_relative_from_include. Therefore, I would expect the root terragrunt.hcl to have this block:

terraform    {
   source = "../${path_relative_from_include()}/../Resources/Network//${path_relative_to_include()}"
}

And the child terragrunt.hcls to have this block:

include      {
   path = find_in_parent_folders()
}

The child terragrunt.hcl’s also seem like the right place to put the inputs block for the vpc_id.

Assuming a set up like this, you can use the dependency block in your child terragrunt.hcl files. In your child terragrunt.hcls, it should be clear where the VPC module exists relative to that path. You set that as the config_path in the dependency block, and the input can use that dependency.my_vpc.outputs.vpc_id as the input. Like so:

dependency "my_vpc" {     # whatever label you use here will be part of the dependency path below
  config_path = "../vpc"
}

inputs = {
  vpc_id = dependency.my_vpc.outputs.vpc_id
}

Read about this here: passing outputs between modules. Now Terragrunt knows it must deploy the VPC module first before the modules that depend on that output. And you don’t have to worry too much about the relative pathing from the root terragrunt.hcl file because the inputs are being passed at the child level.