-
-
Notifications
You must be signed in to change notification settings - Fork 998
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
new interpolation function to get values from var files #603
Comments
Could you not create a |
@brikis98 I don't think that having In particular, if the terraform modules don't follow the same naming conventions (for example, if they are open source modules written by different owners), e.g. if a set of modules expects a variable called An other example of where I can't see what you're proposing achieving what I'm aiming for is the case of a layered architecture. For example, a module might have the concept of a client and service account id. And another module might have the same concept. But the actual config for the client id in the 2nd module is the same account as the service account id for the first module. If you think I'm missing somethig in the solution you're proposing, then by all means please clarify. I'd love to find a better way than what I'm currently doing, which is to achieve this mapping in a little python script and including the generated tfvars file in the terragrunt config. I think that if it's somehting that other users would like to use, it would be best handled by terragrunt itself. If you're happy it's worthy enhancement, I might be able to submit a PR for this. |
Thanks for explaining the use case more! It makes sense, and I agree it can't be handled via |
@brikis98 I've created a test repo with terraform files that I think describe the use cases we want this new interpolation function to cover. use case 2: get_tfvar_from_var_files_with_include_1 and use case 3: get_tfvar_from_var_files_with_include_2 we didn't explicitly talk about, but I think it feels natural to read mapped values from parent Finally, use case 4: get_tfvar_from_var_files_with_include_3 is similar to use case 1, but I wanted to double-check that you would expect that var files defined with Please cast your eyes on those use cases to make sure we're on the same page. Thanks! |
This example contains: arguments = [
"-var-file=${get_tfvars_dir()}/../env1.tfvars",
"-var", "foo=${get_tfvar_from_var_files("bar")}" # should set foo to "bar_value"
] How does
Terragrunt currently only supports one level of
Not sure I follow this one. |
First of all regarding use case 3 and several levels of include, it isn't something I did before (I guess obviously now, since it isn't supported). I thought it was supported so was trying to be comprehensive regarding the use cases. So let's forget about that one. So back to use case 1 and the options you have identified...
So, to summarize, I think that what we should go for is a blend of option 1 and 2 where both Having had a look at the code, I was planning on achieving this by doing a two-pass interpolation of the terragrunt configuration. |
Hm, having thought about it, it seems a bit weird to parse these values from
Moreover, later, when maintaining this code, you won't be able to tell where a variable comes from simply by looking at the It seems more straightforward to be explicit about this and just require the user to tell us what |
You might be right. I quite liked (and still do) the thought of being able to specify the var files in one place (a parent terraform.tfvars file) instead of having to specify locations of So ok, let's go for your solution of providing a |
@marionettist, this is not what you want, but it might help you to implement what you want in a different way: #670 |
You can now use |
I would like to propose the following enhancement to terragrunt.
My team and I have been using terragrunt for about a year now and we like
the value it adds on top of terraform :)
The issue I have is that when writing generic terraform modules that are re-used
by several terragrunt configurations, the names in the module are and should be generic.
However, we also like to define some variables such as account ids in a single place.
That's why we put them in a file at the top level of the tree (in vcs), but at the moment
I believe that there is no way of getting hold of those variables' values and they
have to be included the terraform.tfvars file invoked by terragrunt (or included in a file generated by another tool - which is what we are doing).
So let's assume that I have defined the following in
global_vars.json
:Le'st also assume we have a terraform module that requires 2 variables to be set:
source_account_id
andtarget_account_id
.I would like a way for terragrunt to invoke the module and set the value of
source_account_id
andtarget_account_id
to the values ofbusiness_unit_1_account_id
and
service_account_id
for example.So I propose adding an interpolation function
get_tfvar_from_var_files(var_name)
that walksthe files specifed in the
-var-file
argument, in the order they're specifiedlooking for a variable called
var_name
and returning its value.If several variables with the same name are encountered, the value of the last variable is used.
If the variable is not found, an error is raised.
The terragrunt config would look like something like:
To set variable
source_account_id
, terragrunt would walk through files../../../global_vars.json
,../../level2.json
and../level3.json
looking for variablebusiness_unit_1_account_id
.It would find it in
../../../global_vars.json
and set its value to2345678901
This would allow for another configuration based on that module to be deployed easily,
by amending the following lines:
If the ids for the account change, they can be changed in a single place and the stacks
reapplied.
The text was updated successfully, but these errors were encountered: