【问题标题】:Terraform interpolation throwing error when using terraform.workspace option使用 terraform.workspace 选项时,Terraform 插值抛出错误
【发布时间】:2019-01-01 06:25:40
【问题描述】:

所以我正在尝试使用 public scheme 和 terraform.workspace 进行插值,以便动态选择子网。为此尝试将 terraform.workspace 与 elb_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

【问题讨论】:

    标签: terraform terraform-provider-aws


    【解决方案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}")}"
    }
    

    【讨论】:

    • 我如何查找列表本身而不是逗号分隔的字符串
    猜你喜欢
    • 2020-10-30
    • 1970-01-01
    • 2022-12-08
    • 2018-12-14
    • 2016-10-14
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    相关资源
    最近更新 更多