代码之家  ›  专栏  ›  技术社区  ›  Allyl Isocyanate

使用AWS SAM时无法测试引用DynamoDB表的本地Lambda

  •  0
  • Allyl Isocyanate  · 技术社区  · 4 年前

    我创建了“快速入门:Web后端”AWS SAM项目,该项目使用 sam init sam-app .

    按照自述中的说明,当我尝试构建和调用引用Dynamo表的lambda函数时:

    sam build
    sam local invoke getAllItemsFunction --event events/event-get-all-items.json
    

    我得到一个 Requested resource not found 错误:

    Invoking src/handlers/get-all-items.getAllItemsHandler (nodejs12.x)
    
    Fetching lambci/lambda:nodejs12.x Docker container image......
    Mounting /Users/dev/lab/sam-app/.aws-sam/build/getAllItemsFunction as /var/task:ro,delegated inside runtime container
    START RequestId: bd1dd37e-d464-13c1-45da-a4d427db1e84 Version: $LATEST
    2020-07-12T23:33:50.674Z    bd1dd37e-d464-13c1-45da-a4d427db1e84    INFO    received: { httpMethod: 'GET' }
    2020-07-12T23:33:50.812Z    bd1dd37e-d464-13c1-45da-a4d427db1e84    ERROR   Invoke Error    {"errorType":"ResourceNotFoundException","errorMessage":"Requested resource not found","code":"ResourceNotFoundException","message":"Requested resource not found","time":"2020-07-12T23:33:50.809Z","requestId":"E711QAGJJAH7FDFSIU8PR88053VV4KQNSO5AEMVJF66Q9ASUAAJG","statusCode":400,"retryable":false,"retryDelay":26.07018253857256,"stack":["ResourceNotFoundException: Requested resource not found","    at Request.extractError (/var/task/node_modules/aws-sdk/lib/protocol/json.js:51:27)","    at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:106:20)","    at Request.emit (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:78:10)","    at Request.emit (/var/task/node_modules/aws-sdk/lib/request.js:688:14)","    at Request.transition (/var/task/node_modules/aws-sdk/lib/request.js:22:10)","    at AcceptorStateMachine.runTo (/var/task/node_modules/aws-sdk/lib/state_machine.js:14:12)","    at /var/task/node_modules/aws-sdk/lib/state_machine.js:26:10","    at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:38:9)","    at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:690:12)","    at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:116:18)"]}
    
    

    示例项目包含3个调用DynamoDb后端的lambda函数(为清楚起见进行了修剪):

    AWSTemplateFormatVersion: 2010-09-09
    Description: >-
      sam-ap
    
    Transform:
    - AWS::Serverless-2016-10-31
    
    Resources:
      getAllItemsFunction:
        Type: AWS::Serverless::Function
        Properties:
          Handler: src/handlers/get-all-items.getAllItemsHandler
          Runtime: nodejs12.x
          MemorySize: 128
          Timeout: 100
          Description: A simple example includes a HTTP get method to get all items from a DynamoDB table.
          Policies:
            # Give Create/Read/Update/Delete Permissions to the SampleTable
            - DynamoDBCrudPolicy:
                TableName: !Ref SampleTable
          Environment:
            Variables:
              # Make table name accessible as environment variable from function code during execution
              SAMPLE_TABLE: !Ref SampleTable
          Events:
            Api:
              Type: Api
              Properties:
                Path: /
                Method: GET
    
      SampleTable:
        Type: AWS::Serverless::SimpleTable
        Properties:
          PrimaryKey:
            Name: id
            Type: String
          ProvisionedThroughput:
            ReadCapacityUnits: 2
            WriteCapacityUnits: 2
    
    Outputs:
      WebEndpoint:
        Description: "API Gateway endpoint URL for Prod stage"
        Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
    
    

    我的假设是DynamoDB表不是在AWS测试docker容器中本地创建的。 在本地测试DynamoDB时,我是否缺少另一个构建步骤或其他定义文件?

    0 回复  |  直到 4 年前
        1
  •  1
  •   Marcin    4 年前

    我的假设是DynamoDB表不是在AWS测试docker容器中本地创建的。在本地测试DynamoDB时,我是否缺少另一个构建步骤或其他定义文件?

    没错。 sam local invoke 仅用于在本地调用lambda函数,而不是创建本地DynamoDb。

    本地DynamoDb应该手动设置,例如使用docker镜像或java:

    如何将本地函数与本地DynamoDb一起使用,请参阅AWS博客文章和其他资源,例如: