Search code examples
pythonpython-3.xamazon-dynamodbboto3moto

'Requested resource not found' error with Python Moto library


I am writing a test case in Python for DynamoDB using the moto library and I have the code as shown below:

import boto3
import pytest
from moto import mock_dynamodb2
from myapp import save_contact


@mock_dynamodb2
def test_contact_save_to_dynamo():
    assert save_contact(
        '[email protected]',
        'John Appleseed'
    ) == 200

The test fails with the following error message:

E       AssertionError: assert 'Requested resource not found' == 200

I have double checked the region and the table name and it exists in the console. When I run the code to put an item in my DynamoDB table, it successfully saves it to the table, like so:

import datetime
import boto3
from botocore.exceptions import ClientError

AWS_REGION = 'us-east-1'
DYNAMODB_TABLE = 'aprs_messages'

time_stamp = str(int(datetime.datetime.utcnow().timestamp()))

ddb_client = boto3.client('dynamodb',region_name=AWS_REGION)


def save_contact(email, name):
    try:
        response = ddb_client.put_item(
            TableName=DYNAMODB_TABLE,
            Item={
                'message_time': {'N': time_stamp },
                'email': {'S': email },
                'name': {'S': name }
                }
        )

    except ClientError as e:
        return e.response['Error']['Message']
    else:
        return response['ResponseMetadata']['HTTPStatusCode']

x = save_contact(
    '[email protected]',
    'John Appleseed'
    )

print(x)

When I run this code, I get a 200 response code and I can also see the entry in the DynamoDB table. What could be the issue here?

Package Version:

  • Python: 3.7.0
  • boto3: 1.7.84
  • moto: 1.3.6

Solution

  • When using moto, you need to create the underlying resource before performing any actions on that resource. In your case, you'll need to do a create_table before doing the put_item from within your test function.