使用 terraform.workspace 选项时 Terraform 插值抛出错误

使用 terraform.workspace 选项时 Terraform 插值抛出错误

所以我尝试做插值,即public schemeterraform.workspace因此子网将动态选择。为此尝试合并terraform.workspaceelb_subnets但它抛出错误'terraform.X' 插值唯一支持的键是 'workspace'

variable "elb_scheme" {
  default = "public"
}

variable "prod_elb_subnets" {
  type = "map"

  default = {
    public  = "subnet-23ywe324, subnet-234hj34, subnet-cdh7868"
    private = "subnet-hj3h2323, subnet-jihi782, subnet-237dew"
  }
}

variable "qa_elb_subnets" {
  type = "map"

  default = {
    public  = "subnet-234ee234, subnet-da238sdf, subnet-sd2233"
    private = "subnet-09jsdf23, subnet-hi232rf, subnet-89832w32"
  }
}

  setting {
    namespace = "aws:ec2:vpc"
    name      = "ELBSubnets"

    value = "${var.(terraform.workspace_elb_subnets["${var.elb_scheme}"])}"
  }

输出:

Error: module.ebs.aws_elastic_beanstalk_environment.beanstalk: 1 error(s) occurred:

* module.ebs.aws_elastic_beanstalk_environment.beanstalk: terraform.workspace_elb_subnets: only supported key for 'terraform.X' interpolations is 'workspace'

Terraform 工作区

terraform workspace list
  default
* qa

答案1

您可以标记子网吗?使用基于环境的数据源查找比在变量中硬编码它们更容易。

如果你只需要 ID,可以这样做

data "aws_subnet_ids" "subnets" {

  tags {
    Env = "${terraform.workspace}"
  }
}

如果你的子网标有工作区,那么你可以像使用它们一样

根据计数获取单个子网

"${element(data.aws_subnet_ids.subnets.ids, count.index)}" 

将它们全部放入列表中

"${data.aws_subnet_ids.subnets.ids}"

请参阅以下内容了解更多信息

https://www.terraform.io/docs/providers/aws/d/subnet_ids.html

数据源非常有用,您应该研究使用它们,而不是对任何内容进行硬编码。

相关内容