1

I'm having a bit of trouble sending MQTT messages in an Alexa skill. It is a custom skill hosted by Alexa and the back end is written in python. I need to be able to communicate via mqtt with a device that has already been registered on aws, the device in question publishes and receives (tested on aws) and now I should be able to interact with my alexa skill.

        client = boto3.client('iot-data',region_name='eu-west-1')
        response = client.publish(
            topic='esp32/sub',
            payload=json.dumps({"message":"on"})
        )

this is the code i used and it should post the message but looking at the logger (through cloudwatch) i have this error:

[ERROR] 2023-06-04T08:53:16.221Z    0f58e1f8-aee8-43bf-a10c-d237592577d6    An error occurred (ForbiddenException) when calling the Publish operation: None

I honestly don't know where to look, other users have also had a similar problem but the conversations are dated and I can't implement the proposed solutions. For example text or text where users suggest to attach a policy to the lambda function, but there are no "buttons" in the amazon developer console that allow me to do this

jaster
  • 21
  • 3

1 Answers1

1

As we say in Italy: "I played it and sang it". One of my best friend often says that you had to click on all the buttons to understand the right way, so i did. In the amazon developer console click on "aws itegrate", it will pop-up a window with a link step by step instructions . I've logged on aws (where i already have the ESP32 registered) and followed the instructions. Then, in the lamba_function.py on the amazon developer console I've only inserted this and worked

    sts_client = boto3.client('sts')
    assumed_role_object=sts_client.assume_role(RoleArn="<Your AWS resource role ARN>", RoleSessionName="AssumeRoleSession1")
    credentials=assumed_role_object['Credentials']
    
    client = boto3.client('iot-data',
              aws_access_key_id=credentials['AccessKeyId'],
              aws_secret_access_key=credentials['SecretAccessKey'],
              aws_session_token=credentials['SessionToken'],
              region_name='eu-west-1')
    
    print(client)
    payl=json.dumps({"message":"on"})

    response = client.publish(
        topic='esp32/sub',
        payload=payl
    )
jaster
  • 21
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 04 '23 at 18:16