Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to inherit parent terragrunt.hcl file's inputs block #1011

Closed
yagehu opened this issue Jan 20, 2020 · 5 comments
Closed

How to inherit parent terragrunt.hcl file's inputs block #1011

yagehu opened this issue Jan 20, 2020 · 5 comments
Labels

Comments

@yagehu
Copy link

yagehu commented Jan 20, 2020

In my top-level parent terragrunt.hcl, I have the following block. Taken directly from the terragrunt example repo:

inputs = merge(
  yamldecode(
    file("${get_terragrunt_dir()}/${find_in_parent_folders("region.yaml", local.default_yaml_path)}"),
  ),
  yamldecode(
    file("${get_terragrunt_dir()}/${find_in_parent_folders("env.yaml", local.default_yaml_path)}"),
  ),
  {
    aws_profile = "prod"
  },
)

How can I inherit env, region, and aws_profile in my child terragrunt.hcl file and use them as variables?

$ tree
.
├── terragrunt.hcl
└── us-west-2
    ├── region.yaml
    └── staging
        ├── dynamodb
        │   ├── backend.tf
        │   └── terragrunt.hcl
        └── env.yaml
@yorinasub17
Copy link
Contributor

inputs from the parent are automatically merged to the child when you add the following block to the child's terragrunt.hcl:

include {
  path = find_in_parent_folders()
}

You can read more about the inheritance properties of terragrunt here: https://terragrunt.gruntwork.io/use-cases/keep-your-remote-state-configuration-dry/#filling-in-remote-state-settings-with-terragrunt

@yagehu
Copy link
Author

yagehu commented Jan 21, 2020

Thanks for the prompt response! Let's say my child terragrunt.hcl file looks like this:

terraform {
  source = "git::https://github.com/cloudposse/terraform-aws-ssm-parameter-store?ref=0.1.5"
}

include {
  path = find_in_parent_folders()
}

inputs = {
  parameter_write = [
    {
      name        = "/ctx-staging/dynamodb/tables"    // <----- I want to replace staging with a variable
      value       = data.terraform_remote_state.dynamodb.outputs.arn
      type        = "String"
      overwrite   = "true"
      description = "DynamoDB book table ARN"
    }
  ]
}

Is there a way to replace staging somehow with the environment variable merged into the child?

Also, side question: I can't seem to use data.terraform_remote_state here as I can't add a data block to terragrunt.hcl. How do people usually reference other modules' output?

@yorinasub17
Copy link
Contributor

yorinasub17 commented Jan 21, 2020

Unfortunately, you can't reference other variables in other config yet. See #814 for the thread on this topic.

As a workaround, you will have to do the merge in terraform. E.g you might add a variable name_suffix and environment, and then set environment to staging in env.yaml. Then, you will construct the name in terraform (NOT terragrunt.hcl) as "/ctx-${var.environment}${var.name_suffix}".

Also, side question: I can't seem to use data.terraform_remote_state here as I can't add a data block to terragrunt.hcl. How do people usually reference other modules' output?

Terragrunt is not terraform code, so it does not support all the resources and data sources. I am currently working on reference docs to make it easier to discover all the supported features of terragrunt configuration, but in the meantime, you will have to go through the feature list in the doc site to find what you need. For this use case, see this section.

@yagehu yagehu closed this as completed Jan 21, 2020
@yorinasub17
Copy link
Contributor

I wrote up an RFC that introduces import blocks. This has the potential to address the use case described here, so would love feedback from those following this issue to see if it makes sense.

@ina-stoyanova
Copy link
Contributor

inputs from the parent are automatically merged to the child when you add the following block to the child's terragrunt.hcl:

include {
  path = find_in_parent_folders()
}

You can read more about the inheritance properties of terragrunt here: https://terragrunt.gruntwork.io/use-cases/keep-your-remote-state-configuration-dry/#filling-in-remote-state-settings-with-terragrunt

Just updating one of the links to point to the latest guide on the webpage.

https://terragrunt.gruntwork.io/docs/features/keep-your-remote-state-configuration-dry/#filling-in-remote-state-settings-with-terragrunt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants