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

Sample code to extract variable value from hcl #41

Open
irfanjs opened this issue Jul 28, 2020 · 11 comments
Open

Sample code to extract variable value from hcl #41

irfanjs opened this issue Jul 28, 2020 · 11 comments
Labels

Comments

@irfanjs
Copy link

irfanjs commented Jul 28, 2020

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
Copy link
Author

irfanjs commented Jul 30, 2020

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

@brikis98
Copy link
Member

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

@irfanjs
Copy link
Author

irfanjs commented Jul 31, 2020

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
Copy link
Author

irfanjs commented Aug 1, 2020

please suggest

@yorinasub17
Copy link
Contributor

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?

@irfanjs
Copy link
Author

irfanjs commented Aug 1, 2020

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
Copy link
Contributor

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
Copy link
Contributor

yorinasub17 commented Aug 1, 2020

E.g. in your terragrunt.hcl,

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

@irfanjs
Copy link
Author

irfanjs commented Aug 1, 2020

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
Copy link
Contributor

Yes that is correct.

@irfanjs
Copy link
Author

irfanjs commented Aug 1, 2020

ok Thanks

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