gruntwork-io/terragrunt-infrastructure-live-example

Sample code to extract variable value from hcl

irfanjs opened this issue · 11 comments

Hi,
Can we have sample code (.tf) file to understand how these variable values can be floated to terraform code. ? I see in this repo all hcl files and their structure all along the child folders . However how these variables can be easily extracted in code is not shown.

Please add couple code examples

Thanks

folks , please suggest . This is blocker for me so any help would be much appreciated

Yes , i looked at the above link and now more confused !!! sorry to say that

Simply, what I am looking for is, how to define the terragrunt.hcl file with all common variables and use them in all the child modules. In above example/link, haven't seen any terragrunt file (neither in root module , nor in any child module ) .

in above example, all the variables are coming from variables.tf file but I am expecting to get it from terragrunt.hcl file and use in main.tf file .

Like you said, we can define variables like below in terragrunt file , now can we use these variables in main.tf ? If yes, please give me example.

inputs = {
  foo = "bar"
  baz = 3
}

please suggest

Terragrunt is a wrapper around terrraform where it automates machinery around passing CLI args, vars, and env vars to terraform, so you need to get the variables defined in terraform somehow. The two ways to do this are:

  • Statically define them in the terraform module using variables.tf. This is the canonical and recommended way to do this, as it keeps your terraform module reusable and easy to run in other platforms that doesn't assume terragrunt. This is what we are doing in the examples here.
  • Generate the variables.tf file from terragrunt using generate blocks. This is typically not recommended because it means your terraform module can only be used with terragrunt (or some other preprocessor that generates terraform code).

I suspect you are looking for the second way?

Thanks for the reply.
Let me try to explain

lets say , i have following code in my terragrunt.hcl

inputs = {
  instance_type  = "t2.micro"
  instance_count = 10

  tags = {
    Name = "example-app"
  }
}

now my question is , can i use variable instance_type in terraform (main.tf) ?
if yes, please let me know how ? Thanks

can i use variable instance_type in terraform (main.tf) ?

Yes, assuming you have the variable "instance_type" block in terraform for that variable, which you can define either by having that in the module, or by using generate block to generate it, as mentioned in my previous comment.

E.g. in your terragrunt.hcl,

generate "common_vars" {
  path      = "common_vars.tf"
  if_exists = "overwrite_terragrunt"
  contents = <<EOF
variable "instance_type" {}
EOF
}

regarding generate block , let me understand more and come back

regarding other option of defining variables in terraform, if i understand correctly here is what i can do :

in terragrunt.hcl

inputs = {
  instance_type  = "t2.micro"
  instance_count = 10

  tags = {
    Name = "example-app"
  }
}

and in the module's variable.tf file , i can have something like this :

variable "instance_type" {}

and then in main.tf file , i can use this variable like :
var.instance_type
is this correct ?

Yes that is correct.

ok Thanks