我想要一个具有以下属性的Terraform变量:
该用例用于AWS ECS标记,我希望能够通过更改标记来部署新版本,这将使Terraform创建新任务定义并修改服务定义。但是,如果我运行“ terraform apply”,则在不传递新值的情况下,我什么也不想发生,即terraform会记住上一次运行的值。
欢迎提出建议!
答案 0 :(得分:1)
该文档没有说明保持状态所需要的内容,但是它确实提供了有关变量的有用信息-https://medium.com/@milescollier/handling-environmental-variables-in-terraform-workspaces-27d0278423df
您也可以-
variable "template" {
type = "string"
default = "01000000-0000-4000-8000-000030080200"
}
or
variable "environment-type" {
type = "map"
default = {
dev = "DEVELOPMENT"
preprod = "PRE-PRODUCTION"
prod = "PRODUCTION"
}
}
答案 1 :(得分:1)
我正在尝试做类似的事情,但偶然发现了这篇文章。最后想出了一个解决方案,所以我想我应该分享给其他看到这篇文章的人。
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"