Terragrunt: Remote state key

Created on 18 Jul 2018  路  7Comments  路  Source: gruntwork-io/terragrunt

I have configured remote state key path in root terrafrom.tfvars (key = "${path_relative_to_include()}/terraform.tfstate") . This works perfectly, I get key paths as

  • dev/us-east-1/vpc/terraform.tfstate
  • dev/us-east-1/iam/terraform.tfstate

However, I hard coded these paths in account.tfvars to use the remote state of modules - something like below;
vpc_remote_state_key = "dev/us-east-1/vpc/terraform.tfstate"
iam_remote_state_key = "dev/us-east-1/iam/terraform.tfstate"

and used these variables in modules to get the values using (data "terraform_remote_state").

My question is about the values which I passed to vpc_remote_state_key, iam_remote_state_key. Is there a way or method which can get rid of the hard coded value?? - something like below
vpc_remote_state_key = "{path()}/vpc/terraform.tfstate"
iam_remote_state_key = "{path()}/iam/terraform.tfstate"

question

All 7 comments

What about this?

vpc_remote_state_key = "${var.env}/${var.region}/${var.service}/terraform.tfstate"

You can achieve this by having an env.tfvars on dev/ level with:
env="dev"

and a region.tfvars on dev/us-east-1 level
region="us-east-1"

You can even get further and add a service.tfvars on dev/us-east-1/vpc level
service="vpc"

On root terraform.tfvars though you will have to include:
terraform { optional_var_files = [ "${get_tfvars_dir()}/${find_in_parent_folders("env.tfvars", "ignore")}", "${get_tfvars_dir()}/${find_in_parent_folders("region.tfvars", "ignore")}", "${get_tfvars_dir()}/${find_in_parent_folders("service.tfvars", "ignore")}", ] }

Thats how I do it but I would love to get feedback.

Won't this increase the additional maintenance? and could go against DRY?
Do we have something similar to ${path_relative_to_include()}? but just returns the header path to module, something as ${path_relative_to_include_header()}, and provides dev/us-east-1, in that case It can be used in the arguments? right?

arguments = [
        "-var",
        "vpc_remote_state_key=${path_relative_to_include_header()}/vpc/terraform.tfstate"

@brikis98 any thought on this?

I'm not aware of a way to make that much more DRY currently.

I am in the same situation.
My terragrunt.hcl fil saves my state in a GCP bucket gs://infrastructure/network/shared-vpc.
But i do not really know how i would go about to use terraform_remote_state to access outputs in a parent directory (gs://infrastructure/network).

Did you find any solution @lambertpan ?

One idea: as of v0.19.4, Terragrunt supports all Terraform built-in functions. Perhaps you can figure out a way to use those to do what you want? See also https://github.com/gruntwork-io/terragrunt/issues/303#issuecomment-510309532 for interesting ways these functions can be combined.

@brikis98
Thanks a million! I just put this in my terragrunt.hcl file in order to find the parent state file in my bucket.

inputs = {
  bucket_parent_path = "${path_relative_to_include()}/${dirname(find_in_parent_folders())}"
}
Was this page helpful?
0 / 5 - 0 ratings