I use AWS Service IotData
in AWS Lambda function, so I use AWS SDK, the IotData service needs to provide an iot endpoint configuration parameter when constructing IotData service, so I use another service to get endpoint, the code is as follow.
const AWS = require('aws-sdk');
const iot = new AWS.Iot();
const endpoint = iot.describeEndpoint(
{
endpointType: 'iot:Data-ATS'
}).promise();
const iotdata = new AWS.IotData({endpoint:endpoint});
exports.handler = async (event, context, callback) =>
{
// code goes here...
}
I tried to use iot function describeEndpoint
to get the endpoint, and assign the value to variable endpoint
.
I understand that it will fail, because the variable endpoint
will get a promise object. Therefore, I tried async IIFE to get endpoint:
const endpoint = (async () => {
let result = await iot.describeEndpoint(
{
endpointType: 'iot:Data-ATS'
}).promise();
return result;
})();
However, it also gets promise object as a result.
Is there any other solution to get value from an asynchronous call?
P.S The reason why I am asking is that I am trying to put variable endpoint
and iotdata
on top level, because I am afraid that these variable might be used by other functions.