You can ingest time series data from your IoT devices connected to AWS IoT Core into Amazon Timestream. This repository contains everything needed to setup and test data ingestion via an AWS IoT topic rule into Amazon Timestream:
- a Python script which generates sample data: sensordata.py
- a CloudFormation template which sets up the necessary resources in your AWS account: cfn-iot-rule-to-timestream.json
The CloudFormation template creates:
- a Timestream database and table.
- an IAM role. Part of a rule definition is an IAM role that grants permission to access resources specified in the rule's action.
- IoT topic rule that writes sensor data into Timestream database.
Note The instructions in this repository will use the US East (N. Virginia) Region (us-east-1). If you want to choose another region replace the region in the instructions with the region of your choice. You need also replace the region then in the data generating script sensordata.py
.
The data generation script generates sample data that represent fictitious sensors in buildings which measure humidity, barometric pressure and temperature.
Apart from the measurements the sensors will also report the building name and the room number where they are located.
Here is an example of what the IoT message payload will look like:
{
"temperature": 18.05670413634157,
"pressure": 45.681328608970425,
"humidity": 919.8060681910588,
"device_id": "sensor_01",
"building": "Day 1",
"room": "2.01"
}
The script is implemented in Python 3 and uses the publish API of the AWS IoT Data Plane from the boto3
module in order to ingest data into AWS IoT. In order to setup the environment for the data generating script you need to ensure the following:
- Install Python 3
- Install
boto3
- Setup authentication credentials and associate the permissions to publish to IoT (
"iot:Publish"
) and to describe the IoT endpoint ("iot:DescribeEndpoint"
) with those credentials - Launch the CloudFormation stack in your account
Note: For more information on the installation of the boto3
library and setting up of authentication credentials for use in your Python scripts please see the Quick Start and Credentials sections of the boto3
documentation.
Launch the CloudFormation stack in the region us-east-1
.
Go to the AWS CloudFormation console
- Create stack
- With new resources (standard)
- Template is ready
- Upload a template file
- Choose file
- Select
cfn-iot-rule-to-timestream.json
- Next
- Enter an arbitrary stack name, e.g.
IoTTimestreamIntegration
- Next
- Next
- Check
I acknowledge that AWS CloudFormation might create IAM resources.
- Create stack
- Wait until the status changes from
CREATE_IN_PROGRESS
toCREATE_COMPLETE
. It should take some minutes for the stack to be launched.
Start the data generating script: ./sensordata.py
Do some queries in Timestream for example. Replace DATABASE_NAME
and TABLE_NAME
with the data base name and table name that the CloudFormation stack created.
You can use the Timestream Query editor to run your queries.
- Number of rows:
select count(*) from "DATABASE_NAME"."TABLE_NAME"
- Get some rows:
select * from "DATABASE_NAME"."TABLE_NAME" limit 20
- Retrieve building names:
select distinct(building) from "DATABASE_NAME"."TABLE_NAME"
- Get temperatures in
Day 1
building
select * from "DATABASE_NAME"."TABLE_NAME"
where building = 'Day 1' and measure_name = 'temperature'
limit 20
To monitor AWS IoT activity you should enable logging.
In case something doesn't work as expected try to use Amazon CloudWatch insights:
-
Log group:
AWSIoTLogsV2
-
Filter
fields @timestamp, @message | sort @timestamp desc | limit 20 | filter ruleName = "ToTimestreamRule"