You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在AWS Lambda中创建IoT Policy遇返回Null且回调未执行问题求助

解决AWS Lambda创建IoT Policy时返回Null且回调不执行的问题

嘿,我一眼就揪出问题根源了——你的Lambda handler标记了async,但调用iot.createPolicy时却用了传统的回调模式!Lambda的async handler会等待Promise完成才返回,但回调式操作不会返回Promise,所以函数在回调触发前就直接结束了,自然返回null,回调里的代码根本没机会跑起来。

修复方案:改用AWS SDK的Promise接口(async/await)

AWS SDK v2支持把所有回调式方法转换成Promise,只要在方法后面加个.promise(),再用await等着执行结果就行。这样Lambda会乖乖等IoT Policy创建完成,再返回响应,回调逻辑也能正常执行。

修正后的代码如下:

"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 {
        // 改用promise+await,让Lambda等待操作完成
        const data = await iot.createPolicy(params).promise();
        console.log("Policy created successfully:", data);
        return { 
            headers: { 
                "Access-Control-Allow-Origin": "*", 
                "Access-Control-Allow-Credentials": true 
            }, 
            statusCode: 200, 
            body: JSON.stringify(data) 
        };
    } catch(e) {
        // 完善错误处理,方便调试
        console.error("Failed to create policy:", e);
        return {
            headers: { 
                "Access-Control-Allow-Origin": "*", 
                "Access-Control-Allow-Credentials": true 
            },
            statusCode: 500,
            body: JSON.stringify({ error: e.message, details: e.stack })
        };
    }
};

关键改动说明:

  • 替换回调为async/await:把原来的回调调用改成await iot.createPolicy(params).promise(),确保Lambda等待Policy创建完成后再继续执行,不会提前退出。
  • 错误处理升级:原来的catch只打印错误,现在返回500状态码和详细错误信息,不管是前端调试还是CloudWatch排查都更方便。
  • 确保返回值被捕获:原来回调里的return根本不会被async handler识别,现在直接在try块返回响应,Lambda能拿到正确的返回结果。

另外,顺便提醒你检查下Lambda的执行角色有没有iot:CreatePolicy权限——如果权限不够,改用Promise后会在catch块里明确抛出权限错误,比之前没反应更容易排查。

内容的提问来源于stack exchange,提问作者Sander Buchheim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 19:52:45