使用AWS开发工具包的Lambda上的AWS SAM AccessDeniedException

时间:2019-11-12 23:03:22

标签: node.js amazon-web-services aws-lambda aws-sam

我在Node.js中使用AWS Lambda函数,该函数使用SDK方法listVersionsByFunction

它是通过此AWS SAM模板创建的:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
  Function:
    Timeout: 3
Resources:
  special:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: 'my-project-special'
      CodeUri: ./handlers
      Handler: special.handler
      Runtime: nodejs10.x
  getLatest:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ./handlers
      Handler: getLatest.handler
      Runtime: nodejs10.x
      Events:
        getLatest:
          Type: Api
          Properties:
            Path: /latest/
            Method: get

并且处理程序这样调用SDK:

const result = await lambda.listVersionsByFunction({
    FunctionName: 'my-project-special',
}).promise();

部署并提出请求后,出现AccessDeniedException错误:

  

用户:arn:aws:sts :: 999999999:assumed-role / my-project-getLatest-ADFADSFASD / my-project-getLatest-HJLKHLKJKJ未经授权执行:lambda:ListVersionsByFunction on resource:arn:aws:lambda :us-east-2:999999999:function:my-project-special

如何通过AWS SAM模板允许此访问?

1 个答案:

答案 0 :(得分:1)

该错误表明您的Lambda无权对其他资源(另一个Lambda)执行ListVersionsByFunction操作。

您要做的是创建自定义策略,并将其添加到您的template中。

getLatest:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ./handlers
      Handler: getLatest.handler
      Runtime: nodejs10.x
      Policies:
         - Version: '2012-10-17'
           Statement:
             - Effect: Allow
               Action:
                 - lambda:ListVersionsByFunction
               Resource: '*'
      Events:
        getLatest:
          Type: Api
          Properties:
            Path: /latest/
            Method: get

See reference

或者您也可以在 IAM管理控制台中将此策略添加为inline policy,在“角色”下选择功能getLatest并添加策略。查看快照。

enter image description here 希望对您有帮助