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
Newsletter
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
  • Google Cloud Pub/Sub vs. AWS SNS and SQS: One Java API for Both With MultiCloudJ
  • Should an AI Agent Have Write Access to Your AWS Account?

Trending

  • An Introduction to OpenID AuthZEN and authzen-enforcer, a Java SDK
  • Same Prompt, Different Test: Using AI Agent Workspaces to Make Testing Repeatable
  • Hands-On: Build a Least-Privilege Sandbox for AI Agents on Kubernetes
  • Reusable Operational Playbooks With Agent Skills

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Nov. 27, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Google Cloud Pub/Sub vs. AWS SNS and SQS: One Java API for Both With MultiCloudJ
  • Should an AI Agent Have Write Access to Your AWS Account?

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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