user2315104
user2315104

Reputation: 2722

How to access terragrunt variables in terraform code

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

Upvotes: 1

Views: 3003

Answers (1)

Martin Atkins
Martin Atkins

Reputation: 74229

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.

Upvotes: 1

Related Questions