【问题标题】:How can I access AWS iot.describeCertificate() inside lambda with NodeJS?如何使用 NodeJS 在 lambda 中访问 AWS iot.describeCertificate()?
【发布时间】:2020-08-15 07:47:08
【问题描述】:

我需要使用带有 NodeJS 的 lambda 函数来检索 IoT 证书状态。 在documentation 之后,我应该使用describeCertificate() 来完成这项任务。

这是我目前的代码(用于睾丸):

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')
      console.log(err, err.stack)
    }else{
      console.log('describeCertificate - data')
      cert = data
      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)

...
}

我的猜测是我没有访问函数await iot.describeCertificate( ...,因为我在CloudWatch 中看不到日志。

我应该收到这个序列:

  1. 开始 descCert
  2. 参数
  3. {certificateId: 'xxxx8c0891f8xxxxxx'}
  4. describeCertificate - Fn
  5. 或 describeCertificate - 错误或 describeCertificate - 数据
  6. 实际数据响应
  7. 结束 describeCertificate - Fn
  8. 结束 descCert
  9. descCert 之后
  10. 实际数据响应

但这就是我得到的:

  1. 开始 descCert
  2. 参数
  3. {certificateId: 'xxxx8c0891f8xxxxxx'}
  4. (8) 结束 descCert
  5. (9) 在 descCert 之后
  6. (10) os dados mesmo //{}

我在日志信息中看不到步骤 4-7。所以结论是函数没有被调用。

我错过了什么?

【问题讨论】:

    标签: node.js async-await certificate aws-iot


    【解决方案1】:

    要访问 IoT 核心服务(或任何其他服务),您应该为 Lambda 函数提供对该服务的相应 RIGHT 访问权限。为此,您可以转到 IAM -> 角色并将相应的策略添加到附加到您的 Lambda 函数的角色中。

    您可以将内联策略添加到相应的 lambda-role:

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "VisualEditor0",
                "Effect": "Allow",
                "Action": "iot:DescribeCertificate",
                "Resource": "arn:aws:iot:*:*:cert/*"
            }
        ]
    }
    

    最好把Resource改成你对应的arn

    【讨论】:

      【解决方案2】:
      var AWS = require('aws-sdk');
      
      async function get_certificate_status(iot, params) {
          try {
              const data = await iot.describeCertificate(params).promise();
              return data.certificateDescription.status;
          } catch(e) {
              throw new Error(e.message);  
          }
      }
      
      exports.handler = async function(event, context, callback) {
          var iot = new AWS.Iot({'region': <region>, apiVersion: '2015-05-28'});        
          var params = { certificateId: <certificateId> };
          
          var cert_status = await get_certificate_status(iot, params);
          console.log("STATUS: " + cert_status);
      }
      

      【讨论】:

        猜你喜欢
        • 2020-08-06
        • 2019-12-12
        • 2018-09-22
        • 2019-09-13
        • 1970-01-01
        • 1970-01-01
        • 2016-02-14
        • 2021-11-24
        • 2016-04-11
        相关资源
        最近更新 更多