部署时出现AWS Lambda和IAM错误:Lambda无法承担为该功能定义的角色

时间:2019-08-09 20:59:53

标签: amazon-web-services aws-lambda amazon-iam serverless-framework

在我的AWS项目中,我使用无服务器框架来部署lambda函数和IAM角色。

因此,我创建了6个lambda函数,全部使用以下相同的IAM角色:

functions:

  auto-delete-identity:
    handler: src/auto-delete-identity.handler
    role: arn:aws:iam::123456789012:role/lambdaIAMRole
    name: auto-delete-identity

  auto-move-to-user-group:
    handler: src/auto-move-to-user-group.handler
    role: arn:aws:iam::123456789012:role/lambdaIAMRole
    name: auto-move-to-user-group

  auto-validate-user-creation:
    handler: src/auto-validate-user-creation.handler
    role: arn:aws:iam::123456789012:role/lambdaIAMRole
    name: auto-validate-user-creation

  auto-validation-user-email-modification:
    handler: src/auto-validation-user-email-modification.handler
    role: arn:aws:iam::123456789012:role/lambdaIAMRole
    name: auto-validation-user-email-modification

  hello-demo:
    handler: src/hello-demo.handler
    role: arn:aws:iam::123456789012:role/lambdaIAMRole
    name: hello-demo

  reset-user-password:
    handler: src/reset-user-password.handler
    role: arn:aws:iam::123456789012:role/lambdaIAMRole
    name: reset-user-password

resources:

  Resources:

    lambdaIAMRole:
      Type: "AWS::IAM::Role"
      Properties:
        RoleName: lambdaIAMRole
        AssumeRolePolicyDocument:
          Version: "2012-10-17"
          Statement:
            - Action:
                - "sts:AssumeRole"
              Effect: "Allow"
              Principal:
                Service:
                  - "lambda.amazonaws.com"
        Policies:
          - PolicyDocument:
              Version: "2012-10-17"
              Statement:
                - Action:
                    - "logs:CreateLogGroup"
                    - "logs:CreateLogStream"
                    - "logs:PutLogEvents"
                  Effect: "Allow"
                  Resource:
                    - !Sub "arn:aws:logs:eu-central-1:123456789012:log-group:/aws/lambda/*:*"
        PolicyName: "myLambdaPolicy"

当我使用无服务器部署命令进行部署时,有时会出现以下错误:

An error occurred: HelloDashdemoLambdaFunction - The role defined for the function cannot be assumed by Lambda. (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException; Request ID: 4099072a-809d-4f1c-b83e-7f4f5dd5170b).

这看起来像是一个随机错误,因为它并非每次都发生。另外,当它发生时,它并不总是在同一功能上发生。

我做错了吗?我该如何解决?

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

我认为问题在于,在Lambda函数声明中,您将IAM角色称为role: arn:aws:iam::123456789012:role/lambdaIAMRole。这是一个绝对 ARN,它是如何指示在serverless.yml模板的外部中创建和管理的IAM角色(或其他资源)。

对于您而言,最快的解决方法是将role: arn:aws:iam::123456789012:role/lambdaIAMRole替换为role: lambdaIAMRole。后者是指在模板内部中声明的AWS资源。

假设所有Lambda函数都将扮演相同的角色,一个更好的解决方案是完全删除lambdaIAMRole声明,然后从Lambda函数中删除所有role: arn:aws:iam::123456789012:role/lambdaIAMRole属性。角色声明除了无服务器框架将为您隐式生成并分配给Lambda函数的默认IAM角色之外,没有添加任何内容。这是使框架有价值的一件事-它提供了良好的默认设置,以节省您的时间和精力。示例here