Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localhost Endpoint to DynamoDB Local with Boto3

Although Amazon provides documentation regarding how to connect to dynamoDB local with Java, PHP and .Net, there is no description of how to connect to localhost:8000 using Python. Existing documentation on the web points to the use of the DynamoDBConnection method inside boto.dynamodb2.layer1, but this creates an incompatibility between live and test environments that use the boto3 protocol to manage dynamoDB.

In boto3, you can make a request to dynamo using the following constructor and variables set into the environment:

client = boto3.client('dynamodb') table = client.list_tables() 

Whereas the boto.dynamodb2.layer1 package requires you to construct the following:

client = DynamoDBConnection(     host='localhost',     port=8000,     aws_access_key_id='anything',     aws_secret_access_key='anything',     is_secure=False) table = client.list_tables() 

Although it is possible to create logic which determines the proper constructor based upon the local environment, I am wary of building a set of methods which treat each constructor as the same. Instead, I would prefer to use boto3 for everything and to be able to set the endpoint for dynamoDB in the environmental variables. Unfortunately, that option does not appear to be currently be available.

Is there any way to use boto3 to define a dynamoDB local endpoint (like the other languages)? Or any chance that Amazon will plan to support this feature?

like image 935
R J Avatar asked Aug 11 '15 17:08

R J


People also ask

How does Boto3 connect to DynamoDB?

Connecting AWS Python SDK (Boto3) with DynamoDBInstall the latest version of Boto3 by running the command below. This will install the Boto3 Python dependency, which is required for our code to run. Now we will connect with our local instance of DynamoDB using Python. We will use the code below to do so.

How do I connect to Dynamo database locally?

To access DynamoDB running locally, use the --endpoint-url parameter. The following is an example of using the AWS CLI to list the tables in DynamoDB on your computer. The AWS CLI can't use the downloadable version of DynamoDB as a default endpoint. Therefore, you must specify --endpoint-url with each AWS CLI command.

Can we run DynamoDB locally?

DynamoDB Local is available as a download (requires JRE), as an Apache Maven dependency, or as a Docker image. If you prefer to use the Amazon DynamoDB web service instead, see Setting up DynamoDB (web service).


2 Answers

It does support DynamoDB Local. You just need to set the appropriate endpoint such as you can do with other language SDKs

Here is a code snippet of how you can use boto3's client and resource interface via DynamoDB Local:

import boto3  # For a Boto3 client. ddb = boto3.client('dynamodb', endpoint_url='http://localhost:8000') response = ddb.list_tables() print(response)  # For a Boto3 service resource ddb = boto3.resource('dynamodb', endpoint_url='http://localhost:8000') print(list(ddb.tables.all())) 
like image 195
Kyle Knapp Avatar answered Sep 19 '22 03:09

Kyle Knapp


Note: You will want to extend the above response to include region. I have appended to Kyle's code above. If your initial attempt is greeted with a region error, this will return the appropriate '[]' response.

import boto3  ## For a Boto3 client ('client' is for low-level access to Dynamo service API) ddb1 = boto3.client('dynamodb', endpoint_url='http://localhost:8000', region_name='us-west-2') response = ddb1.list_tables() print(response)  # For a Boto3 service resource ('resource' is for higher-level, abstracted access to Dynamo) ddb2 = boto3.resource('dynamodb', endpoint_url='http://localhost:8000', region_name='us-west-2') print(list(ddb2.tables.all())) 
like image 42
Damian Wilbur Avatar answered Sep 19 '22 03:09

Damian Wilbur