【问题标题】:Create IoT Policy with AWS Lambda使用 AWS Lambda 创建 IoT 策略
【发布时间】:2020-05-30 20:41:22
【问题描述】:

我正在尝试在 AWS Lambda 中创建 IoT 策略。我当前的 Lambda 函数如下所示:

"use strict";
const AWS = require("aws-sdk");
AWS.config.update({ region: "eu-central-1" });
var iot = new AWS.Iot();

exports.handler = async (event, context) => {


  var params = {
    policyDocument: `{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "iot:Connect"
      ],
      "Resource": [
        "arn:aws:iot:xxxxx:client/sander"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "iot:Subscribe"
      ],
      "Resource": [
        "arn:aws:iot:xxxx:topicfilter/$aws/things/ManuelBohrmaschine/shadow/*",
        "arn:aws:iot:xxxx:topicfilter/$aws/things/HeikoBohrmaschine/shadow/*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "iot:Publish",
        "iot:Receive"
      ],
      "Resource": [
        "arn:aws:iot:xxxx:topic/$aws/things/ManuelBohrmaschine/shadow/*",
        "arn:aws:iot:xxxx:topic/$aws/things/HeikoBohrmaschine/shadow/*"
      ]
    }
  ]
}`,
    policyName: 'sander1231564654654654',
  };
  
  
  try{

    iot.createPolicy(params, function (err, data) {
      if (err) console.log(err, err); // an error occurred
      else {
        console.log("test")
        console.log(data);
        return {
          headers: {
            "Access-Control-Allow-Origin": "*", // Required for CORS support to work
            "Access-Control-Allow-Credentials": true // Required for cookies, authorization headers with HTTPS 
          },
          statusCode: 200,
          body: JSON.stringify(data)
        };

      }         
    });
  }
  catch(e){
    console.log(e);
  }
};

lambda 函数只返回 null,甚至没有进入 iot.createPolicy() 的回调函数。我也尝试过没有尝试和捕捉。同样的问题。没有正确的错误。我正在使用这个文档:https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Iot.html#createPolicy-property

【问题讨论】:

    标签: javascript amazon-web-services aws-lambda aws-iot


    【解决方案1】:

    我认为原因是您的函数在有机会运行您的iot 部分之前返回。这是因为async handlers:

    如果您的代码执行异步任务,返回一个承诺以确保它完成运行。当您解决或拒绝承诺时,Lambda 会将响应或错误发送给调用者。

    要克服这个问题,您可以使用const promise = new Promise(...),如docs 所示。

    我修改了代码以使用Promise 模式(见下文)。我不能保证它完全有效,但你的函数现在应该能够执行iot.createPolicy 部分。

    "use strict";
    const AWS = require("aws-sdk");
    AWS.config.update({ region: "eu-central-1" });
    var iot = new AWS.Iot();
    
    exports.handler = async (event, context) => {
    
      var params = {
        policyDocument: `{
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "iot:Connect"
          ],
          "Resource": [
            "arn:aws:iot:xxxxx:client/sander"
          ]
        },
        {
          "Effect": "Allow",
          "Action": [
            "iot:Subscribe"
          ],
          "Resource": [
            "arn:aws:iot:xxxx:topicfilter/$aws/things/ManuelBohrmaschine/shadow/*",
            "arn:aws:iot:xxxx:topicfilter/$aws/things/HeikoBohrmaschine/shadow/*"
          ]
        },
        {
          "Effect": "Allow",
          "Action": [
            "iot:Publish",
            "iot:Receive"
          ],
          "Resource": [
            "arn:aws:iot:xxxx:topic/$aws/things/ManuelBohrmaschine/shadow/*",
            "arn:aws:iot:xxxx:topic/$aws/things/HeikoBohrmaschine/shadow/*"
          ]
        }
      ]
    }`,
        policyName: 'sander1231564654654654',
      };
    
      const promise = new Promise(function(resolve, reject) {
    
      try{
    
        console.log(params);
    
        iot.createPolicy(params, function (err, data) {
          if (err) {
              console.log(err, err); // an error occurred
              reject(Error(err));
          }
          else {
            console.log("test")
            console.log(data);
            resolve({
              headers: {
                "Access-Control-Allow-Origin": "*", // Required for CORS support to work
                "Access-Control-Allow-Credentials": true // Required for cookies, authorization headers with HTTPS 
              },
              statusCode: 200,
              body: JSON.stringify(data)
            });
    
          }         
        });
      }
      catch(e){
        console.log(e);
      }
    })
     return promise
    };
    

    【讨论】:

      猜你喜欢
      • 2018-04-22
      • 2023-03-30
      • 2018-02-27
      • 1970-01-01
      • 2018-04-13
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多