【问题标题】:terraform get list variable to resourceterraform 获取资源列表变量
【发布时间】:2019-04-08 00:25:19
【问题描述】:
variable "iam_action" {
  type    = "list"
  default = ["ec2.amazonaws.com","ecs.amazonaws.com"]
}

resource "aws_iam_role" "s3_role" {
  name               = "abcd"
  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": "sts:AssumeRole",
      "Principal": {
        "Service": [ "${var.iam_action}"
        ]
      },
      "Effect": "Allow,
      "Sid": ""
    }
  ]
}
EOF
}

错误:

At column 1, line 1: output of an HIL expression must be a string, or a single list (argument 2 is TypeList) in:

我尝试了 join 函数,但我需要将输出作为列表 ["a","b","c"] join 函数给出类似 ["a,b,c"] 的输出

【问题讨论】:

    标签: amazon-web-services terraform


    【解决方案1】:

    我用jsonencodetemplate_file修复它

    首先创建下面的json文件

    $ cat s3_policy.json
    
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Action": "sts:AssumeRole",
          "Principal": {
            "Service": ${iam_action}
          },
          "Effect": "Allow",
          "Sid": ""
        }
      ]
    }
    

    更新 tf 文件

    variable "iam_action" {
      type    = "list"
      default = ["ec2.amazonaws.com", "ecs.amazonaws.com"]
    }
    
    data "template_file" "s3_role" {
      template = "${file("${path.module}/s3_policy.json")}"
    
      vars {
        iam_action = "${jsonencode(var.iam_action)}"
      }
    }
    
    resource "aws_iam_role" "s3_role" {
      name = "abcd"
    
      assume_role_policy = "${data.template_file.s3_role.rendered}"
    }
    

    运行template plan

      + aws_iam_role.s3_role
          arn:                   "<computed>"
          assume_role_policy:    "{\n  \"Version\": \"2012-10-17\",\n  \"Statement\": [\n    {\n      \"Action\": \"sts:AssumeRole\",\n      \"Principal\": {\n        \"Service\": [\"ec2.amazonaws.com\",\"ecs.amazonaws.com\"]\n      },\n      \"Effect\": \"Allow\",\n      \"Sid\": \"\"\n    }\n  ]\n}\n"
          create_date:           "<computed>"
          force_detach_policies: "false"
          name:                  "abcd"
          path:                  "/"
          unique_id:             "<computed>"
    

    参考:

    terraform interpolation

    jsonencode(item) - 返回给定项目的 JSON 编码表示,它可以是字符串、字符串列表或从字符串到字符串的映射。 注意,如果item是字符串,返回值包含双引号

    这里解释了我不能直接在"${var.iam_action}"in template_file 中使用vars 的原因:

    vars - (可选)模板内插值的变量。请注意,变量必须都是原语。 直接引用列表或地图会导致验证错误

    【讨论】:

    • assume_role_policy: "" =&gt; "{\n \"Version\": \"2012-10-17\",\n \"Statement\": [\n {\n \"Action\": \"sts:AssumeRole\",\n \"Principal\": {\n \"Service\": \"${var.actionl}\"\n },\n \"Effect\": \"Allow\",\n \"Sid\": \"\"\n }\n ]\n}\n" create_date: "" =&gt; "&lt;computed&gt;" 错误:* aws_iam_role.s3_role:创建 IAM 角色 s3_sysops_role 时出错:MalformedPolicyDocument:策略中的主体无效:“SERVICE”:“${var.iam_action}”
    • 它呈现为 ${foo} 但它不呈现 foo =["a","b","c"]
    • @user60679 我修复并更新了答案,现在应该没问题了。
    • JSON 不是有效的 json
    • 错误:“assume_role_policy”包含无效的 JSON:对象键:值对 @BMW 后的无效字符“e”
    猜你喜欢
    • 2022-10-13
    • 2019-10-16
    • 2019-09-15
    • 2022-01-13
    • 1970-01-01
    • 2020-03-26
    • 2018-08-01
    • 1970-01-01
    • 2019-11-20
    相关资源
    最近更新 更多