Terraform - JSON 中的 for 指令

Terraform - JSON 中的 for 指令

我正在嘗試循環 IAM 策略資源區塊中的字串值以允許 rds IAM 身份驗證。我的資源定義是:

  resource "aws_iam_policy" "rds_iam_authentication"{
  name    = "${title(var.environment)}RdsIamAuthentication"
  policy  = templatefile(
    "${path.module}/iam_policy.json",
    {
      aws_account_id        = data.aws_caller_identity.current.account_id
      region         = var.region
      environment           = var.environment
      iam_rds_pg_role_name  = var.iam_rds_pg_role_name
    }
  )
}

iam_rds_pg_role_namein的變數定義terraform.tfvars如下:

region                  = "eu-west-3"
environment             = "env_name"
iam_rds_pg_role_name    = ["read_only_role", "full_role"]
aws_account_id          = "1234567890"

IAM 策略範本檔案為:

{
  "Version": "2012-10-17",
  "Statement": [
      {
          "Effect": "Allow",
          "Action": [
              "rds-db:connect"
          ],
          "Resource": [
            %{ for rds_role in iam_rds_pg_role_name ~}
              "arn:aws:rds-db:${region}:${aws_account_id}:dbuser:*/${rds_role}"
            %{ endfor ~}
          ]
      }
  ]
}

我收到一條錯誤訊息

錯誤:「policy」包含無效的 JSON:陣列元素後的字元「」無效

我非常確定問題與 json 編碼有關,但是當嘗試jsonencode像下面這樣在 json 中定義 arn 時,錯誤仍然存在:

%{ for rds_role in iam_rds_pg_role_name ~}
    jsonencode("arn:aws:rds-db:${region}:${aws_account_id}:dbuser:*/${rds_role}")}
%{ endfor ~}

希望有人能解釋我所缺少的內容或有人為我指明正確的方向。

先致謝

答案1

在您的 IAM 策略範本檔案中,您缺少以下項目的尾隨逗號Resource

...
        "Resource": [
            %{ for rds_role in iam_rds_pg_role_name ~}
              "arn:aws:rds-db:${region}:${aws_account_id}:dbuser:*/${rds_role}",
                                                  there should be a comma here ^
            %{ endfor ~}
          ]
...

您可以循環遍歷 中的索引和項目iam_rds_pg_role_name,如果索引小於 則新增逗號length(iam_rds_pg_role_name) - 1

...
        "Resource": [
            %{ for index, rds_role in iam_rds_pg_role_name ~}
              "arn:aws:rds-db:${region}:${aws_account_id}:dbuser:*/${rds_role}"%{ if idx < length(iam_rds_pg_role_name) - 1 },%{ endif }
            %{ endfor ~}
          ]
...

結果:

Terraform will perform the following actions:

  # aws_iam_policy.rds_iam_authentication will be created
  + resource "aws_iam_policy" "rds_iam_authentication" {
      + arn         = (known after apply)
      + id          = (known after apply)
      + name        = "Env_nameRdsIamAuthentication"
      + name_prefix = (known after apply)
      + path        = "/"
      + policy      = jsonencode(
            {
              + Statement = [
                  + {
                      + Action   = [
                          + "rds-db:connect",
                        ]
                      + Effect   = "Allow"
                      + Resource = [
                          + "arn:aws:rds-db:eu-west-3:1234567890:dbuser:*/read_only_role",
                          + "arn:aws:rds-db:eu-west-3:1234567890:dbuser:*/full_role",
                        ]
                    },
                ]
              + Version   = "2012-10-17"
            }
        )
      + policy_id   = (known after apply)
      + tags_all    = (known after apply)
    }

相關內容