Unable to publish MQTT message to AWS iot Core with lambda function. Where lambda is connected to VPC. Using VPC enpoint we need to connect to IOT core and publish message

0

I have created a lambda function. Where lambda is connected to VPC with two private subnet. Without using Nat Gateway we need to create lambda function and publish a mqtt message to IoT core. We configured VPC endpoint to AWS iot core. Created private hosted zone but no use we are getting error like Timeout task

Below is the code we used to publish a message to mqt

import json
import boto3

client = boto3.client('iot-data', region_name='*******')



def lambda_handler(event, context):
    print(event)
    # TODO implement
    # Change topic, qos and payload
    response = client.publish(
        topic='esp32/sub',
        qos=1,
        payload=json.dumps({"foo":"bar"})
    )
    print(response)
    
    return {
        'statusCode': 200,
        'body': json.dumps('Published to topic')
    }
1 Answer
1
Accepted Answer

I tried it in an EC2 environment with AWS CLI.

aws iot-data publish --topic esp32/sub --payload '{"a":"b"}' --region ap-northeast-1 --debug
EndpointConnectionError: Could not connect to the endpoint URL: "https://data.iot.ap-northeast-1.amazonaws.com/topics/esp32%2Fsub"
2022-06-25 11:57:35,520 - MainThread - awscli.clidriver - DEBUG - Exiting with rc 255

Could not connect to the endpoint URL: "https://data.iot.ap-northeast-1.amazonaws.com/topics/esp32%2Fsub"

It seems to be trying to connect to the data endpoint instead of data-ats endpoint

It appears that only ats-data is provided for VPC endpoints. https://docs.aws.amazon.com/iot/latest/developerguide/IoTCore-VPC.html

I was able to Publish correctly by explicitly specifying the endpoint.

aws iot-data publish --topic esp32/sub --payload '{"a":"b"}' --region ap-northeast-1 --debug --endpoint https://a2klze32u5vkt3-ats.iot.ap-northeast-1.amazonaws.com
2022-06-25 11:57:55,896 - MainThread - urllib3.connectionpool - DEBUG - https://a2klze32u5vkt3-ats.iot.ap-northeast-1.amazonaws.com:443 "POST /topics/esp32%2Fsub HTTP/1.1" 200 65
2022-06-25 11:57:55,897 - MainThread - botocore.parsers - DEBUG - Response headers: {'date': 'Sat, 25 Jun 2022 11:57:55 GMT', 'x-amzn-RequestId': '2740292f-48b4-3fca-2fe6-11f6bbfaf895', 'content-length': '65', 'content-type': 'application/json', 'connection': 'keep-alive'}
2022-06-25 11:57:55,897 - MainThread - botocore.parsers - DEBUG - Response body:
{"message":"OK","traceId":"2740292f-48b4-3fca-2fe6-11f6bbfaf895"}
2022-06-25 11:57:55,898 - MainThread - botocore.hooks - DEBUG - Event needs-retry.iot-data-plane.Publish: calling handler <botocore.retryhandler.RetryHandler object at 0x7f2daa776790>
2022-06-25 11:57:55,898 - MainThread - botocore.retryhandler - DEBUG - No retry needed.
2022-06-25 11:57:55,898 - MainThread - awscli.formatter - DEBUG - RequestId: 2740292f-48b4-3fca-2fe6-11f6bbfaf895

I have not tried this with boto3, but it might work if you specify an endpoint.

profile picture
answered 2 years ago
profile picture
EXPERT
reviewed 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions