AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1143075
Accepted
joebegborg07
joebegborg07
Asked: 2023-09-05 16:56:53 +0800 CST2023-09-05 16:56:53 +0800 CST 2023-09-05 16:56:53 +0800 CST

Terraform - JSON 中的 for 指令

  • 772

我正在尝试循环 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 ~}

希望有人能解释我所缺少的内容或有人为我指明正确的方向。

提前致谢

terraform
  • 1 1 个回答
  • 23 Views

1 个回答

  • Voted
  1. Best Answer
    Thai Nguyen
    2023-09-05T19:07:33+08:002023-09-05T19:07:33+08:00

    在您的 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)
        }
    
    • 1

相关问题

  • 子网没有在 azure 上使用 terraform 创建,如何解决?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve