DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Real-Object Detection at the Edge: AWS IoT Greengrass and YOLOv5
  • A Comparative Analysis: AWS Kinesis vs Amazon Managed Streaming for Kafka - MSK
  • Build a GitHub Slack Bot With AWS Bedrock and MCP, Part 2
  • Compliance Automated Standard Solution (COMPASS), Part 11: Compliance as Code, the OSCAL MCP Server Way

Trending

  • Why Your Test Automation Is Always Behind the Code And the Architecture That Fixes It
  • Building a DevOps-Ready Internal Developer Platform: A Hands-On Guide to Golden Paths, Self-Service, and Automated Delivery Pipelines
  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic
  • Data Contracts as the "Circuit Breaker" for Model Reliability
  1. DZone
  2. Data Engineering
  3. Big Data
  4. AWS IoT: Retrieving SQS Messages From a Queue

AWS IoT: Retrieving SQS Messages From a Queue

Let's set up an AWS IoT Rule to call an AWS Lambda function triggered by MQTT messages. See how it's configured with this step-by-step guides.

By 
Kevin Hooke user avatar
Kevin Hooke
·
Nov. 27, 17 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
8.8K Views

Join the DZone community and get the full member experience.

Join For Free

AWS IoT Rules have predefined rules for sending a message to an SQS Queue, but for some reason not to retrieve a message from a queue using an IoT Rule (or does it? If you know how, leave me a comment). You can easily retrieve a message using a Lambda function using the AWS SDK APIs though, and you can call a Lambda function using an IoT Rule. So let’s set that up.

Let's create an IoT Rule to call the Lambda function, which we’ll trigger with incoming MQTT messages on a topic called topic/checkForMessage:

Next, select an action from the long list of available actions. I chose ‘Invoke a Lambda function’:

Select the Lambda function we want to call. In this case, it’s one created earlier (it has to exist to show in the list. If not, press ‘Create a new Resource’ to create one):

On the next summary screen, press ‘Create Rule’ and you’re done:

To allow the IoT Rule to call the function, we need to grant the lambda:invokeFunction rule.

Per the docs, we can use the AWS CLI to add the permission like this:

aws lambda add-permission 
  --function-name "function_name" 
  --region "region" 
  --principal iot.amazonaws.com 
  --source-arn arn:aws:iot:us-east-2:account_id:rule/rule_name 
  --source-account "account_id" 
  --statement-id "unique_id" 
  --action "lambda:InvokeFunction"


To apply this to our function and rule, replace:

“function_name” : “LightsOnReceiveMessageFromQueue”

“region”: “us-east-1”

source-arn arn: aarn:aws:iot:full-arn-for-the-rule – see below

account_id: your-aws-account-id

rule_name: RetrieveSQSMessageFromQueue

“account_id”: “your-account-id”

“unique_id”: unique-id-for-this-permission


I’m not sure the AWS Console for IoT shows the ARN for IoT Rules anywhere in its pages, but you can easily list it with the AWS CLI using:

$ aws iot list-topic-rules
{
    "rules": [
        {
            "ruleArn": "arn:aws:iot:us-east-1:your-account-id:rule/RetrieveSQSMessageFromQueue",
            "ruleName": "RetrieveSQSMessageFromQueue",
            "topicPattern": "topic/checkForMessage",
            "createdAt": 1511115896.0,
            "ruleDisabled": false
        }
    ]
}


Ok, plugging in my values into the AWS CLI statement, I have a permission added.

This is it for the IoT Rule. To summarize, this allows us to:

  • Respond to incoming messages from an AWS IoT device publishing a message to an MQTT topic called topic/checkForMessages
  • When a message arrives from the device on the topic, it triggers the IoT Rule we just created
  • The rule invokes an AWS Lambda to interact with an AWS SQS Queue to pull a message from a queue.

I’ll share more details on the implementation of the Lambda to interact with the SQS queue and the implementation of the node.js app on a Raspberry Pi in upcoming posts. You’re probably wondering what this is that I’m building. Check back for my followup posts to find out!

AWS IoT

Published at DZone with permission of Kevin Hooke. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Real-Object Detection at the Edge: AWS IoT Greengrass and YOLOv5
  • A Comparative Analysis: AWS Kinesis vs Amazon Managed Streaming for Kafka - MSK
  • Build a GitHub Slack Bot With AWS Bedrock and MCP, Part 2
  • Compliance Automated Standard Solution (COMPASS), Part 11: Compliance as Code, the OSCAL MCP Server Way

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook