IOT core mqtt over websockets - DNS resolution

0

Hello

We would like to assign a Route 53 domain name to IOT core end point (as this varies based on region) so that the client side doesn't have to change . However if we pass this route53 domain url to AWS IOT Core JS client SDK, it is unable to resolve this domain resolution , before attempting to connect via wss. e.g. route 53 - iotcore-yy.xx.com -> will map to IOT end point

  1. Is there any configuration available in IOT Core SDK to resolve this first , before sdk attempt mqtt connection via web sockets?
  2. If not, do you expect client to resolve this first before passing the value to SDK?

Thanks Suresh

SS
asked 3 months ago189 views
1 Answer
0

Hi SS,

Please try this below solution once I hope it will help to resolve your issue

1. Set Up Route 53 with Your Custom Domain

Create a CNAME Record:

  • In the Route 53 console, create a CNAME record that maps your custom domain (iotcore-yy.xx.com) to the AWS IoT Core endpoint for your region (e.g., xxxxxxxxxxxx.iot.<region>.amazonaws.com).

2. Update AWS IoT Core SDK Configuration

  • AWS IoT Core SDKs don't inherently resolve custom domains. Therefore, you need to ensure that the client resolves the custom domain to the IoT endpoint before passing it to the SDK. Here's a step-by-step approach:

For AWS IoT Core JS Client SDK

Resolve the Custom Domain:

  • Use a DNS resolution library or a built-in function to resolve your custom domain to the actual AWS IoT endpoint.

Pass the Resolved Endpoint to the SDK:

  • Once resolved, pass the actual endpoint to the AWS IoT Core JS Client SDK.

Here's a sample implementation:

const dns = require('dns').promises;
const AWSIoTData = require('aws-iot-device-sdk');

// Custom Route 53 domain
const customDomain = 'iotcore-yy.xx.com';

async function resolveDomain(domain) {
  try {
    const addresses = await dns.resolveCname(domain);
    return addresses[0]; // Assuming the first CNAME record is the target IoT endpoint
  } catch (error) {
    console.error('DNS resolution failed:', error);
    throw error;
  }
}

async function connectToIoT() {
  try {
    const resolvedEndpoint = await resolveDomain(customDomain);
    console.log('Resolved endpoint:', resolvedEndpoint);

    const device = AWSIoTData.device({
      host: resolvedEndpoint,
      clientId: 'your-client-id',
      protocol: 'wss',
      accessKeyId: 'your-access-key-id',
      secretKey: 'your-secret-access-key',
      sessionToken: 'your-session-token', // Optional, if using temporary credentials
    });

    device.on('connect', () => {
      console.log('Connected to IoT Core');
      // Your logic here
    });

    device.on('error', (error) => {
      console.error('Connection error:', error);
    });
  } catch (error) {
    console.error('Failed to connect to IoT Core:', error);
  }
}

connectToIoT();

Explanation

DNS Resolution:

  • The resolveDomain function uses Node.js's dns module to resolve the CNAME record for your custom domain.

Connect to AWS IoT Core:

  • The connectToIoT function resolves the custom domain to the actual AWS IoT endpoint and then initializes the IoT device using the resolved endpoint.

Handle Events:

  • The device handles connect and error events to log the connection status.
EXPERT
answered 3 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