9

I am building an AWS lambda with aws-sam-cli. In the function, I want to access a certain DynamoDB table. My issue is that the function comes back with this error when I invoke it locally with the sam local invoke command: ResourceNotFoundException: Requested resource not found

const axios = require('axios')
const AWS = require('aws-sdk')
AWS.config.update({region: <MY REGION>})
const dynamo = new AWS.DynamoDB.DocumentClient()

exports.handler = async (event) => {
  const scanParams = {
    TableName: 'example-table'
  }
  const scanResult = await dynamo.scan(scanParams).promise().catch((error) => {
    console.log(`Scan error: ${error}`)
    // => Scan error: ResourceNotFoundException: Requested resource not found
  })

  console.log(scanResult)
}

However, if I actually sam deploy it to AWS and test it in the actual Lambda console, it logs the table info correctly.

{
  Items: <TABLE ITEMS>,
  Count: 1,
  ScannedCount: 1
}

Is this expected behavior? Or is there some additional configuration I need to do for it to work locally? My template.yaml looks like this:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 'Example SAM stack'
Resources:
  ExampleFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs12.x
      Policies:
      - DynamoDBCrudPolicy:
          TableName: 'example-table'
reesaspieces
  • 1,600
  • 4
  • 18
  • 47

1 Answers1

7

I believe when you invoke your Lambda locally, SAM is not recognising which profile to use for the remote resources, ex: DynamoDB

Try to pass the credentials profile for your remote dynamoDB

ex:

sam local invoke --profile default

You can check the command documentation here: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-local-invoke.html

me2resh
  • 786
  • 6
  • 10
  • 2
    Thank you so much! I'd been confused because `sam local invoke` with the `AWS_PROFILE=default` environment variable hadn't worked either. Your solution worked perfectly. – reesaspieces Feb 10 '20 at 00:19
  • This did not work for me. Still getting resource not found with `sam local invoke sacribe50AAC574 --no-event --profile default --region us-east-1`. @reesaspieces can you tell me the full command you used? – 55 Cancri Apr 04 '20 at 21:22
  • @55Cancri This is what I used: `sam local invoke MyFunctionName --no-event --env-vars env.json --profile default`. So it seems to be the same as yours :/ Did you deploy the function to the actual Lambda console and see if you could access DynamoDB there? – reesaspieces Apr 05 '20 at 02:24
  • It works perfectly from the console, but not from SAM local. Did you do anything special with docker? For example, did use the sam command `sam local start-api` to start a local api gateway or the docker command `docker run -d -v "$PWD":/dynamodb_local_db -p 8000:8000 --network lambda-local --name dynamodb cnadiminti/dynamodb-local` at all? – 55 Cancri Apr 05 '20 at 10:18
  • @55Cancri Sorry, for some reason I only saw this now. I haven't used api gateway, but I do use DynamoDB Local with `docker run -p 8000:8000 amazon/dynamodb-local`. And I set up the endpoint to be `'http://docker.for.mac.localhost:8000'` (this only works if you're on a Mac). I actually wrote a blog post on it, where I wrote the exact setup. Hope this will help. https://dev.to/risafj/get-dynamodb-local-up-and-running-in-3-minutes-with-docker-5ec6#5 – reesaspieces Apr 14 '20 at 01:14
  • 2
    @55Cancri That being said, you can also hit the real DynamoDB in the cloud (not DynamoDB Local) from your local lambda if you specify the profile as @me2resh's answer says. In that case, you shouldn't specify the endpoint as `'http://docker.for.mac.localhost:8000'`. – reesaspieces Apr 14 '20 at 01:18
  • 1
    Thanks so much man, that resolved my issue. I never wanted to use a local dynamodb, I just thought that docker url was necessary. You are right, I just need to use `--profile default`. The issue I was facing was with `--env-vars env.json`. Sam fails silently if the file is not in the directory where command is run. I connect to dynamodb with the tableName as an environment variable. Hardcoding the name revealed I could connect to dynamodb, it was just the environment variables were not initialized correctly. Hopefully this saves someone else weeks of trouble. – 55 Cancri Apr 18 '20 at 18:08
  • @55Cancri No problem, glad it helped! – reesaspieces Sep 16 '20 at 04:56