如何告诉terraform不要有条件地破坏现有资源
如何告诉 terraform 不要在有条件的情况下破坏现有资源
你好,我有一个 terraform 变量,它控制资源的创建
variable "apigw_key" {
type = string
default = "X"
}
当我运行 terraform apply 时,它会适当地创建资源
resource "aws_api_gateway_resource" "whitelist-create" {
parent_id = "u8u7hy"
path_part = "create"
rest_api_id = "9uumm7"
count = var.apigw_key == "X" ? 1 : 0
}
resource "aws_api_gateway_resource" "account-delete" {
parent_id = "fgty72"
path_part = "delete"
rest_api_id = "9uumm7"
count = var.apigw_key == "Y" ? 1 : 0
}
运行 terraform apply 时,输出似乎很完美
terraform apply -var="apigw_key=X"
Plan: 1 to add, 0 to change, 0 to destroy.
当我在 Y 上运行 terraform 计划时,X 资源在计划中显示已损坏
variable "apigw_key" {
type = string
default = "Y"
}
terraform plan -var="apigw_key=Y"
Plan: 1 to add, 0 to change, 1 to destroy.
如何控制现有资源不被破坏
编辑基于Marko反馈
variable "X" {
type = bool
default = false
}
variable "Y" {
type = bool
default = false
}
这是我更新的资源配置
resource "aws_api_gateway_resource" "whitelist-create" {
parent_id = "u8u7hy"
path_part = "create"
rest_api_id = "9uumm7"
count = var.Y ? 1 : 0
}
resource "aws_api_gateway_resource" "account-delete" {
parent_id = "fgty72"
path_part = "delete"
rest_api_id = "9uumm7"
count = var.X ? 1 : 0
}
terraform apply -var X=true
aws_api_gateway_resource.account-delete< /strong> 将被创建并维护状态文件
,当我执行以下操作时 terraform apply -var Y=true 它将创建一个资源并删除一个
我的问题是如何阻止现有资源不被删除?
How to tell the terraform not to destroy the existing resource on condition
Hello I have a terraform variable which controls the creation of the resource
variable "apigw_key" {
type = string
default = "X"
}
When i run the terraform apply It create the resource appropriately
resource "aws_api_gateway_resource" "whitelist-create" {
parent_id = "u8u7hy"
path_part = "create"
rest_api_id = "9uumm7"
count = var.apigw_key == "X" ? 1 : 0
}
resource "aws_api_gateway_resource" "account-delete" {
parent_id = "fgty72"
path_part = "delete"
rest_api_id = "9uumm7"
count = var.apigw_key == "Y" ? 1 : 0
}
The output seems to be perfect when terraform apply is run
terraform apply -var="apigw_key=X"
Plan: 1 to add, 0 to change, 0 to destroy.
When i run the terraform plan on Y the X resources shows destroyed in the plan
variable "apigw_key" {
type = string
default = "Y"
}
terraform plan -var="apigw_key=Y"
Plan: 1 to add, 0 to change, 1 to destroy.
How to control the existing resource not to be destroyed
Edited based on Marko feedback
variable "X" {
type = bool
default = false
}
variable "Y" {
type = bool
default = false
}
This is my updated resource config
resource "aws_api_gateway_resource" "whitelist-create" {
parent_id = "u8u7hy"
path_part = "create"
rest_api_id = "9uumm7"
count = var.Y ? 1 : 0
}
resource "aws_api_gateway_resource" "account-delete" {
parent_id = "fgty72"
path_part = "delete"
rest_api_id = "9uumm7"
count = var.X ? 1 : 0
}
terraform apply -var X=true
aws_api_gateway_resource.account-delete will get created and maintain the state file
and when i do the following terraform apply -var Y=true it will create one resource and delete one
My Question is how to prevent existing resource not to be deleted ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,您所描述的行为是可取的 - 如果您的配置没有创建资源,则您不希望该资源存在。但是,在某些情况下,可能存在记录保存或折旧要求,这会阻止您在不需要时立即将其删除。
如果您想有条件地创建资源,但在条件发生变化时不删除它,请使用
prevent_destroy
生命周期参数:Generally, the behavior you're describing is desirable - if your configuration doesn't create a resource, you don't want that resource to exist. However, in some cases there might be requirements for record keeping or depreciation that prevent you from deleting it as soon as you don't need it.
If you want to conditionally create a resource, but not delete it when that condition changes, use the
prevent_destroy
lifecycle argument: