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')
    }
已提問 2 年前檢視次數 969 次
1 個回答
0
已接受的答案

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
已回答 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南