-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Inability to dynamically create resources via conditionals #11566
Comments
This would be extremely useful for me as well. I was trying to add a toggle between using an aws ELB or ALB (using
EDIT: actually, a better example is with the
|
Right... the problem here is that the interpolation language isn't currently able to "short-circuit" the evaluation of the conditional branch that doesn't end up being used. This is a known limitation of the language evaluator currently, and one I'd definitely like to fix. Thanks for capturing this in an issue! |
Thank you for taking it into consideration. I look forward to having this capability! |
I've filed hashicorp/hil#50 to track the root issue here; the "hil" codebase is where the interpolation language infrastructure lives. |
I found a usable workaround to this issue. It unblocked us, so maybe it can work for others too. It's based on the fact that resources which have a count attribute can be applied splat syntax I know this is very ugly and downright abusive to HCL, but it does work around the problem. Ideally a fix upstream in HIL will render this hack useless. |
Seeing the same issue here too. Help! :D |
The workaround of using the "splat" syntax is the best path for the immediate term, though I totally understand that it is ugly and counter-intuitive. Others have reported success with it, and it's become a common idiom while we work through this problem. On a more positive note, we're working right now on a revamp of the configuration language that includes, among many other fixes and improvements, a proper solution to this problem. Given the scope of the changes we will be rolling this out carefully, first via an opt-in experimental version to gather feedback on some of the more significant changes. The new work here changes a few different things that related to this issue:
We'll have more info on this once it gets closer to being released. As noted above, the first release will be an opt-in, experimental version so we can gather feedback and address any big issues before we switch to the new implementation. However, we are definitely motivated to address these issues as quickly as possible since we know that these assorted inconsistencies all add up to making Terraform feel clumsy to use and make Terraform configurations hard to maintain as a result. |
I've also had this issue, with an additional twist: If the reference to the missing resource is part of a conditional that sets the value of a map item, then there is a silent failure but interpolation of other items in the map fails. ie in the example below, used as output, "fixedItemID" is set to an empty string rather than the interpolated value
|
Bump to this issue; as seen with the many related Issues/PR, it's amazing that a year later the latest interpolation parser hasn't been pushed. variable "file_path" {
default = ""
}
locals {
content = "${var.file_path != "" ? file(var.file_path) : "initial content here"}"
} It's pretty annoying that this doesn't work as expected and that both side of the ternary operation are actually interpreted. ps: for those interested,
similar to the solution found here: #15605 |
@apparentlymart I think I'm hitting this or a very similar problem of Terraform evaluating the entire interpolation rather than just the "true" part in this issue hashicorp/terraform-provider-aws#3744 |
It's been almost a year and a half since a fix to the configuration language was mentioned. This item is still open. Update??? |
The ternary operator issue it's fixed in HCL2, there is a beta version
available but it's not our of Beta I believe..
…On Thu, Mar 7, 2019, 18:51 MMarulla ***@***.***> wrote:
It's been almost a year and a half since a fix to the configuration
language was mentioned. This item is still open. Update???
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#11566 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAaEQq0noS4SNpEfTQCm_br6KK0HUVmCks5vUVGKgaJpZM4Ly9Ag>
.
|
@paddie is it part of the latest RC1? |
@JustinGrote (and everyone else): yes, short-circuit conditional evaluation is in terraform 0.12. You can test it now in terraform 0.12.0-rc1 - but please do not use a pre-release with real production infrastructure! |
Confirmed, this plus the "null" value opens up a LOT more scenarios to make
modules more friendly with intelligent defaults and "plug-in" objects.
Already looking forward to hopefully for_each and count for modules in
0.13+!
…On Tue, May 21, 2019 at 11:43 AM Kristin Laemmert ***@***.***> wrote:
@JustinGrote <https://github.com/JustinGrote> (and everyone else): yes,
short-circuit conditional evaluation is in terraform 0.12. You can test it
now in terraform 0.12.0-rc1
<https://releases.hashicorp.com/terraform/0.12.0-rc1/> - but please do
not use a pre-release with real production infrastructure!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#11566?email_source=notifications&email_token=ADUNKUUOVOV5AJSGNIHOP4TPWQ7DHA5CNFSM4C6L2AQKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODV4Z7JQ#issuecomment-494509990>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ADUNKUUDFBEMJ5OM4NM52ALPWQ7DHANCNFSM4C6L2AQA>
.
|
🎉 I am going to close this issue; the original bug is fixed and working as expected in Terraform 0.12. Thanks for the great conversation! |
The use of count like this is really a hack, especially as true / false are no longer cast to 1 and 0. Ideally there needs to be a feature request, for a true conditional resource (ie resource only created if resource.CreateResource is 'true' ) Currently I have to set count to the result of a conditional statement that has the value 0 if false, and the number of items configured in the environment if true. I'm controlling features of the environment independently to the number of items that would be created if the feature was enabled. |
Hi @thallam08! Please open a new GH issue and use the feature request template to request an enhancement - the more specific your example use-case, the better. Thanks 🎉 |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
Hi,
I was trying to leverage conditionals to create resources dynamically but it seems it's not actually possible. Please advise on whether I am missing something here or I'm doing something wrong.
Assume there is one
tfvars
per environment. When thevar.environment
is set todevelopment
, everything works fine as expected. The Virtual Private Gateway is created as expected (and successfully destroyed when not needed anymore).But when the
var.environment
is set tostaging
orproduction
, runningterraform plan
will produce:Looks like Terraform is expecting the VPN Gateway resource to exist when in reality it shouldn't.
To rephrase: the VPN Gateway resource will have
count = 0
when thevar.environment
is set tostaging
orproduction
, and it should pick thevar.vgw_propagated_list
variable instead. By doing so, I am effectively declaring a dynamic resource which would be created based on the environment you're working on. But it's not working.Is this expected? Am I doing something wrong?
Thank you all.
The text was updated successfully, but these errors were encountered: