1
votes

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

1
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
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?Mark_Dev
Boto3 iot-data client is definitely the way to go.Mark B
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.Mark_Dev
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

1 Answers

1
votes

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.