Welcome back! I'm thrilled to see you here again.
Are you still curious about how to set up your IoT devices and store their data in AWS cloud? I know I am!
Whether you're a tech expert or a newbie, don't worry, because I've got your back. In this article, we're going to cover the general overview of installing IoT devices, connecting them with AWS IoT Core, and storing their data in an AWS Timestream database using Infrastructure as Code (IaC).
I promise this is going to be a fun and exciting ride, so get ready to buckle up and dive in!
Connecting with AWS IoT Core
Before we can connect our devices to AWS IoT Core, we need to get them installed first! Don't worry, the process is straightforward, but it will vary depending on the type of device you're working with. Generally speaking, you'll need to follow these steps:
Step 1: Choose your device - there are tons of IoT devices out there, from simple sensors to complex devices with built-in processing power. Do your research and test out multiple devices to find the one or ones that best fit your needs. You could even use multiple devices for a single project - the possibilities are endless!
Step 2: Assemble your device - this is where the fun begins! Sometimes documentation can be a bit outdated, so make sure to follow the manufacturer's instructions as closely as possible to assemble your device.
Step 3: Connect your device to the internet - in today's world, it's easy to connect your device to the internet using Wi-Fi, Ethernet, or cellular data. In Toronto, Canada, all the major telecommunication providers offer these types of services. Just be sure to read the manufacturer's instructions and ensure that your device has the necessary connectivity features for best performance.
Now that we've finished the initial setup process, it's time to connect our devices to AWS IoT Core and get things rolling!
To start, we'll need to create an AWS IoT thing that will generate an X.509 certificate, public key and private key, which you will need to download and save securely. You will then need to configure your device to use this certificate and key to authenticate with AWS IoT Core. Don't worry if these terms sound foreign to you. You can always read more about them in my previous blog post!
Next, we'll create an IoT document policy that enables secure communication between our IoT devices and AWS IoT Core. With this policy in place, we'll be able to send and receive data without any issues.
Finally, once we've deployed our AWS IoT thing, collected the three certificates, and attached the policy to the thing, it's time to connect both our IoT devices and AWS IoT core. The devices will need the private certificate, ca-certificate, and the AWS IoT Core endpoint to establish a connection.
Are you ready to dive deeper into the world of Terraform? Let's get started!
This code block below will deploy all the resources we mentioned earlier.
Get ready to see some magic happen!
#AWS-IoT-Thing
resource "aws_iot_thing" "iot_core" {
name = "your-iot-core-thing"
}
#AWS-IoT-certificates
resource "aws_iot_certificate" "iot_certificate" {
active = true
}
#AWS-IoT-certificates-attachment
resource "aws_iot_thing_principal_attachment" "iot_attachment" {
principal = aws_iot_certificate.iot_certificate.arn
thing = aws_iot_thing.iot_core.name
}
#AWS-IoT-policy
resource "aws_iam_policy" "iot_timestream_policy" {
name = "iot-timestream-policy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"timestream:WriteRecords"
]
Resource = [
"${aws_timestreamwrite_database.timestream_database.arn}"
]
}
]
})
}
#AWS-IoT-policy-Attachment
resource "aws_iot_policy_attachment" "iot_policy_attachment" {
policy = aws_iam_policy.iot_timestream_policy.name
target = aws_iot_certificate.iot_certificate.arn
}
Creating an IoT Rule
resource "aws_iot_topic_rule" "iot_rule" {
name = "myiotrule"
sql = "SELECT * FROM 'iot-core-topic'"
sql_version = "2016-03-23"
enabled = true
description = "Send data from the AWS IoT Core to AWS Timestream"
timestream {
database_name = aws_timestreamwrite_database.timestream_database.database_name
dimension {}
}
Creating an AWS Timestream Database
resource "aws_timestreamwrite_database" "timestream_database" {
database_name = "timestream-database-iot"
tags = {
Name = "timestream-database-iot"
}
}
If you are not familiar with Timestream Database you can always read more about it in my previous blog post!
Please note that the above code is just an example and you should modify it to fit your security regulations and specific use case.
Testing-Time
In this section, you'll get some hands-on experience collecting and storing data in the AWS cloud. I've created a simple Bash script that generates random temperature and humidity values and publishes them to an IoT topic using the AWS CLI V2. This script was inspired by this great article, so make sure you have the right CLI version installed!
You might be asking yourself, How do you run this Bash script? Don't worry it's easy! In the CLI, you just need to paste this command: sh sensors.sh
, and hit enter
. You should see random data being generated, as shown in the screenshot below.
Publishing data 1/10 to AWS IoT topic iot-core-topic:
temperature: 87
humidity: 86
Publishing data 2/10 to AWS IoT topic iot-core-topic:
temperature: 20
humidity: 73
Publishing data 3/10 to AWS IoT topic iot-core-topic:
temperature: 39
humidity: 97
Publishing data 4/10 to AWS IoT topic iot-core-topic:
temperature: 68
humidity: 43
Publishing data 5/10 to AWS IoT topic iot-core-topic:
temperature: 55
humidity: 16
Publishing data 6/10 to AWS IoT topic iot-core-topic:
temperature: 67
humidity: 87
Publishing data 7/10 to AWS IoT topic iot-core-topic:
temperature: -19
humidity: 62
If you are seeing the same result in your CLI, then you should be ready to see the data coming into the AWS IoT Core MQTT test client. See the screenshot below.
We still have one more resource to check: the AWS Timestream Database. Now that we have confirmed that we are receiving data through the MQTT connection, we should be able to query and analyze the data in our AWS Timestream Database table using simple SQL query such as SELECT * FROM "timestream-database-iot"."timestream-database-iot-table"
, as shown in the screenshot below.
You can find the repository and instructions on how to successfully deploy each database in this series of articles, as well as how to complete the AWS IoT project portion of the series, right here.
Conclusion
Congratulations! You have successfully learned a general overview of how to install IoT devices, connect them with AWS IoT Core, and push incoming data into an AWS Timestream database. With AWS IoT Core, you can easily manage and secure your IoT devices, and with AWS Timestream, you can store and analyze time-stamped data. I hope this guide has been helpful and that you're excited to start your own IoT project. Don't forget to share your experiences with everyone and let me know what you've built!
Top comments (2)
The combination of AWS IoT Core and Timestream makes it easy to collect data, send alerts, and manage fleets of devices.
Yes, one can use both resources in many ways.
Thank you for sharing!