0

I have terragrunt config where have declared the variables using locals as below at root level. In the child module , have declared the child terragrunt config file called (terragrunt.hcl). parent terragrunt file has following code :

locals {
  location = "East US"
}

child module terragrunt file has below code :

include {
  path = find_in_parent_folders()
}

locals {
  myvars = read_terragrunt_config(find_in_parent_folders("terragrunt.hcl"))
  location = local.myvars.locals.location
}

now, trying to access location variable in the terraform code (main.tf) using following code :

 location = "${var.location}"

but it throws error:

Error: Reference to undeclared input variable

  on main.tf line 13, in resource "azurerm_resource_group" "example":
  13:   location = "${var.location}"

Not getting how i can access variables defined in the terragrunt file in the terraform code . please suggest

user2315104
  • 2,378
  • 7
  • 35
  • 54

1 Answers1

2

This error message means that your root module doesn't declare that it is expecting to be given a location value, and so you can't refer to it.

In your root Terraform module you can declare that you are expecting this variable by declaring it with a variable block, as the error message hints:

variable "location" {
  type = string
}

This declaration will then make it valid to refer to var.location elsewhere in the root module, and it will also cause Terraform to produce an error if you accidentally run it without providing a value for this location variable.

Martin Atkins
  • 62,420
  • 8
  • 120
  • 138
  • I am using terrgrunt. And i need this "location" variable to be defined in root terragrunt file and then refer it to the child module of terraform. can this be possible ? If yes, any example plz – user2315104 Jul 30 '20 at 07:02
  • I'm not familiar with Terragrunt, but it seems like [inputs](https://terragrunt.gruntwork.io/docs/features/inputs/) is the feature for doing that. – Martin Atkins Jul 30 '20 at 20:09
  • Ok. Thanks ... i can make a use of inputs but now next question is , how to reference that in main.tf ? something like this : var.location (if location is defined in inputs ??) – user2315104 Jul 31 '20 at 16:49
  • The variable block I showed in my first answer is what will make `var.location` work in your root module, as long as you can configure Terragrunt to populate that variable with a value. – Martin Atkins Jul 31 '20 at 23:26