使用terraform.workspace选项时

时间:2018-07-24 15:01:01

标签: terraform terraform-provider-aws

所以我尝试用 public scheme terraform.workspace 进行插值,以便动态选择子网。对于尝试将terraform.workspaceelb_subnets合并的操作,但它抛出的错误是 仅支持'terraform.X'插值的键是'工作区'

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 workspace list
  default
* qa

1 个答案:

答案 0 :(得分:1)

您可以通过使用嵌套的地图并使用terraform.workspace作为键来获得所需的结果。像这样

variable "elb_scheme" {
  default = "public"
}

variable "subnets" {
  type = "map"

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

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

output "my_subnets" {
  value = "${lookup(var.subnets[terraform.workspace],"${var.elb_scheme}")}"
}