TS Lambda in VPC cannot publish to IoT Core

17 views Asked by At

Here is the relevant code in the lambda ():

import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import AWS from 'aws-sdk'

 @param {Object} event - API Gateway Lambda Proxy Input Format
 @returns {Object} object - API Gateway Lambda Proxy Output Format

export const lambdaHandler = async (
    event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {


    try {

        let body: any
        let routeKey = (event as any).path

        if (routeKey === "/iot-test") {

            // Connection for IOT
            const iotdata = new AWS.IotData({
                endpoint: 'my IoT endpoint f5'
            });


            const params = {
                topic: "test/lambda",
                payload: "POC lambda reaching IOT topic"
            };

            let res = await iotdata.publish(params, (err: any, data: any) => {
                if (err) {
                    console.error("Error occurred during iot", err);
                } else {
                    body = { message: "Successfully published", res }
                }
            });
 } catch (error) {
        // Handle any errors that occurred during the operation
        console.error('Error:', error);
        // Return an error response
        return {
            statusCode: 500,
            headers: {
                "Access-Control-Allow-Origin": "*",
            },
            body: JSON.stringify({
                error: 'Error completing request',
                message: error
            }),
        };
    }
};

The lambda is in VPC a1 in security group b1 with subnets d1,d2,d3. It has a role with IoT full access. Following the guidelines from this post. I have created a VPC endpoint in VPC a1 in security group b1 with subnets d1,d2,d3. The main DNS name from that endpoint is e5. Then, I have created a private hosted zone in Route53 and made the hosted zone name my AWS IoT endpoint f5 (retreived via: aws iot describe-endpoint --endpoint-type iot:Data-ATS). Then I created a record, left out the domain name (so it would use the hosted zone name), set the type to A, value/route traffic to Alias to VPC endpoint, selected the correct region, and put in the endpoint DNS name e5 then created the record. When I point my lambda to my aws IoT endpoint f5 and hit the correct route, it just hangs and gives me no feedback (postman -> API gateway -> lambda) even though it is in a try-catch - indicating that something is just hanging.

I have gone over this connection a few times making sure I have the endpoints in the right places, that I can hit the lambda in the route normally and get a test string back, but I am not sure what else to try.

0

There are 0 answers