terraform - 如何在 terraform 中实现“粘性”变量?
问题描述
我想要一个具有以下属性的 Terraform 变量:
- 如果从未设置,则使用默认值
- 如果没有提供值,则使用上一次运行的值
- 如果提供了一个值,请使用它并记住它
该用例适用于 AWS ECS 标签,我希望能够通过更改标签来部署新版本,这将导致 Terraform 创建新的任务定义并修改服务定义。但是,如果我运行“terraform apply”,但不传递新值,我不希望发生任何事情,即 terraform 会记住上一次运行的值。
欢迎提出建议!
解决方案
我试图做类似的事情并遇到了这篇文章。最后制定了一个解决方案,所以我想我应该分享给其他遇到这篇文章的人。
variable "maintenance" {
description = "Set to active, disabled or previous (Default)"
type = string
default = "previous"
}
# This is used so we can lookup the previous value of the maintenance setting in the state file
data "terraform_remote_state" "bc" {
backend = "gcs"
config = {
bucket = "terraform-bucket"
prefix = "terraform/state"
}
workspace = terraform.workspace
# Set a default value in case of an empty state file
defaults = {
maintenance = "disabled"
}
}
locals {
maintenance_status = var.maintenance == "previous" ? data.terraform_remote_state.bc.outputs.maintenance : var.maintenance
}
# Used to expose the current value to subsequent tf runs
output "maintenance" {
value = example_resource.maintenance.status
}
然后您可以使用命令行更改设置,但如果未指定,它将使用以前的值
terraform apply -var="maintenance=active"
推荐阅读
- c++ - 在 OpenGl 中以程序方式将数据添加到顶点和元素缓冲区?
- r - 计算 R 中列表中 NULL 的出现次数
- java - 我可以编写一个 Java 程序来调用另一种语言(Rust)的程序来进行一些计算吗?
- node.js - 创建一个支持 https 的 HTTP 代理服务器,并使用另一个代理服务器使用 nodejs 提供响应
- javascript - 由输入值 javascript 制成的 Google Charts 数据数组
- octave - 如何在绘图中仅显示 'n' 刻度日期时间标签或让 octave 为我选择大量刻度?
- laravel - 向 Laravel 验证响应添加状态标志
- wpf - Caliburn 事件未触发
- html - div 之间的标签的有效高度和宽度为 0
- c# - 使用 ubuntu+nginx 从 asp.net 上的站点发送电子邮件