This is a fun little side project that I’ve wanted to do for a while. As discussed in my article on how to ask Alex for the pool temperature (here) I mentioned that I wrote a little python script to upload the temperature to both adafruit.com and initialstate.com. I could then access the data from an AWS lambda function with little problems at all. I’ve been meaning to get the data directly into AWS, so here I am. This mini-guide will assume that you already have an AWS account.
Each entry should look something like this:
Reading:
{
tempid: "pool",
timestamp: "123456789",
temperature: "78.56"
}
- From your aws console screen select “DynamoDB”

- On the next page select “Create table”

- Create your table. In this example I am not looking to write too much unique data. What I am essentially looking to do is write log entries to the database that I can then query by timestamp. For that reason I added a sort key to my table so that I can query by dates. Word to the wise: don’t use capital letters in the partition key as it caused problems later.

- Congrats! You’ve got a table in a database. Make sure that you put your ARN in a safe spot as you will need it later.

- Still in the table you just created, go to Items and create a new item. Later on we will write a method to update the table with real data.

- Create a Policy. This has a few steps:
- Go to policy page: https://console.aws.amazon.com/iam/home?#/policies
- Click on “Create policy”
- Fill out tab 1 of the policy. Note that we are only allowing this to query the db.

- Click on “Review Policy” to move to the next page.
- Fill out the next screen:

- Click on “Create Policy” to finish the policy
- Create a Role
- Go to https://console.aws.amazon.com/iam/home?#/roles
- Click “Create role”
- Select “API Gateway”
- Click on “Next: Permissions” at the bottom of the page.
- On the next page go directly to “Next: Tags”
- On the next page go directly to “Next: Review”
- Fill out the next page:

- Click “Create Role”
- Find your role in the table and open it
- Click on “Attach policies”
- Find your policy, select it, and then click “Attach Policy”

- Run “Simulate Policy” to verify that everything is good so far. Note that the “Action” should be “query” as that is the method we will be invoking.

- Go to the API gateway and create a new API
- Open the new gateway and create a nested resource tree by clicking on “Create resources” two times. When doing it the second time you will have the first one selected.

-
Higlight the nested resource and click on “Create Method”.
- Choose “Post” in the first drop=down. Choose “Get” as the method.
- The http method will be “Post”
- Scroll down to “Mapping Templates” and enter “application/json” for the “Content-Type”
- In the area below the “Mapping Template” there is a box for a template. Add something like this:
{ "TableName": "temperature", "PrimaryKey" : "tempid", "KeyConditionExpression" : "tempid = :v1", "ExpressionAttributeValues" : { ":v1" : { "S" : "$input.params('tempid')" } } }
- Go back to “Method Execution” and click “Test”. Enter the key value you that you created earlier (in my case, “pool”) and you should have a successful test.





[…] the “How to Read from a DynamoDb” (here) to see how my table is set […]