SAM 模板中的 IAM 角色

IAM role inside SAM template

如何在 SAM 模板中创建 IAM 角色,就像我在 SAM 包中所做的那样。 我试过如下:

"lambdaFunctionRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "lambda.amazonaws.com",
                  "apigateway.amazonaws.com"
                ]
              },
              "Action": "sts:AssumeRole"
            }
          ]
        },
        "ManagedPolicyArns": [
          {
            "Ref": "lambdaBasePolicy"
          }
        ],
        "Policies": [
          {
            "PolicyName": "root",
            "PolicyDocument": {
              "Version": "2012-10-17",
              "Statement": [
                {
                  "Effect": "Allow",
                  "Action": [
                    "logs:CreateLogGroup",
                    "logs:CreateLogStream",
                    "logs:PutLogEvents"
                  ],
                  "Resource": "arn:aws:logs:*:*:*"
                },
                {
                  "Effect": "Allow",
                  "Action": [
                    "s3:*",
                    "dynamodb:*",
                    "iam:ListRoles",
                    "ses:*",
                    "events:*"
                  ],
                  "Resource": "*"
                }
              ]
            }
          }
        ]
      }
    }

它抛出一个错误:com.amazonaws.serverlessappsrepo.template.InvalidTemplateException:名称为 [lambdaFunctionRole] 的资源无效。 AWS::Serverless::Role 不是受支持的无服务器应用存储库类型。

发布到无服务器应用程序存储库时,您需要注意在 SAM 模板中仅使用 supported resources

在您的情况下,您可以跳过将 lambdaFunctionRole 创建为独立资源,而只需在函数资源定义中内联创建它。

"lambdaFunction": {
  "Type": "AWS::Serverless::Function",
  "Policies": [
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "logs:CreateLogGroup",
            "logs:CreateLogStream",
            "logs:PutLogEvents"
          ],
          "Resource": "arn:aws:logs:*:*:*"
        },
        {
          "Effect": "Allow",
          "Action": [
            "s3:*",
            "dynamodb:*",
            "iam:ListRoles",
            "ses:*",
            "events:*"
          ],
          "Resource": "*"
        }
      ]
    }
  ]
}

请注意,我只复制了角色中策略的 PolicyDocument 部分。请参阅 SAM spec 中的政策部分。