1

I am new to AWS world, am currently developing an Alexa skill that simply publish a mqtt message to AWS IoT Core broker interacting with a previously created 'thing' and topic. Currently I am using boto3 but I am not sure that's the right path. This is the code but it is not working when deploying the Lambda and invoking the intent from Alexa.

import boto3
import json

payload = json.dumps(
{'state': 
    { 
        'desired': { 'irrigation': 'on' } 
    }
})

client = boto3.client('iot-data', region_name='us-west-2')

response = client.publish(
    topic='$aws/things/gateway/shadow/update',
    qos=1,
    payload=payload
)

CloudWatch shows no excceptions, I simply get an error response from Alexa and not receiving any message if subscribing to the same topic where the publish should be triggered.

I am using 'shadow', maybe this is not the right thing to do? Tried normal topic but not working as well. Also, I would love to debug the code before publishing. Is there an easy way to do that without using CloudWatch?

TL;TR Only found a lot of guides online that goes this way: iot core -> aws lambda. But I am actually looking for the opposite: aws lambda -> publish to iot core

Panda
  • 45
  • 9
  • The code looks OK to me. Are there no errors in the Lambda function's CloudWatch logs? I suggest adding a try/catch around the `publish()` call that logs any exceptions. Also, are you aware there is an `update_thing_shadow()` function? – Mark B Dec 16 '20 at 20:55
  • Ok I'll use try/catch and see if there are any exceptions. No errors in lambda CloudWatch logs.. I can't just receive the message published and Alexa responds by voice, there was an error with the skill execution. I am aware about that function, and I also tried it. but no luck. So is the code right? Is boto3 the right way to do this? – Panda Dec 16 '20 at 21:09
  • Boto3 iot-data client is definitely the way to go. – Mark B Dec 16 '20 at 21:17
  • Ok thank you Mark, Good to know. Please, could you tell me if there is any particular IAM Role/Policy I need to attach to the lambda in order to get the access to IoT Core? Maybe the issue is a permission issue and not in code. – Panda Dec 17 '20 at 15:09
  • 1
    You would be getting a permission error. You need to work on debugging this to figure out the issue instead of guessing. Start by triggering it directly from the Lambda console to see if any error is logged. Watch the device shadow in another browser window to see if it gets the update. This could simply be that the payload format returned isn't something your Alexa skill can handle. You need to isolate the pieces to figure out which part isn't working. – Mark B Dec 17 '20 at 15:13
  • I have a similar issue on code that has been working for months. It stop working a couple of days ago. In my case, the logs indicate the publish is timing out. I can publish to the topic from the iot test console and the client that subscribed to the topic gets the test message, but if I use the IoT test console to subscribe to the topic it does NOT receive messages the lambda function attempts to publish. Started a separate thread on this before I saw this post. – Dave Thomas Dec 18 '20 at 14:36
  • I'll decouple the problem and start debugging and see if I come up with something new. Thanks for the Tip. @DaveThomas you case is weird as you describe. maybe you did some edits in the code before getting everything stopping working? Could it be a AWS backend issue? It's the first time I work on this so I can't help much – Panda Dec 19 '20 at 16:22
  • Absolutely sure I changed nothing in either the Lambda code, nor the code on the rpi when it stopped working. It's what I use to make legacy "dumb" devices into devices that fit into the Alexa Smart Home Skill model. It had been working untouched for several months, then just stopped working. For debugging, I added a log statement in the Lambda code before and after the publish and noted the statement after the publish never was executed. Nothing else changed. I left it in that state (with the two lines that did the logging) and it started working again the following day. – Dave Thomas Dec 20 '20 at 01:17

1 Answers1

1

Also, you probably need an inline policy like this:

> {
>     "Version": "2012-10-17",
>     "Statement": [
>         {
>             "Effect": "Allow",
>             "Action": [
>                 "iot:Publish"
>             ],
>             "Resource": [
>                 "*"
>             ]
>         }
>     ] }

I added that based on another stackoverflow post when I first implemented the lambda publish a few months ago. It was working well until a couple of days ago.

Dave Thomas
  • 81
  • 10