Ingesting data – Working with Data and Analytics
We can now send some data through the pipeline. At this time, we will create some mock data to send through. To simulate data ingestion from the AWS console, you might need to utilize another AWS service such as Lambda to push data to IoT Analytics or use the AWS SDK; so, we will use the following steps:
Navigate to the AWS Lambda service in the AWS console.
Click the Create function button.
Choose Author from scratch. Provide a name for the Lambda function (for example, PushTemperatureDataToIoTAnalytics).
For the runtime, select a preferred language. For this walkthrough, we’ll assume Python 3.8.
Under Permissions, choose or create a role that has permissions to write to IoT Analytics.
Click Create function.
Write the Lambda function, as shown next. This code mocks temperature data and pushes it to AWS IoT Analytics:
import json
import boto3
import random
client = boto3.client(‘iotanalytics’)
def lambda_handler(event, context):
# Mocking temperature data
temperature_data = {
“temperature”: random.randint(15, 35) }
response = client.batch_put_message(
channelName=’mychannel’,
messages=[
{ ‘messageId’: str(random.randint(1, 1000000)),
‘payload’: json.dumps(temperature_data).encode()}, ])
return {‘statusCode’: 200,
‘body’: json.dumps(‘Temperature data pushed successfully!’)}
The function creates a client to interact with AWS IoT Analytics using boto3. Inside the function, it generates mock temperature data, where the temperature value is a random integer between 15 and 35 degrees. This data is structured in a dictionary as can be seen in a snapshot of the AWS Lambda window below.

Figure 10.9 – AWS Lambda window for inserting code
Then, it sends this temperature data to a channel named mychannel in AWS IoT Analytics using the batch_put_message method. The message includes a unique ID, generated randomly, and the payload, which is the serialized temperature data. The function concludes by returning a success status code (200) and a message indicating the successful push of the temperature data.
Deploy the function after inserting the code.
At the top right of the Lambda function dashboard, click the Test button.
Configure a new test event. The actual event data doesn’t matter in this context since our function isn’t using it, so you can use the default template provided.
Name the test event and save it.
With the test event selected, click Test. If everything’s set up correctly, you should see an execution result indicating success, and your AWS IoT Analytics channel (mychannel in this example) should have received a temperature data point.
Having properly ingested the data, let’s now see how to monitor it.
Leave a Reply