Dent7777
Dent7777

Reputation: 220

How to access existing Amazon DynamoDB Table?

General Problem

I have followed this tutorial on creating and accessing an Amazon DynamoDB from an Android application, and then adapted it for use in an app I am writing. It works great despite the difficulties I faced getting it up and running. However, I would also like to be able to access the database using a python script running on my Raspberry Pi.

I have found this tutorial, but it seems to only describe how to interact with a local DynamoDB table.

Specific Problem

The following code connects and writes an item to a DynamoDB table. I can't find any sort of endpoint URL for my Amazon DynamoDB, only the ARN, and there is no passing of a password or username as I use in my App.

# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            if o % 1 > 0:
                return float(o)
            else:
                return int(o)
        return super(DecimalEncoder, self).default(o)    

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

table = dynamodb.Table('Movies')    

title = "The Big New Movie"
year = 2015    

response = table.put_item(
   Item={
        'year': year,
        'title': title,
        'info': {
            'plot':"Nothing happens at all.",
            'rating': decimal.Decimal(0)
        }
    }
)

I have searched for any sort of instructions for connecting to an Amazon DynamoDB instance, but everything I have found describes a local table. If anyone can give advice for the specific issue or recommend a tutorial to that effect, I would appreciate it immensely.

Upvotes: 2

Views: 5512

Answers (1)

F_SO_K
F_SO_K

Reputation: 14799

Change

dynamodb = boto3.resource('dynamodb',endpoint_url="http://localhost:8000") 

To

dynamodb = boto3.resource('dynamodb',region_name='REGION') 

Where REGION is the name of your dynamodb region, such as 'us-west-2'. It will then connect to AWS DynamoDB instance.

EDIT: If you haven't do so already, you will need to setup your AWS Credentials. There are several options for this. One option is to use environment variables

Boto3 will check these environment variables for credentials:

AWS_ACCESS_KEY_ID The access key for your AWS account.

AWS_SECRET_ACCESS_KEY The secret key for your AWS account.

Upvotes: 6

Related Questions