4

I am making a class to handle operations for a DynamoDB database. It is going to be simple database with only one table.

I am getting a NoRegionError() when running my script as python dynamo.db. However, when I just call the class in a jupyter notebook or qtconsole environment or run each of the commands in the script separately in a python shell, I get no such error.

I have added my credentials to the local .aws folder so I'm not sure why this error is occurring and how to debug it. Here is the class I'm working on:

import boto3

class Dynamo(object):

    def __init__(self, **kwargs):

        self._db_api = 'dynamodb'
        self._session = None
        self._client = None
        self._database = None
        self._region_name = kwargs.get('region_name', 'us-west-2')
        self._endpoint_url = kwargs.get('endpoint_url', 'http://localhost:8000')

        self.cached_table = {}
        self.table = None

    def connect(self):
        self._session = boto3.Session()
        print(self._region_name)
        print(self._db_api)

        if self._session:
            self._database = self._session.resource(self._db_api)


def main():

    dynamo = Dynamo()
    dynamo.connect()

    return dynamo

if __name__ == '__main__':
    dynodb = main()

And this is the error I'm getting:

Traceback (most recent call last):
  File "test.py", line 39, in <module>
    dynodb = main()
  File "test.py", line 34, in main
    dynamo.connect()
  File "test.py", line 28, in connect
    self._database = self._session.resource(self._db_api)
  File "python2.7/site-packages/boto3/session.py", line 389, in resource
    aws_session_token=aws_session_token, config=config)
  File "python2.7/site-packages/boto3/session.py", line 263, in client
aws_session_token=aws_session_token, config=config)
  File "python2.7/site-packages/botocore/session.py", line 824, in create_client
client_config=config, api_version=api_version)
  File "python2.7/site-packages/botocore/client.py", line 69, in create_client
verify, credentials, scoped_config, client_config, endpoint_bridge)
  File "python2.7/site-packages/botocore/client.py", line 222, in _get_client_args
verify, credentials, scoped_config, client_config, endpoint_bridge)
  File "python2.7/site-packages/botocore/args.py", line 44, in get_client_args
endpoint_url, is_secure, scoped_config)
  File "python2.7/site-packages/botocore/args.py", line 101, in compute_client_args
service_name, region_name, endpoint_url, is_secure)
  File "python2.7/site-packages/botocore/client.py", line 295, in resolve
service_name, region_name)
  File "python2.7/site-packages/botocore/regions.py", line 122, in construct_endpoint
partition, service_name, region_name)
  File "python2.7/site-packages/botocore/regions.py", line 135, in _endpoint_for_partition
raise NoRegionError()
botocore.exceptions.NoRegionError: You must specify a region.
Alexander Patrikalakis
  • 5,054
  • 1
  • 30
  • 48
oxtay
  • 3,990
  • 6
  • 30
  • 43

2 Answers2

1

You should create a DynamoDB client using boto3 templates:

boto3.client('dynamodb', region_name='us-west-2', endpoint_url='http://localhost:8000')

Also see this question and answer.

Community
  • 1
  • 1
Alexander Patrikalakis
  • 5,054
  • 1
  • 30
  • 48
  • That's true, but shouldn't boto3 check the local settings if it fails to find it in the code, as it does when called through ipython notebook? I want to be able to set this elsewhere and as explained in the question, I've put the region and credentials in `.aws` folder. I guess the question is why doesn't boto3 see that folder. – oxtay Feb 07 '17 at 17:43
0
boto3.session('dynamodb', region_name='us-west-2', endpoint_url='http://localhost:8000')

This will may helpful for you.

Hushen
  • 427
  • 1
  • 4
  • 15