-
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
"Variables may not be used here" for prevent_destroy
#22544
Comments
Hi @tomasaschan,
|
Just ran into this but with a "normal" variable. It would be create if we can use variables in the |
I'm hitting this, too. Please allow variables derived from static values to be used in
|
Seen multiple threads like this. There is an ongoing issue (#3116) which is currently open but @teamterraform seem to have made that private to contributors only. Our modules need to be capable of having lifecycle as variables. |
My use case is very much like @weldrake13's. It would be nice to understand why this can't work. |
I would also appreciate if Terraform allows variables for specifying "prevent_destroy" values. As a workaround, since we use the S3 backend for managing our Terraform workspaces, I block the access to the Terraform workspace S3 bucket for the Terraform IAM user in my shell script after Terraform has finished creating the prod resources. This effectively locks down the infrastructure in the workspace and requires a IAM policy change to re-enable it. |
I write tests for my modules. I need to be able to re-run tests over and over. There's no way for me to delete buckets in a test account and set protection in a production account. Swing and a miss on this one. |
Is there a general issue open with Terraform to improve conditional support? Off the top of my head I can think of the following limitations:
All of these make writing enterprise-level Terraform code difficult and more dangerous. |
The same of: #3116 |
Hashicorp locked down 3116. If this gets closed then those following cant view the issue. |
It's over 4 years since #3116 was opened, I think we'd all appreciate some indication of where this is? Is it still waiting on the proposal mentioned in this comment, #4149 ? Thought I'd offer up a work around I've used in some small cases. Example here is a module for gcloud sql instance, where obviously in production I want to protect it, but more ephemeral environments I want to be able to pull the environment down without editing the code temporarily. It's not pretty but it works, and is hidden away in the module for the most part: ### variables.tf
variable "conf" {
type = map(object({
database_version = string
...
prevent_destroy = string
}))
description = "Map of configuration per environment"
default = {
dev = {
database_version = "POSTGRES_9_6"
...
prevent_destroy = "false"
}
# add more env configs here
}
}
variable "env" {
type = string
description = "Custom environment used to select conf settings"
default = "dev"
}
### main.tf
resource "google_sql_database_instance" "protected" {
count = var.conf[var.env]["prevent_destroy"] == "true" ? 1 : 0
...
lifecycle {
prevent_destroy = "true"
}
}
resource "google_sql_database_instance" "unprotected" {
count = var.conf[var.env]["prevent_destroy"] == "false" ? 1 : 0
...
lifecycle {
prevent_destroy = "false"
}
}
### outputs.tf
output "connection_string" {
value = coalescelist(
google_sql_database_instance.protected.*.connection_name,
google_sql_database_instance.unprotected.*.connection_name,
)
description = "Connection string for accessing database"
} Module originated prior to 0.12, so those conditionals could well be shortened using |
It is so funny. I am asking this question WHY? WHY? |
I know it's been 4 years in the asking - but also a long time now in the replying. Commenting on #3119 was locked almost 2 years ago saying "We'll open it again when we are working on this". Can someone with the inner knowledge of this "feature" work please step up and give us some definitive answers on simple things like:
Thanks for your work - Hashicorp - this tool is awesome! Not slanting at you, just frustrated that this feature is languishing and I NEED it ... Now.... |
@Penumbra69 and all the folks on here: I hear you, and the use cases you're describing totally make sense to me. I'm recategorizing this as an enhancement request because although it doesn't work the way you want it to, this is a known limitation rather than an accidental bug. |
Hi team Maybe a duplicate of #3116 ? |
@danieldreier given that Hashicorp has acknowledged this issue as a "known limitation" based on your June 12, 2020 comment, is the company able to provide a standard or recommended workaround to address this? |
I think the recommended workaround is find-and-replace value before running terraform :( |
Wow this is a real problem so either we duplicate all resources with prevent_destroy, you we use m4 or something to do a search for this (like you have to do with Dockerfiles. pretty ugly :-) |
+1 |
+1 We use terraform modules, the main dev set the default value at "true", that's not my use case :( |
Just a reminder to please use the 👍 reaction on the original post to upvote issues - we do sort by most upvoted to understand which issues are the most important. This also reduces "noise" in the notification feed for folks following this issue. Thanks!
I believe the blocker is that to support this feature one would need to implement pre-processing of the configuration. This would be a major design change to the underlying fundamentals of Terraform. Not impossible, but not something that is likely to happen without a major product design effort. Again, please do not quote me on that technical explanation; this is how I understand the underlying issue but I may be a little off-base. You might also check out these adjacent issues: |
its 2023 guys, any updates? |
Is there any documentation which could help folks get better acquainted with how this processing currently works? With a better understanding of the current difficulties/blockers, it would be easier to discuss potential solutions. |
+1 |
1 similar comment
+1 |
Thanks for your interest in this issue! This is just a reminder to please avoid "+1" comments, and to use the upvote mechanism (click or add the 👍 emoji to the original post) to indicate your support for this issue. Thanks again for the feedback! |
Any update on this one? |
+1 seems like a fairly common sense feature.. |
+1 |
I was surprised to find such a long and old tread for such a simple issue.... +1 |
You are not alone
|
+1 |
Thanks for your interest in this issue! This is just a reminder to please avoid "+1" comments, and to use the upvote mechanism (click or add the 👍 emoji to the original post) to indicate your support for this issue. Thanks again for the feedback! |
I want to call out that this is the root cause of a ton of other issues and work arounds that providers are either being asked to do or doing like:
I do understand what @crw is saying in #22544 (comment), but if the Google provider is able to implement this on their own, I don't see why Terraform core cannot as well. Yes, there are some user experience downsides to the Google implementation that they do for databases, like needing to have a separate apply that changes the |
Mentioning #30957 here as it is related. |
any update on this . |
Cannot use prevent_destroy in modules without a variable so for me it cannot be used without this feature. |
Running Terraform v1.4.6, still no support for variables in |
Still can't use |
This comment was marked as duplicate.
This comment was marked as duplicate.
any updates on this issue ? |
Thanks for the continued interest in this issue. As the use cases seem fairly clear and the replies primarily being of the +1 variety, I am going to lock comments to maintainers. For posterity, the last two substantive comments on this issue are: #22544 (comment) and #22544 (comment). Thanks again for your continued interest in this issue! |
Terraform Version
Terraform Configuration Files
Steps to Reproduce
terraform init
Description
The documentation notes that
so while I'm bummed that this doesn't work, I understand that I shouldn't expect it to.
However, we discovered this behavior because running
terraform init
failed where it had once worked. And indeed, if you comment out the variable reference in the snippet above, and replace it withprevent_destroy = false
, it works - and if you then change it back it keeps working.Is that intended behavior? And will it, if I do this workaround, keep working?
Debug Output
The text was updated successfully, but these errors were encountered: