0

I am using AWS lambda to get some data from cloudwatch metric and below is my code in lambda

var AWS = require('aws-sdk');    
AWS.config.update({region: 'ap-northeast-1'});
var cloudwatch = new AWS.CloudWatch({apiVersion: '2010-08-01'});

exports.handler = async function(event, context) {
    console.log('==== start ====');
    const connection_params = {
      // params
    };

    cloudwatch.getMetricStatistics(connection_params, function(err, data) {
        if (err){
            console.log(err, err.stack);  
        } else {
            console.log(data);      
            active_connection = data.Datapoints[0].Average.toFixed(2);
        }   
        console.log(`DDDDDDD ${active_connection}`);
    });

    console.log('AAAA');    
};

I always get 'AAAA' first and then get 'DDDD${active_connection }'.

But what i want is get 'DDDD${active_connection }' first and then 'AAAA'.

I tried to use like

cloudwatch.getMetricStatistics(connection_params).then(() => {})

but show

cloudwatch.getMetricStatistics(...).then is not a function

2 Answers 2

2

Try writing your code like this,

  1. With then
    const x = cloudwatch.getMetricStatistics(connection_params).promise();

    x.then((response) => do something);
  1. With async/await
    const x = await cloudwatch.getMetricStatistics(connection_params).promise();

Sign up to request clarification or add additional context in comments.

Comments

0

You could use util#promisify Docs

const util = require("util");
const cloudwatch = new AWS.CloudWatch();

const getMetricStatistics = util.promisify(cloudwatch.getMetricStatistics.bind(cloudwatch));

getMetricStatistics(connection_params).then(() => {})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.