我正在嘗試根據使用條件運算式的變數將資源塊部署到 dev 或 prod 中,為此我正在嘗試使用命令列引數。這適用于 terraform.tfvars 但不適用于 CMD 引數,這意味著當我嘗試運行terraform plan
它時沒有任何其他更改。
理想情況下,它應該添加 1 個實體。
這是我的資源塊
main.tf 檔案
resource "aws_instance" "dev" {
ami = "ami-0ca285d4c2cda3300"
instance_type = var.instanceType
count = var.istest == true ? 1 : 0
}
resource "aws_instance" "prod" {
ami = "ami-0ca285d4c2cda3300"
instance_type = "t2.micro"
count = var.istest == false ? 1 : 0
}
變數.tf
variable "istest" {
default = true
}
terraform .tf vars 為空,運行 terraform 的命令
terraform plan -var="istest=false"
uj5u.com熱心網友回復:
我建議使用以下語法而不是檢查文字true
或false
值
resource "aws_instance" "dev" {
ami = "ami-0ca285d4c2cda3300"
instance_type = var.instanceType
count = var.istest ? 1 : 0
}
resource "aws_instance" "prod" {
ami = "ami-0ca285d4c2cda3300"
instance_type = "t2.micro"
count = var.istest ? 0 : 1
}
這樣,如果 istest var 是true
,它將部署dev
實體。
如果是false
,它將創建prod
實體
嘗試
terraform plan -var="istest=false"
更新
核心問題似乎是 terraform 執行型別轉換
引自:https ://www.terraform.io/language/expressions/type-constraints#conversion-of-primitive-types
automatically convert number and bool
values to string values when needed
只要字串包含數字或布林值的有效表示,Terraform 語言就會,反之亦然。
true converts to "true", and vice-versa false converts to "false"
因此,您應該明確設定type
變數的
在你的variables.tf
檔案中
variable "istest" {
default = true
type = bool
}
然后它應該按預期作業
terraform plan -var="istest=false"
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/480637.html
標籤:亚马逊网络服务 地形 terraform-provider-aws