【问题标题】:conditional operator works fine with one condition but not with other条件运算符适用于一种条件但不适用于其他条件
【发布时间】:2019-02-02 23:20:17
【问题描述】:

我有一个使用计划,我打算根据变量 mantanance_mode 创建或不创建它,该变量的值为 true/false。以下代码运行良好。

resource "aws_api_gateway_usage_plan" "usageplan" {
  name  = "${var.environment}-usage-plan"
  count = "${var.mantanance_mode == true ? 1 : 0}"

  api_stages {
    api_id = "${aws_api_gateway_rest_api.api_gateway.id}"
    stage  = "${aws_api_gateway_stage.api_gateway_stage.stage_name}"
  }
}

但是当我尝试以以下方式使用它时,它就不起作用了。

security_group_id = "${var.mantanance_mode == true ? aws_security_group.allow_ssh_from_office_sg.id : aws_security_group.lambda_sg.id}"

【问题讨论】:

    标签: terraform


    【解决方案1】:

    按照文档https://www.terraform.io/docs/configuration/interpolation.html 中所述的方式使用条件运算符是值得的,而不是仅使用布尔值 true 或 false 如果您使用 "some_value" 比较,它肯定会起作用。

    security_group_id = "${var.mantanance_mode == "true" ? aws_security_group.allow_ssh_from_office_sg.id : aws_security_group.lambda_sg.id}"
    
    resource "aws_api_gateway_usage_plan" "usageplan" {
      name  = "${var.environment}-usage-plan"
      count = "${var.mantanance_mode == "true" ? 1 : 0}"
    
      api_stages {
        api_id = "${aws_api_gateway_rest_api.api_gateway.id}"
        stage  = "${aws_api_gateway_stage.api_gateway_stage.stage_name}"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多