【问题标题】:IAM policy: MalformedPolicyDocument: Syntax errors in policyIAM 策略:MalformedPolicyDocument:策略中的语法错误
【发布时间】:2017-11-26 07:40:47
【问题描述】:

我能够成功运行包含以下 sn-p 的 cloudformation 堆栈,现在我的最终目标是将其移植到 Terraform,但是..

即使在 AWS 控制台中,我也会收到格式错误的语法错误。我尝试使用 AWS 控制台的“策略编辑器”进行调试,然后单击“验证”按钮,但错误不是特定的。有人知道我在做什么错吗?这很奇怪,因为当我部署 cloudformation 堆栈模板时,这个策略似乎起作用了。 (顺便说一句,如果有帮助,这来自 GorillaStack 的 AutoTagging 项目)

此政策包含以下错误:政策中的语法错误。有关 IAM 策略语法的更多信息,请参阅 AWS IAM 策略。

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "logs:CreateLogGroup",
            "logs:CreateLogStream",
            "logs:PutLogEvents"
          ],
          "Resource": "arn:aws:logs:*:*:*"
        },
        {
          "Effect": "Allow",
          "Action": [
            "s3:GetObject",
            "s3:ListBucket"
          ],
          "Resource": [
            "*"
          ]
        },
        {
          "Effect": "Allow",
          "Action": [
            "cloudformation:DescribeStackResource"
          ],
          "Resource": [
            { "Fn::Join": [ "", [ "arn:aws:cloudformation:", { "Ref" : "AWS::Region" }, ":", { "Ref" : "AWS::AccountId" }, ":stack/autotag/*" ] ] }
          ]
        },
        {
          "Effect": "Allow",
          "Action": [
            "sts:*"
          ],
          "Resource": [
            { "Fn::GetAtt" : [ "AutoTagMasterRole", "Arn" ] }
          ]
        }
      ]
    }

我的 terraform 配置有以下资源(包括上面的 sn-p)

 resource "aws_iam_role_policy" "AutoTagExecutionPolicy" {
   name = "AutoTagExecutionPolicy"
   role = "${aws_iam_role.AutoTagExecutionRole.id}"

   policy = <<EOF
   <-THE POLICY ABOVE GOES HERE->
 EOF
 }

【问题讨论】:

  • 当您将策略移至 terraform 时,您是否删除了 Cloudformation 功能,如 ref 的?
  • 不,我没有。你能推断出来吗?例如,我是否需要在 terraform config 中为 cloudformation 模板中的此类条目插入值?:{“Ref”:“AWS::AccountId”}
  • 这些函数中的每一个都是 cloudformation 的原生函数,您需要将它们转换为 terraform 脚本中的变量。
  • 该死,我明白了。这并不像我想象的那么简单。我在文档中没有看到类似的东西。如果你对这个答案有信心,那我就指明了正确的方向。
  • 我需要转换这一整行: { "Fn::Join": [ "", [ "arn:aws:cloudformation:", { "Ref" : "AWS::Region" }, ":", { "Ref" : "AWS::AccountId" }, ":stack/autotag/*" ] ] } 我不知道那会是什么样子。新手在这里

标签: amazon-cloudformation amazon-iam terraform


【解决方案1】:

您需要将 Cloudformation 函数转换为 terraform 脚本中的变量。

data "aws_iam_policy_document" "example" {
  statement {
    sid    = "allow logs"
    effect = "Allow"

    action = [
      "logs:CreateLogGroup",
      "logs:CreateLogStream",
      "logs:PutLogEvents",
    ]

    Resources = [
      "arn:aws:logs:*:*:*",
    ]
  }

  statement {
    sid    = "allow s3"
    effect = "Allow"

    action = [
      "s3:GetObject",
      "s3:ListBucket",
    ]

    resource = [
      "*",
    ]
  }

  statement {
    sid = "allow cfn"

    effect = "Allow"

    action = [
      "cloudformation:DescribeStackResource",
    ]

    resource = [
      "${var.cfn_stack}",
    ]
  }

  statement {
    sid    = "allow sts"
    effect = "Allow"

    action = [
      "sts:*",
    ]

    resource = [
      "${var.AutoTagMasterRole_arn}",
    ]
  }
}

那么

resource "aws_iam_policy" "example" {
  name   = "example_policy"
  path   = "/"
  policy = "${data.aws_iam_policy_document.example.json}"
}

https://www.terraform.io/docs/providers/aws/d/iam_policy_document.html

https://www.terraform.io/docs/configuration/interpolation.html

【讨论】:

    猜你喜欢
    • 2017-11-02
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 2017-07-27
    • 2016-06-05
    • 1970-01-01
    相关资源
    最近更新 更多