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
Using if
meta attribute instead of count
for single-instance conditional resource
#30221
Comments
if
meta attribute instead of count
if
meta attribute instead of count
for single-instance conditional resource
Hi @sandipndev, I think there are other issues where this was discussed in the past, but it doesn't hurt to revisit and see if all of the previous assumptions/conclusions still hold. First, I want to note that if Terraform is behaving correctly then you should not need to use However, it seems like your stronger concern here is the need to refer to index zero when using the resource elsewhere in the module. An important part of declaring a conditional number of instances of a resource is explaining to Terraform how to handle different numbers of instances elsewhere in your module. For example, consider: resource "aws_instance" "example" {
count = condition ? 1 : 0
} It's only valid to refer to resource "aws_instance" "example" {
if = condition
} With all of that said then, it's been a common theme in proposals of this sort to propose a syntax for declaring the condition (an In previous discussions some have suggested making If you have any other thoughts about what By the way, in the meantime if you want to make it clearer to future maintainers of your module that you intend there to be only zero or one of something, you can use the You do still need to figure out how to contend with the resulting
|
I wrote a broader blog post on this topic, and have gotten a lot of positive feedback. I hope you'll consider how those of us who are daily TF developers and module writers have to interact with the software. |
much needed feature |
I'm glad to find this, I was considering filing this exact enhancement request. While I understand and agree with the desire to have a better way to refer to these types of "optional" resources, I think we shouldn't let a lack of good way to improve that stop us from improving how these resources are declared. Using |
Would a possible way forward simply be: And then make use of depends_on, for resources that have no direct dependencies? If the child resource tries to create, it checks the parent, see's the flag set, issues a warning that it will not create as the parent is not enabled and then does nothing. |
Maybe I'm pushing TF in a direction it's not designed for, but it's hard to communicate how much frustration the lack of this feature causes when trying to create consistency across development stages whilst also having some infra modules not deployed to some environments. Especially since if you try to retrospectively introduce a count meta-argument, since terraform often then wants to recreate all those resources (I realise it tries to move them, but this doesn't always work). It's my top WTF item since delving into devops. I understand where the language developed from and why this doesn't exist, but it's now impossible to create consistent but differing environments with anything close to DRY code. For me I only care about pre-determined feature flags (e.g. set in a |
Probably duplicate of #21953 |
The world is a messy place, with changing requirements and a multitude of technical and non-technical reasons. I've been using terraform for a long time and find myself in a new shop, completely retro-fitting all their services and teams with infrastructure-as-code and modern devops practices. Having used Conditional logic is always going to be the fastest and simplest way to "gate" something from an environment you really can't have it running, in a code-base you really can't be dramatically modifying for one small workaround. We all want perfectly factored, beautiful codebases managing clean, immutable infrastructure.... but a lot of the world is not there yet and needs some additional time and help getting there. Help like "basic I find my lazy workaround is |
Hi again all! Lately I've noticed that this request is captured in a few different issues, which is causing the design discussion to get fragmented. I'm going to close this one in favor of #21953 because that one has the most existing discussion and the most existing 👍 upvotes. If you currently have an upvote attached to this issue then I suggest transferring it over to #21953 so we can track all of that in a central place. 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. |
The Problem
When we search for "Conditional Resource creation in Terraform", we always find the answer:
While this seems simple enough, I have faced issues numerous times with this syntax. Whenever we have to make a given resource optional, which was already deployed without using
count
, we always have toterraform state mv
to migrate the state to index0
of this resource, now modified usingcount
.Moreover, if we want to deploy ONE single instance of a resource, it doesn't really make sense to have a
resource[0]
on the state. It might make someone analysing the state think that the resource should or might have multiple copies while in reality, the developer just wanted to deploy the resource conditionally.Proposal
I think the following syntax would be pretty nice to have:
This should be implemented in such a manner that if the
if
clause returns truthy, only then should the resource be created, conforming tocount
and all other meta args. By default, without count, the resource should not be indexed and theif
clause will just serve as a boolean gate for whether or not the resource will be created.I understand that many resources could have used "if" as an argument to the resource definition itself, but this could be solved by placing the if conditional inside the
lifecycle
hook, probably?I have felt that
count = conditional ? 1 : 0
was being used in many places where a simpleif = condition
would do. Just wanted to have a discussion on this. Also, is there already a solution to this problem and I'm missing out?What are your opinions?
The text was updated successfully, but these errors were encountered: