首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用创建IoT策略

用创建IoT策略
EN

Stack Overflow用户
提问于 2020-05-30 11:38:05
回答 1查看 231关注 0票数 1

我正试图在中创建一个IoT策略。我当前的Lambda函数看起来是这样的:

代码语言:javascript
复制
"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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-30 12:23:45

我认为原因是函数在有机会运行iot部分之前返回。这是异步处理程序的信标

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

要克服这一问题,可以使用const promise = new Promise(...),如文档中所示。

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

代码语言:javascript
复制
"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
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62101866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档