gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
8.04k stars 975 forks source link

undeclared resource error using a dependency #1730

Closed dratspiker closed 3 years ago

dratspiker commented 3 years ago

When running terragrunt plan in /spoke07/subnet/, I get the following undeclared resource error after following these instructions for setting up a dependency. I believe it is because I do not have the variable from the subnet.tf file mapped back to the corresponding terragrunt.hcl correctly but I have not been able to figure out the fix. I have confirmed that terragrunt output at /spoke07/vpc/ outputs the vpc-id.

│ Error: Reference to undeclared resource
│
│   on subnet.tf line 16, in resource "aws_subnet" "tanium-subnet1":
│   16:   vpc_id = dependency.vpc.outputs.vpc_id
│
│ A managed resource "dependency" "vpc" has not been declared in the root
│ module.

/spoke07/subnet/terragrunt.hcl

locals {
  environment_vars = read_terragrunt_config(find_in_parent_folders("env.hcl"))
  region_vars      = read_terragrunt_config(find_in_parent_folders("region.hcl"))

  env    = local.environment_vars.locals.environment
  region = local.region_vars.locals.aws_region
}

terraform {
  source = "${get_parent_terragrunt_dir()}/tf-spoke/subnet/"
}

dependency "vpc" {
  config_path = "../vpc"
}

include {
  path = find_in_parent_folders()
}

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

/tf-spoke/vpc/outputs.tf

output "vpc_id" {
  value = aws_vpc.tanium-spoke.id
  }

/tf-spoke/subnet/subnet.tf

resource "aws_subnet" "tanium-subnet1" {
  assign_ipv6_address_on_creation = "false"
  cidr_block                      = var.subnet1-cidr-block
  map_public_ip_on_launch         = "true"
  availability_zone               = var.subnet1-az

  vpc_id = dependency.vpc.outputs.vpc_id
}

Directory structure.

.
├── spoke07
│   ├── account.hcl
│   ├── env.hcl
│   ├── region.hcl
│   ├── subnet
│   │   └── terragrunt.hcl
│   └── vpc
│       └── terragrunt.hcl
├── terragrunt.hcl
└── tf-spoke
    ├── subnet
    │   ├── outputs.tf
    │   ├── subnet.tf
    │   └── vars.tf
    └── vpc
        ├── outputs.tf
        ├── vars.tf
        └── vpc.tf
yorinasub17 commented 3 years ago

It looks like you are referencing the dependency resource in terraform. dependency is a terragrunt construct and only available in terragrunt.hcl. The idea is to feed the dependency to the terraform module using variable inputs. So in this case, you want to replace whereever you were referencing dependency in your terraform module with a variable, and have terragrunt set that variable in the inputs map in the terragrunt.hcl.

dratspiker commented 3 years ago

Thank you for your help. I guess I need to figure out how to set the variable from the outputs of the vpc module. I am learning terraform and terragrunt at the same time so I sometimes get lost in the interplay between the two.