【发布时间】:2019-12-07 04:57:16
【问题描述】:
使用 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
【问题讨论】:
标签: terraform terraform-provider-aws