Skip to content

AWS Serverless Backend

Dan Lwo edited this page Jul 21, 2017 · 16 revisions

Introduction

The system architecture is shown in the figure above. We use Raspberry Pi & DHT22 as sensor device to send readings to AWS Gateway, then each reading will be sent to AWS DynamoDB to be saved as an item in a table, and simultaneously trigger an AWS Lambda function to exam the reading and send out warning alert when the reading exceed expected range.

AWS DynamoDB

First we need to create a table to store readings. Each table must have a partition key. If two items have the same value in the attribute which is selected as partition key, the later one will overwrite the previous one. To avoid readings being overwritten, we'll give each reading a uniq UUID by using a trick in API Gateway, here we just fill in the partition key space with "UUID", and add "Moment" as sort key (or whichever attribute you want).

Keep in mind that DynamoDB reserved some words for its own use, you can NOT choose these words as names of attributes. Please refer here for the reserved list and details:

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html

If you have a lot of sensor device or you're going to query it a lot, you might need to adjust Write/Read capacity to ensure expected performance.

to be continued...

AWS API Gateway

Create API

Body Mapping Templates:

https://github.com/danlwo/Temperature-Humidity-Sensor---Raspberry-Pi-DHT-22-and-AWS-Serverless-Architecture-Backend/blob/master/API_Body_Mapping_Templates

Deploy API & use API

to be continued...

AWS Lambda

Lambda is a tool to execute functions, which suits our need for a alarm that warm us when there's reading exceeding expected range.

Create a Lambda function, choose Blank Function. Set a trigger by choosing the DynamoDB table we just created, and don't forget to enable it.

Now you can write the function you want to run when DynamoDB receives a reading.

Here's a example that will send out alert email when there's reading exceeding expected range (python 2.7):

https://github.com/danlwo/Temperature-Humidity-Sensor---Raspberry-Pi-DHT-22-and-AWS-Serverless-Architecture-Backend/blob/master/sensorReadingAlarm.py