我正在尝试循环 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_name
in的变量定义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 ~}
希望有人能解释我所缺少的内容或有人为我指明正确的方向。
提前致谢