gruntwork-io / terragrunt-infrastructure-live-example

A repo used to show examples file/folder structures you can use with Terragrunt and Terraform
https://www.gruntwork.io/
Apache License 2.0
776 stars 467 forks source link

Sample code to extract variable value from hcl #41

Open irfanjs opened 4 years ago

irfanjs commented 4 years ago

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

irfanjs commented 4 years ago

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

brikis98 commented 4 years ago

Did you look at the corresponding https://github.com/gruntwork-io/terragrunt-infrastructure-modules-example?

irfanjs commented 4 years ago

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
}
irfanjs commented 4 years ago

please suggest

yorinasub17 commented 4 years ago

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:

I suspect you are looking for the second way?

irfanjs commented 4 years ago

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

yorinasub17 commented 4 years ago

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.

yorinasub17 commented 4 years ago

E.g. in your terragrunt.hcl,

generate "common_vars" {
  path      = "common_vars.tf"
  if_exists = "overwrite_terragrunt"
  contents = <<EOF
variable "instance_type" {}
EOF
}
irfanjs commented 4 years ago

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 ?

yorinasub17 commented 4 years ago

Yes that is correct.

irfanjs commented 4 years ago

ok Thanks