Solvedaws sam cli AWS::Serverless::SimpleTable not Created in DynamoDB Local

Based on the spaces vs. tabs example https://aws.amazon.com/blogs/aws/new-aws-sam-local-beta-build-and-test-serverless-applications-locally/

It appears the SAM local YAML configuration with AWS::Serverless::SimpleTable should automatically create the table in DynamoDB Local, which in my case it is not. The voting lambda works as intended if I manually create the table with AWS CLI or the Dynamo Shell.

Should this create our table?

The specific output with error stack returned is:

2017/08/31 16:35:47 Invoking lambda.lambda_handler (python3.6)
START RequestId: 7477bb46-6a7f-42bb-89bd-8819de055637 Version: $LATEST
dynamodb.Table(name='spaces-tabs-votes')
An error occurred (ResourceNotFoundException) when calling the UpdateItem operation: Cannot do operations on a non-existent table: ResourceNotFoundException
Traceback (most recent call last):
  File "/var/task/lambda.py", line 29, in lambda_handler
    ReturnValues='ALL_NEW'
  File "/var/runtime/boto3/resources/factory.py", line 520, in do_action
    response = action(self, *args, **kwargs)
  File "/var/runtime/boto3/resources/action.py", line 83, in __call__
    response = getattr(parent.meta.client, operation_name)(**params)
  File "/var/runtime/botocore/client.py", line 310, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/var/runtime/botocore/client.py", line 599, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.errorfactory.ResourceNotFoundException: An error occurred (ResourceNotFoundException) when calling the UpdateItem operation: Cannot do operations on a non-existent table

END RequestId: 7477bb46-6a7f-42bb-89bd-8819de055637
REPORT RequestId: 7477bb46-6a7f-42bb-89bd-8819de055637

Note: the table name is printed out for verification of correct Environment Variable usage

Here's my configurations:

template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  VotesTable:
    Type: "AWS::Serverless::SimpleTable"
  VoteSpacesTabs:
    Type: "AWS::Serverless::Function"
    Properties:
      Runtime: python3.6
      Handler: lambda.lambda_handler
      Policies: AmazonDynamoDBFullAccess
      Environment:
        Variables:
          TABLE_NAME: !Ref VotesTable
      Events:
        Vote:
          Type: Api
          Properties:
            Path: /
            Method: post
        Query:
          Type: Api
          Properties:
            Path: /
            Method: get

I tried running DynamoDB local both as:

docker run -p 8000:8000 dwmkerr/dynamb  -sharedDb

and using

java -Djava.library.path=./DynamoDBLocal_lib/ -jar DynamoDBLocal.jar -sharedDb

Same result for both.

Initializing DynamoDB Local with the following configuration:
Port:	8000
InMemory:	false
DbPath:	null
SharedDb:	true
shouldDelayTransientStatuses:	false
CorsParams:	*

I am connecting to dynamo okay though, as seen by:

$ aws dynamodb list-tables --endpoint-url http://127.0.0.1:8000
{
    "TableNames": []
}

The votes_table assignment appears:

#lambda.py

import os
import json
import boto3

if os.getenv('AWS_SAM_LOCAL'):
  votes_table = boto3.resource('dynamodb', endpoint_url='http://docker.for.mac.localhost:8000/').Table(os.getenv('TABLE_NAME'))
else:
  votes_table = boto3.resource('dynamodb').Table(os.getenv('TABLE_NAME'))

def lambda_handler(event, context):
    print(votes_table)
.
.
.

And this is invoked by

TABLE_NAME=spaces-tabs-votes sam local start-api

then

curl -d '{"vote": "spaces"}' http://127.0.0.1:3000/

on

macOS Sierra 10.12.6
aws-cli/1.11.142 Python/3.6.2 Darwin/16.7.0 botocore/1.7.0
sam version 0.2.0
Docker version 17.06.1-ce, build 874a737

Thank you!

17 Answers

✔️Accepted Answer

@PaulMaddox wrote:
Sorry for the confusion. SAM Local currently only does anything with AWS::Serverless::Functionresources, other resources are ignored. You will need to create the table.

Seems to be an important limitation that should be noted in the docs.

Other Answers:

There are big issues with the tutorial. Very frustrating for me, and potentially other people new to the tech.

Actually getting this to work requires

  • getting dynamodb-local to work with sam, using a solution like that suggested by @heitorlessa
  • manual creation of the dynamodb table

Figuring out those steps helped me learn, but the tutorial does not even hint that they will be necessary. If you just follow the tutorial, it won't work, and that makes it a bad tutorial.

@mrrobby if that helps, I simply user DynamoDB Local in a Docker Container and use both SAM Local and DynamoDB Local in the same Docker Network.

I also use a Bootstrap script to create my table with same basic data.

DynamoDB Local in a docker container

docker run -d -v "$PWD":/dynamodb_local_db -p 8000:8000 --name dynamodb cnadiminti/dynamodb-local

Inside my code I have a simple check for SAM_LOCAL env variable and initialize my DynamoDB differently:

# Quick sanity checks and predefined local dev
if os.getenv("AWS_SAM_LOCAL", ""):
    ddb = boto3.resource('dynamodb',
                         endpoint_url="http://dynamodb:8000")
    ddb_table = "users"
    # ret = requests.get("https://google.com")
    # print("Status --> ", ret.text)
else:
    ddb = boto3.resource('dynamodb')
    ddb_table = os.getenv("TABLE_NAME", None)

Source code: https://github.com/heitorlessa/sam-local-python-hot-reloading

@revmischa IMO dynamodb-local support would be very welcome, specially for reading the DynamoDB schema from the template YAML, otherwise, we need to manually provision the DynamoDB tables with custom scripts, duplicating in somehow the schema definition already in place in the template YAML. Besides that, we need also to manually configure it to run within the same network as the lambda Docker container.

The more SAM local can provision locally, the less we need to configure.

I don't want compare one with the other, but sls, does all of that, you define your DynamoDB schema, and it can provision DDB locally or on AWS, that's very handy. I would love to have that in SAM local.

I just read Tabs or Spaces tutorial and I was wondering if the table should be created or not. And YES! That's kind of confusing. I had to google a lot to figure out that that's not currently supported.

As @phstc pointed The more SAM local can provision locally, the less we need to configure, and the better.

Maybe an integration with LocalStack to provision all services we require would be great.

Related Issues:

59
aws sam cli Error when installing python version on MACOS
Try with --user flag? Description: I got an error when trying to install the new version on MacOS St...
27
aws sam cli AttributeError in arn_generator.py
I am a new user following the hello world tutorial linked from the README and hit this issue I don't...
26
aws sam cli fork/exec /var/task/main: permission denied
Not sure if this is the case here I successfully deployed a golang lambda api service using sam pack...
24
aws sam cli AWS::Serverless::SimpleTable not Created in DynamoDB Local
@PaulMaddox wrote: Sorry for the confusion SAM Local currently only does anything with AWS::Serverle...
23
aws sam cli sam build fails when required to install some 3rd party libraries in requirements.txt
For those that don't want to run the build inside a container: pip install wheel fixed it for me wit...
16
aws sam cli sam init errors with "No module named functools_lru_cache"
I had the same problem today Resolved be uninstalling and installing backports.functools_lru_cache ...
15
aws sam cli Local API Gateway responds with Internal Server error
Your timeout looks 3s try to increase in your template. Hi ...
9
aws sam cli sam local: --env-vars parameter does not work with "start-api" subcommand
If it helps anyone I found out that by setting empty environment variables in the template.yml ...
3
aws sam cli Conditionals, Refs, etc. in template yaml don't appear to be evaluated
Should this caveat be mentioned in the documentation? It took a lot of searching to hunt down this c...
373
Zappa bad magic number in 'application': b'\x03\xf3\r\n': ImportError
I was able to fix this too by doing find -name \*.pyc -delete Mine definitely wasn't caused by PY2 -...
94
Zappa AttributeError: 'module' has no attribute 'get_installed_distributions'
You probably have a newer pip they made changes to their public api top level functions recently Try...
22
kong how to let kong start automatically
you can use systemd for Control kong service First create kong.service file : and then put this line...
19
serverless express [Question] Internal server error when request method OPTIONS
With the help from staff on AWS forums I've resolved it by adding: which now returns a correct 200 r...
14
spring cloud gateway Spring-cloud-gateway application not starting up.
Gateway is not compatible with org.springframework.boot:spring-boot-starter-web ...
14
spring cloud gateway io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record:
You usually see this when you have a http vs https mismatch I encountered this problem when my ssl r...
14
serverless express Async handler doesn't work on node 8.10
PR for this here #173 Note that I plan on improving the interface in a future breaking change (aws-s...
13
Zappa Error loading psycopg2 module on Python 3.6 (when on Lambda)
I solved the issue by installing psycopg2-binary Context Shiny new Zappa 0.41 and Python 3.6 Django=...
13
emissary Dev Portal attempts to find docs for every mapping by default
+1 for this ticket I would also like to disable this feature as it is generating large numbers of lo...
13
spring cloud gateway Cors Pre Flight Request
To version pre to 2.1.3.RELEASE this works for me: Spring Cloud Version: Greenwich.RELEASE To make m...
12
serverless next.js Started getting the Error: Cannot find module '@sls-next/next-aws-cloudfront'
Hi folks please pin the version number in your serverless.yml like it states in the README For examp...
12
serverless express Can't get the API Gateway event object
In case any other lost souls who are encountering Missing x-apigateway-event or x-apigateway-context...
11
spring cloud gateway Doubled CORS headers after upgrade to Greenwich
This works for me No need to add any bean. Some of the legacy back ends behind our gateway have thei...
10
spring cloud gateway Preflight Request
@hnxuruochen Just did a workaroundd below to set RoutePredicateHandlerMapping CORS configuration ...
7
serverless next.js Using webpack 5 causes 503
Same for 10.2.3 Using webpack 5 returns the following from Cloudfront: Using versions: ...
6
serverless next.js Error: Source and destination must not be the same. When using useServerlessTraceTarget
I was also facing the same issue (getting Error: Source and destination must not be the same) and sp...
4
kong request-transformer plugin fails if multipart/form-data contains file
@arvileino that's really valuable Summary When request-transformer plugin enabled multipart/form-dat...
4
spring cloud gateway How to modify request / response body?
I didn't get how to change the response body I have a json in the body and I want to add a property ...
4
Zappa Error when deploying AWS Lambda with Zappa package
I seems that https://pypi.org/project/troposphere/ causes the problem: release 3.0.0 does not instal...
3
serverless next.js 503 when using image optimization
It should be fixed now in@sls-next/serverless-component@1.19.0-alpha.19 after also include 7256ab9 W...
480
aws cli Invalid Syntax Error when running any AWS command.
Leaving this here in case someone comes across this via google like me: I had the same issue with th...
328
amplify js Uncaught ReferenceError: global is not defined in latest Angular 6 RC
Just for reference I have passed through this issue with adding these lines on my index.html head: ...
219
aws cli aws s3api create-bucket throws error for us-east-2
For those of you looking to create a bucket via the CLI run this: aws s3api create-bucket --bucket d...
210
aws sdk js Typescript error: Cannot find name 'Buffer'/'http'/'https'
@pvamshi So I was able to reproduce your issue Hi! First I want to thank you for this SDK ...
171
aws cli aws ssm put-parameter performs an HTTP GET request when the value param is an url
Just kill this 'feature' seriously Even apart from security concerns it certainly violates the princ...
151
serverless How do I enable CORS?
@lakinducker Thanks No problem! I updated your comment with the corresponding markdown and now the i...
136
terragrunt Upgrading to Terraform 0.12: separate configuration file for Terragrunt?
Hi all Hi! I'm one of the engineers at HashiCorp who works on Terraform Core As you might be aware ...
122
amplify cli Many-To-Many
You can implement many to many yourself using two 1-M @connections and a joining @model ...
120
aws cli How to describe instances from all regions?
For people arriving here from Google here's one way to list all your instances across all regions wi...
114
amplify js fetch is not defined
nodejs fix: I'm using amazon-cognito-auth-js with my express app and I'm following the case 1 exampl...
109
terraform aws eks Error: Post "http://localhost/api/v1/namespaces/kube-system/configmaps": dial tcp 127.0.0.1:80: connect: connection refused
this fixed it for me thanks @cidesaasoptics I am started getting this issue: All my code were workin...
106
amplify js Error: No credentials, applicationId or region
I had the same issue (running on the latest Amplify v3) and worked around it by changing the followi...
104
amplify js Is it possible to get cognito user attributes in Lambda/cloud logic ?
I have been looking around for a while I feel the answers here didn't really answer the problem ...
96
sops Cannot decrypt with GPG 2.2.5 and SOPS 3.0.0
The problem suddenly re-occured.. I think it has to do with the gpg-agent For the moment this solved...
93
serverless Error: spawn java ENOENT
Have you tried running sls dynamodb install? This downloads the DynamoDb libs you need. ...
88
serverless Schedule event not created when supplying options
Sorry this was user error and I actually just had the wrong indentation Correct indentation ...
83
amplify cli aws-exports.js is not generated
Even after the third read I find it utterly confusing and I have usability issues too ...
83
amplify js Auth Error: Amplify has not been configured correctly using Nuxt.js
I 'm having the same issue in aws-amplify: ^3.0.11 I found out Auth module didn't load configs of aw...
81
serverless chrome NSS_VersionCheck("3.26") failed
I have done to fix this in Ubuntu 16.04 by reinstalling libnss3 Hello I get the below at runtime whe...
77
kubespray After the certificate expires how use kubespray to renew certificate
@kerOssinas you are right the upgrade-cluster.yml of Kubespray will also rotate the certificates ...