You don't need AWS credentials to try DynamoDB
Table of Contents
While writing code for DynamoDB, I started by creating a class to fake how DynamoDB could work, there were a lot of flaws in the logic, but it worked - it was mostly just putting dictionaries into a global dict.
This worked okay when all I had to do was put/get items, but when it came time to use other things, such as query
with different filters, variable names and begins_with
statements, it became hard to use the fake class.
Luckily, AWS created DynamoDB Local, which allows you to setup a local DynamoDB that works. You can use all the commands available in your SDK and test your code as if using your AWS account.
Using Docker
DynamoDB Local requires you to have the Java Runtime Environment. If you use Java and have JRE installed, you can follow the installation details in the DynamoDB Local documentation.
I didn't want to install the Java Runtime Environment on my computer, so Docker was the way to go. This example will use the most straightforward recipe to get DynamoDB Local installed.
Let's start by creating a docker-compose.yml
file and adding all the required things:
yaml1version: "3.8"2services:3 dynamodb:4 image: "amazon/dynamodb-local:latest"5 container_name: dynamodb6 ports:7 - "8000:8000"8 volumes:9 - "./docker/dynamodb:/home/dynamodblocal/data"10 working_dir: /home/dynamodblocal11 command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data"12 environment:13 - AWS_ACCESS_KEY_ID=""14 - AWS_SECRET_ACCESS_KEY=""15 - AWS_SESSION_TOKEN=""16 - AWS_DEFAULT_REGION=us-east-1
Note that we are passing some environment variables, they need to be set, but you don't need your real AWS credentials.
Now, all we need to do is run the docker-compose up
to pull the image and run DynamoDB Local. DynamoDB will be available at http://localhost:8000
.
Using DynamoDB Local
Now that we have the database running locally, we need to do one more thing - use it instead of the AWS service. When creating our client or resource, we need to pass endpoint_url="http://localhost:8000"
In this article, I'll use the Python library aioboto3
.
python1import aioboto2
3async def put_item(item: dict):4 """Put item in the local dynamodb."""5 session = aioboto.Session()6 async with session.resource("dynamodb", "us-east-1", endpoint_url="http://localhost:8000") as dynamodb:7 table = await dynamodb.Table("Books")8 9 await table.put(Item=item)
If you run this command, you will see that it will fail because the table doesn't exist in your DynamoDB Local. You will have to create a new table before using your local database.
Creating a table
Let's copy the code from the example in the How to create and use indexes in DynamoDB article. We can create a new table with:
python1async def create_table_and_index():2 """Create DynamoDB table with index."""3 session = aioboto3.Session()4 async with session.resource("dynamodb") as dynamodb:5 table = await dynamodb.create_table(6 TableName="Books",7 KeySchema=[8 {"AttributeName": "BookTitle", "KeyType": "HASH"}, # Partition key9 {"AttributeName": "Publisher", "KeyType": "RANGE"}, # Sort key10 ],11 AttributeDefinitions=[12 #13 # Table attributes.14 #15 {"AttributeName": "BookTitle", "AttributeType": "S"},16 {"AttributeName": "Publisher", "AttributeType": "S"},17 #18 # Index attributes.19 #20 {"AttributeName": "Author", "AttributeType": "S"},21 ],22 GlobalSecondaryIndexes=[23 {24 "IndexName": "author-index",25 "KeySchema": [26 {"AttributeName": "Author", "KeyType": "HASH"},27 {"AttributeName": "Publisher", "KeyType": "RANGE"},28 ],29 "Projection": {30 "ProjectionType": "ALL",31 },32 }33 ],34 # There are required on table creation35 ProvisionedThroughput={36 "ReadCapacityUnits": 10,37 "WriteCapacityUnits": 10,38 },39 )40
41 await table.wait_until_exists()
Once you run the above command, if you have the AWS CLI installed locally, you can run the following command in your terminal to confirm that the table was indeed created:
bash1aws dynamodb list-tables --endpoint-url http://localhost:8000
You should see our Books
table. Now you can run the put_item
to add a new item to the database.