策略包含无效的JSON:查找起始值的字符“a”无效

dzhpxtsq  于 2023-05-08  发布在  其他
关注(0)|答案(1)|浏览(186)

我正在尝试向我的执行角色添加一个策略,以便我的lambda函数可以访问我的s3存储桶。因此,我在terraform中为策略创建了这个data资源,它看起来像这样。

data "aws_iam_policy_document" "lambda_s3_access" {
  statement {
    effect = "Allow"
    actions = [
      "s3:ListBucket",
      "s3:*Object"
    ]
    resources = [
      "${aws_s3_bucket.bronze-stage.arn}/*",
      "${aws_s3_bucket.silver-stage.arn}/*",
      "${aws_s3_bucket.gold-stage.arn}/*",
      "${aws_s3_bucket.bronze-stage.arn}",
      "${aws_s3_bucket.silver-stage.arn}",
      "${aws_s3_bucket.gold-stage.arn}"
    ]
  }
}

创建计划时的json如下所示。(编辑)

+ resource "aws_iam_policy" "lambda_s3_access_iam_policy" {
      + arn         = (known after apply)
      + id          = (known after apply)
      + name        = "lambda_s3_access"
      + name_prefix = (known after apply)
      + path        = "/"
      + policy      = jsonencode(
            {
              + Statement = [
                  + {
                      + Action   = [
                          + "s3:ListBucket",
                          + "s3:*Object",
                        ]
                      + Effect   = "Allow"
                      + Resource = [
                          + "arn:aws:s3:::silver-stage-bucket-test/*",
                          + "arn:aws:s3:::silver-stage-bucket-test",
                          + "arn:aws:s3:::gold-stage-bucket-test/*",
                          + "arn:aws:s3:::gold-stage-bucket-test",
                          + "arn:aws:s3:::bronze-stage-bucket-test/*",
                          + "arn:aws:s3:::bronze-stage-bucket-test",
                        ]
                      + Sid      = ""
                    },
                ]
              + Version   = "2012-10-17"
            }
        )
      + policy_id   = (known after apply)
      + tags_all    = (known after apply)
    }

之后,我从这些数据中创建了一个策略资源,如下所示。

resource "aws_iam_policy" "lambda_s3_access_iam_policy" {
  name = "lambda_s3_access"
  policy = data.aws_iam_policy_document.lambda_s3_access.json
}

但是当我使用这个策略资源时,我不断得到这个错误。

Error: "policy" contains an invalid JSON: invalid character 'a' looking for beginning of value

我也尝试过使用EOF语法创建策略,但没有成功。所有的帮助是非常感谢。

uemypmqf

uemypmqf1#

对于那些遇到同样错误的人。我没有找到一个terraform修复这个问题,但你可以做的是创建一个shell脚本,它做完全相同的事情,它创建策略,然后将其附加到lambda执行角色。我知道这可能不是最好的方法,但由于这是terraform/aws中的一个bug,我看不到其他方法。不管怎样,这是我的解决方案。这是我的JSON格式的策略

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "ListObjectsInBucket",
            "Effect": "Allow",
            "Action": [
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::bucket"
            ]
        },
        {
            "Sid": "AllObjectActions",
            "Effect": "Allow",
            "Action": "s3:*Object",
            "Resource": [
                "arn:aws:s3:::bucket/*"
            ]
        }
    ]
}

这是我在github中调用的shell脚本。

#!/bin/bash

# create policy
output=$(aws iam create-policy --policy-name "access_s3_buckets" --policy-document file://bucket_access_policy.json)
arn_policy=$(echo $output | jq -r '.Policy.Arn')

aws iam attach-role-policy --role-name lambda_role --policy-arn $arn_policy

它首先创建策略,我存储json输出,然后我从这个输出中获取Arn,并使用它将我的策略附加到lambda执行角色(lambda_role)。希望这能帮助其他人解决这个错误。对于那些使用这个解决方案的人,不要忘记在github action中使你的脚本可执行。(git update-index --chmod=+x script_name.sh

编辑

我已经找到了一个解决这个问题的地形。

resource "aws_iam_policy" "lambda_role_s3_bucket" {
  name = "lambda_access_s3_buckets"
  policy = <<EOF
{
  "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllBucketActions",
            "Effect": "Allow",
            "Action": "s3:*Bucket",
            "Resource": [
                "${aws_s3_bucket.bronze-stage.arn}",
                "${aws_s3_bucket.silver-stage.arn}",
                "${aws_s3_bucket.gold-stage.arn}"
            ]
        },
        {
            "Sid": "AllObjectActions",
            "Effect": "Allow",
            "Action": "s3:*Object",
            "Resource": [
                "${aws_s3_bucket.bronze-stage.arn}/*",
                "${aws_s3_bucket.silver-stage.arn}/*",
                "${aws_s3_bucket.gold-stage.arn}/*"
            ]
        }
    ]
}
EOF
}

像这样以纯文本形式创建策略。

resource "aws_iam_role_policy_attachment" "lambda_s3_access_attachment" {
  role = "${aws_iam_role.lambda_role.name}"
  policy_arn = aws_iam_policy.lambda_role_s3_bucket.arn
}

然后您可以像这样将其附加到执行角色。
我把两个答案都留在这里。希望这对其他人有帮助!

相关问题