Allyl Isocyanate
Allyl Isocyanate

Reputation: 13616

Can't test local Lambda that references a DynamoDB table when using AWS SAM

I created the "Quick Start: Web Backend" AWS SAM project that incudes a DynamoDB table using the sam init sam-app.

Following the instructions in the readme, when I try and build and invoke a lambda function that references the Dynamo table:

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

I get a Requested resource not found error:

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)"]}

The sample project contains 3 lambda functions that invoke a DynamoDb backend (trimmed for clarity):

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/"

My assumption is the DynamoDB table isn't created locally inside the AWS test docker container. Is there another build step or other definition file I'm missing to test DynamoDB locally?

Upvotes: 1

Views: 2121

Answers (1)

Marcin
Marcin

Reputation: 238111

My assumption is the DynamoDB table isn't created locally inside the AWS test docker container. Is there another build step or other definition file I'm missing to test DynamoDB locally?

That's correct. sam local invoke is only for invoking your lambda function locally, not creating a local DynamoDb.

The local DynamoDb should be setup manually, for example using a docker image or java:

How to use you local function with local DynamoDb is shown in the AWS blog post and other resources, e.g:

Upvotes: 2

Related Questions