在 terraform 中,如何将传入的变量用于地图?
In terraform, how do I use a passed-in variable for a map?
使用 terraform v0.12.3
$ terraform version
Terraform v0.12.3
+ provider.aws v2.20.0
我的 variables.tf 文件中有这个地图变量
variable "subnet_id" {
type = map
description = "Subnet to use"
default = {
sandbox = {
us-east-1a = "subnet-1234"
us-east-1b = "subnet-2345"
us-east-1c = "subnet-3456"
us-east-1d = ""
us-east-1e = ""
us-east-1f = ""
}
staging = {
us-east-1a = "subnet-4567"
us-east-1b = "subnet-5678"
us-east-1c = "subnet-6789"
us-east-1d = ""
us-east-1e = ""
us-east-1f = ""
}
production = {
us-east-1a = "subnet-7890"
us-east-1b = "subnet-0987"
us-east-1c = "subnet-9876"
us-east-1d = "subnet-8765"
us-east-1e = "subnet-7654"
us-east-1f = ""
}
}
}
如何通过传递环境变量获得我想要的子网ID? IOW,我想...
$ terraform plan -var 'environment=sandbox'
在我的 main.tf 代码中我有
module "jenkins_slave" {
subnet_id = var.subnet_id[var.environment["us-east-1a"]]
....
}
我明白了
$ terraform plan -var environment="sandbox"
Error: Invalid index
on main.tf line 17, in module "jenkins-slaves":
17: subnet_id = var.subnet_id[var.environment["us-east-1a"]]
|----------------
| var.environment is "sandbox"
This value does not have any indices.
在我的例子中,我希望 subnet_id
键最终等于 subnet-1234
首先,为了能够传递一个变量,你需要在你的代码中定义它:
variable "environment" {}
此外,您需要 var.subnet_id[var.environment]["us-east-1a"]
.
而不是 var.subnet_id[var.environment["us-east-1a"]]
否则,var.environment["us-east-1a"]
指的是 environment
映射的 us-east-1a
键。在您的情况下 environment
只是一个字符串而不是地图,因此 Terraform 会抛出一个错误:This value does not have any indices.
.
P.S。看来,你的variable "subnet_id" {...}
也有错别字,ssandbox
应该是sandbox
.
使用 terraform v0.12.3
$ terraform version
Terraform v0.12.3
+ provider.aws v2.20.0
我的 variables.tf 文件中有这个地图变量
variable "subnet_id" {
type = map
description = "Subnet to use"
default = {
sandbox = {
us-east-1a = "subnet-1234"
us-east-1b = "subnet-2345"
us-east-1c = "subnet-3456"
us-east-1d = ""
us-east-1e = ""
us-east-1f = ""
}
staging = {
us-east-1a = "subnet-4567"
us-east-1b = "subnet-5678"
us-east-1c = "subnet-6789"
us-east-1d = ""
us-east-1e = ""
us-east-1f = ""
}
production = {
us-east-1a = "subnet-7890"
us-east-1b = "subnet-0987"
us-east-1c = "subnet-9876"
us-east-1d = "subnet-8765"
us-east-1e = "subnet-7654"
us-east-1f = ""
}
}
}
如何通过传递环境变量获得我想要的子网ID? IOW,我想...
$ terraform plan -var 'environment=sandbox'
在我的 main.tf 代码中我有
module "jenkins_slave" {
subnet_id = var.subnet_id[var.environment["us-east-1a"]]
....
}
我明白了
$ terraform plan -var environment="sandbox"
Error: Invalid index
on main.tf line 17, in module "jenkins-slaves":
17: subnet_id = var.subnet_id[var.environment["us-east-1a"]]
|----------------
| var.environment is "sandbox"
This value does not have any indices.
在我的例子中,我希望 subnet_id
键最终等于 subnet-1234
首先,为了能够传递一个变量,你需要在你的代码中定义它:
variable "environment" {}
此外,您需要 var.subnet_id[var.environment]["us-east-1a"]
.
var.subnet_id[var.environment["us-east-1a"]]
否则,var.environment["us-east-1a"]
指的是 environment
映射的 us-east-1a
键。在您的情况下 environment
只是一个字符串而不是地图,因此 Terraform 会抛出一个错误:This value does not have any indices.
.
P.S。看来,你的variable "subnet_id" {...}
也有错别字,ssandbox
应该是sandbox
.