0
const AWS = require('aws-sdk');

exports.handler = (event) => {
    AWS.config.update({region: 'eu-west-1'});
    var cw = new AWS.CloudWatch({apiVersion: '2010-08-01'});

    var alarmStatus = {};
    cw.describeAlarms({}, function(err, data) {
        if (err) {
            console.log("Error", err);
        } else {
            data.MetricAlarms.forEach(function (item, index, array) {
                var pair = {[item.AlarmName]: item.StateValue};
                alarmStatus = {...alarmStatus, ...pair};
                console.log(alarmStatus);
            });
        }
    });

    const response = {
        statusCode: 200,
        body: JSON.stringify(alarmStatus),
    };
    return response;
};

I want to send response back to the caller of the lambda function, but since describe alarm function of cloudwatch is asynchronous, I am getting empty response in the body. How can I wait for the function to execute before returning the response?

Bharthan
  • 1,458
  • 2
  • 17
  • 29

0 Answers0