Reading from an an AWS Dynamo Db via API

Oh how I heart data

Reading from an an AWS Dynamo Db via API

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"
}
  1.  From your aws console screen select “DynamoDB”

  2. On the next page select “Create table”

  3. 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.

  4. 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.

  5. 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.

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

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

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

    8. Click “Create Role”
    9. Find your role in the table and open it
    10. Click on “Attach policies”
    11. Find your policy, select it, and then click “Attach Policy”

    12. 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.

    13. Go to the API gateway and create a new API
    14. 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.

    15. Higlight the nested resource and click on “Create Method”.

      1. Choose “Post” in the first drop=down.  Choose “Get” as the method.
      2. The http method will be “Post”
      3. Scroll down to “Mapping Templates” and enter “application/json” for the “Content-Type”
      4. 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')"
        }
        }
        }
    16. 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.

      Blog Comments

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

      Add a comment

      *Please complete all fields correctly

      This site uses Akismet to reduce spam. Learn how your comment data is processed.

      Related Blogs