What is the correct way to use AWS Iot.describeCertificate()? I cannot access the function asynchronously

Asked

Viewed 40 times

0

I need to regain the status of an AWS-Iot-Core certificate using a Lambda function with Nodejs. According to the official documentation would need to use the function describeCertificate().

This is the code I’m using for testing:

const AWS = require('aws-sdk')
const iot = new AWS.Iot()
let cert = {}

async function descCert (params) {

  console.log("start descCert")
  console.log("params")
  console.log(params)

  await iot.describeCertificate(params, function(err, data) {
    console.log('describeCertificate - Fn')
    if (err) {
      console.log('describeCertificate - Error')
      return console.log(err, err.stack)
    }else{
      console.log('describeCertificate - data')
      cert = data
      return console.log(data)
    }
    console.log("end describeCertificate - Fn")
  })

  console.log("end descCert")
}

module.exports.testFn = async (event, context, callback) => { 

    var zzz = {
        certificateId: 'xxxx8c0891f8xxxxxx'
    }
    await descCert(zzz)
    console.log("after descCert")
    console.log(cert)

...
}

I think the mistake here is how I’m using this function with Nodejs because the control points within await iot.describeCertificate( ... are not appearing in the CloudWatch.

I was hoping to see this sequence:

  1. descstart Cert
  2. params
  3. {certificateId: 'xxxx8c0891f8xxxxxx'}
  4. describeCertificate - Fn
  5. Or describeCertificate - Error OR describeCertificate - data
  6. the same data
  7. end describeCertificate - Fn
  8. end descCert
  9. descafter Cert
  10. the same data

But what I’m getting is this sequence:

  1. descstart Cert
  2. params
  3. {certificateId: 'xxxx8c0891f8xxxxxx'}
  4. end descCert
  5. descafter Cert
  6. the same data //{}

Steps 4 to 7 do not appear in the log, that is, the function is not being called.

Where am I going wrong?

1 answer

0

Try it this way

const AWS = require('aws-sdk')
const iot = new AWS.Iot()
let cert = {}

descCert = async (params) => {

  console.log("start descCert")
  console.log("params")
  console.log(params)

  try {
    const data = await iot.describeCertificate(params).promise(); 
    console.log(data);
    return data;
  } catch(e) {
    console.log('erro fn descCert');
    throw new Error(e.message);  }

}

module.exports.testFn = async (event, context, callback) => { 

    const zzz = {
        certificateId: 'xxxx8c0891f8xxxxxx'
    }
    try {
      const cert = await descCert(zzz)
      console.log("after descCert")
      console.log(cert)
      return cert
    } catch(e) {
       console.log(e)
    }



}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.