Skip to content

Design pattern to avoid Warning: Value for undeclared variable ? #873

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

Closed
rgarrigue opened this issue Sep 23, 2019 · 3 comments
Closed

Design pattern to avoid Warning: Value for undeclared variable ? #873

rgarrigue opened this issue Sep 23, 2019 · 3 comments
Labels

Comments

@rgarrigue
Copy link

Hi guys

I'm setting up a new live repository for a new job after 2 years. Read Terragrunt stuff again, here's the beginning of my organization (some stuff are missing, still need to be done etc)

development
├── aws
│   ├── eu-west-1
│   │   ├── bastion
│   │   │   ├── ha_instance
│   │   │   │   └── terragrunt.hcl
│   │   │   ├── README.md
│   │   │   └── security_group
│   │   │       └── terragrunt.hcl
│   │   ├── domain
│   │   │   ├── main.tf
│   │   │   ├── outputs.tf
│   │   │   └── terragrunt.hcl
│   │   ├── haproxy
│   │   │   └── terragrunt.hcl
│   │   ├── key_pair
│   │   │   └── terragrunt.hcl
│   │   ├── regional.auto.tfvars
│   │   └── vpc
│   │       └── terragrunt.hcl
│   ├── _global
│   │   ├── identities
│   │   │   ├── admin
│   │   │   │   ├── account
│   │   │   │   │   ├── terraform.tfvarscame 
│   │   │   │   │   └── terragrunt.hcl
│   │   │   │   └── role
│   │   │   │       ├── data.tf
│   │   │   │       ├── terraform.tfvars
│   │   │   │       └── terragrunt.hcl
│   │   │   └── README.md
│   │   └── regional.auto.tfvars
│   ├── provider.tf
│   └── terragrunt.hcl
└── environment.auto.tfvars

At the provider (AWSes fodlers) level I've a terragrunt.hcl like this

terraform {
  # Copy relevant variables in cache folder. They are ignored by git via .gitignore.
  # It's done on provider level since this is where the remote_state block is needed, hence where the top level terragrunt.hcl should be
  after_hook "copy_environment_vars" {
    commands = ["init-from-module"]
    execute  = ["cp", "${find_in_parent_folders("environment.auto.tfvars")}", "."]
  }

  after_hook "copy_provider_resources" {
    commands = ["init-from-module"]
    execute  = ["cp", "${find_in_parent_folders("provider.tf")}", "."]
  }

  after_hook "copy_regional_vars" {
    commands = ["init-from-module"]
    execute  = ["cp", "${find_in_parent_folders("regional.auto.tfvars")}", "."]
  }

  after_hook "workaround_terragrunt_issue_749" {
    commands = ["init"]
    execute  = ["rm", "-f", "${get_terragrunt_dir()}/environment.auto.tfvars", "${get_terragrunt_dir()}/provider.tf","${get_terragrunt_dir()}/regional.auto.tfvars"]
  }
}

My problem is, I'm defining in environment.auto.tfvars variables like domain, since I'm not using all of those everywhere, Terraform gives warning (soon to be errors) about undefined variables.

Warning: Value for undeclared variable

on regional.auto.tfvars line 2:
2: aws_region = "us-east-1"

The root module does not declare a variable named "aws_region". To use this
value, add a "variable" block to the configuration.

Using a variables file to set an undeclared variable is deprecated and will
become an error in a future release. If you wish to provide certain "global"
settings to all configurations in your organization, use TF_VAR_...
environment variables to set these instead.

What should I do, ship a workaround_warning_undeclared_global_vars.tf everywhere ? Feels ugly :-/

@yorinasub17
Copy link
Contributor

See #303 (comment) and #303 (comment) for the current recommended workaround.

We also have had discussions around improvements to terragrunt itself, discussed here: #744 (comment)

@rgarrigue
Copy link
Author

I've been quickly reading those refs. I'm waiting for the globals { } I guess. Since the recommended "live" organization is a tree structure, a way to define metadata at each level with downward overloading so one can have a merged relevant result on the leaf, is clearly needed.

Meanwhile I'll go for the yamldecode I guess.

@LozanoMatheus
Copy link

Hi @rgarrigue not sure if you found your way to solve this, but I may have something that can help you. Basically, I'm using the generate to fix the var warnings.

/
├── global_vars.yaml
├── terragrunt.hcl
└── eks_cluster
    ├── generated_empty_vars.tf
    ├── main.tf
    ├── generated_provider.tf
    └── terragrunt.hcl

In the root terragrunt.hcl, I added this 👇 so TG will generate the file during the runtime and before running TF command.

locals {
  common_inputs = yamldecode(file(find_in_parent_folders("global_vars.yaml")))
  inputs        = merge(local.common_inputs)
}

inputs = local.inputs

generate "empty_vars" {
  path      = "generated_empty_vars.tf"
  if_exists = "overwrite"
  contents  = join("\n", [for s in keys(local.common_inputs) : "variable ${s} {}"])
}

This is what I've in the global_vars.yaml (the vars' values will be filled via inputs)

var001: "value001"
var002: "value002"
var003: "value003"
var004: "value004"

And this is the content of the generated_empty_vars.tf (created via generate "empty_vars"):

# Generated by Terragrunt. Sig: nIlQXj57tbuaRZEa
variable var001 {}
variable var002 {}
variable var003 {}
variable var004 {}

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