Since AWS services are paid-for, it's better to bootstrap a local instance of the DynamoDB database for development or testing your application. There is an image of DynamoDB on Docker Hub. Run the instance with this command:
docker run -it --rm --name test-dynamodb -p 8000:8000 amazon/dynamodb-local
This command creates an instance of a database and forwards port 8000 of a container to a local port with the same number.
To work with this database instance, you need the AWS CLI tool. This can be installed using the instructions from https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html. On Linux, I use the following command:
pip install awscli --upgrade --user
This command doesn't need administration privileges to be installed. After I installed the tool, I created a user with programmatic access, as detailed here: https://console.aws.amazon.com/iam/home#/users$new?step=details. You can read more about creating a user account to access the AWS API here: https://docs.aws.amazon.com/IAM/latest/UserGuide/getting-started_create-admin-group.html.
When you have a user for programmatic access, you can configure the AWS CLI using the configure subcommand:
aws configure
AWS Access Key ID [None]: <your-access-key>
AWS Secret Access Key [None]: <your-secret-key>
Default region name [None]: us-east-1
Default output format [None]: json
The subcommand asks you for your user credentials, default region, and desired output format. Fill in those fields as appropriate.
Now, we can create a table using the AWS CLI tool. Enter the following command into the console:
aws dynamodb create-table --cli-input-json file://table.json --endpoint-url http://localhost:8000 --region custom
This command creates a table from a declaration in JSON format from the table.json file in the local database, with an endpoint of localhost:8000. This is the address of the container we have started. Look at the contents of this table declaration file:
{
"TableName" : "Locations",
"KeySchema": [
{
"AttributeName": "Uid",
"KeyType": "HASH"
},
{
"AttributeName": "TimeStamp",
"KeyType": "RANGE"
}
],
"AttributeDefinitions": [
{
"AttributeName": "Uid",
"AttributeType": "S"
},
{
"AttributeName": "TimeStamp",
"AttributeType": "S"
}
],
"ProvisionedThroughput": {
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1
}
}
This file contains a declaration of a table with two required attributes:
- Uid - This stores user identifiers. This attribute will be used as a partition key.
- TimeStamp - This stores a timestamp when location data is produced. This attribute will be used as a sorting key to order records.
You can check whether the database instance contains this new table with the following command:
aws dynamodb list-tables --endpoint-url http://localhost:8000 --region custom
It prints the list of tables that the database instance contains, but our list is rather short, as we only have one table:
{
"TableNames": [
"Locations"
]
}
The database is prepared. Now, we will create a tool to add records to this table using Rust.