Skip to main content

Example: Reading and writing in DynamoDB using the REST API with RAW

In this example we will see how to read and write in a Amazon DymanoDB using snapi.

info

If you are not familiar with RAW we recommend checking out our Getting started guide first. To use RAW, you need an account which you can create and use for free here.

What is DynamoDB

Amazon DynamoDB is a fully managed NoSQL database service provided by Amazon Web Services (AWS). It is designed to provide fast and predictable performance with seamless scalability. DynamoDB supports document and key-value data models, and offers features such as automatic scaling, backup and restore, encryption, and global tables for replicating data across multiple AWS regions.

Creating a new table

To create a new table do the following steps:

  1. Go to the AWS Console DynamoDB page.
  2. Click on"Tables" on the left pane and then on the button "Create New" on the right side pane.
  3. Set a table name, for this example we used "snapi-example".
  4. Set as partition key, for this example we used "name" and type string.
note

In case you choose to use a distinct table name or partition key, it will be necessary to modify the code to reflect the values you have opted for.

Creating AWS Access keys

The initial step towards utilizing AWS resources is to obtain an access key. To do so, you can refer to the AWS documentation instructions:

  1. Open the IAM console at https://console.aws.amazon.com/iam/.
  2. On the navigation menu, choose Users.
  3. Choose your IAM user name (not the check box).
  4. Open the Security credentials tab, and then choose Create access key

It is important to remember these values, as they will be utilized later on.

Accessing DynamoDB API

To access the table we have created previously, we will utilize the DynamoDB low-level API. You can find additional details about this in the DynamoDB low-level API documentation. Additionally, you can obtain an in-depth API reference guide in PDF format from Amazon's documentation by downloading it here.

AWS signed requests

To ensure security, access to the AWS API is carried out through signed requests. Fortunately, there is a pre-existing function within snapi: Aws.SignedV4Request that can handle this process.

Here is a prototype of the function:

Aws.SignedV4Request(
<aws_access_key_id>,
<aws_secret_access_key>,
<services>, // e.g. "logs", "iam", "ec2"
region = <region>, // e.g. "us-east-1", "eu-west-1", etc.
method = <http_verb>, // e.g. "GET", "POST"
bodyString = <body>, // http payload in case of "POST"
args = <list_of_query_parameters>, // in case of "GET"
headers = <list_of_headers> // list of http headers

Function to perform DynamoDB API requests

Using the information provided above, it is possible to create a function that can execute requests on DynamoDB.

dynamoDbRequest(action: string, data: string) =
String.Read(
Aws.SignedV4Request(
Environment.Secret("aws_access_key_id"),
Environment.Secret("aws_secret_access_key"),
"dynamodb",
region = "eu-west-1",
method = "POST",
bodyString = data,
headers = [
{"x-amz-target", "DynamoDB_20120810." + action},
{"content-type", "application/x-amz-json-1.0"}
]
)
)
note

We stored the "AWS access key ID" and "AWS secret key" generated in step 2 in secrets. Secrets can be accessed using the Environment.Secret function. Instructions on how to create secrets can be found in here.

For example to insert an an item with the name "Fido" and of type "dog" can be done with the operation "PutItem" like this:

dynamoDbRequest(
"PutItem",
Json.Print(
{
TableName: "snapi-example",
Item: {name: {S: "Fido"}, Type: {S: "dog"}}
}
)
)

Please note that the input data for the function is in JSON format, therefore, the Json.Print function is used.

To read values can be done with the "GetItem" operation like this:

dynamoDbRequest(
"GetItem",
Json.Print(
{
TableName: "snapi-example",
Key: {name: {S: "Fido"}}
}
)
)

Inserting and Reading data in a loop

Lastly, the following code provides an example of inserting a list of values and then retrieving one of those values:

dynamoDbRequest(action: string, data: string) =
String.Read(
Aws.SignedV4Request(
Environment.Secret("aws_access_key_id"),
Environment.Secret("aws_secret_access_key"),
"dynamodb",
region = "eu-west-1",
method = "POST",
bodyString = data,
headers = [
{"x-amz-target", "DynamoDB_20120810." + action},
{"content-type", "application/x-amz-json-1.0"}
]
)
)

let
pets = [
{name: "Fido", Type: "dog"},
{name: "Mr Socks", Type: "cat"},
{name: "Slim Slow", Type: "turtle"}
],
inserted = List.Transform(
pets,
(x) ->
dynamoDbRequest(
"PutItem",
Json.Print(
{
TableName: "snapi-example",
Item: {name: {S: x.name}, Type: {S: x.Type}}
}
)
)
)
in
dynamoDbRequest(
"GetItem",
Json.Print(
{TableName: "snapi-example", Key: {name: {S: "Mr Socks"}}}
)
)