首先,我尝试使用AWS Amplify库来使用IOT模块为我的无服务器应用程序创建AWS IOT Chat(我按照文档中给出的步骤进行操作),但是它不起作用,并显示“套接字已关闭”错误。
然后,我尝试了aws-iot-device-sdk,并按照AWS文档和代码段进行了操作,但是存在相同的错误。
我已按照以下步骤操作:
答案 0 :(得分:0)
我不确定您的问题可能在哪里,但是当我第一次开始使用Amplify PubSub时,我也遇到了同样的问题。对我来说,这是一个政策问题。因此,其中之一可能会有所帮助:
将您的事物策略文档连接到联合池的用户标识(而不是用户池中的标识)。我的保单文件如下:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect",
"iot:AttachPrincipalPolicy",
"iot:Publish",
"iot:Subscribe",
"iot:Receive",
"iot:GetThingShadow",
"iot:UpdateThingShadow",
"iot:DeleteThingShadow"
],
"Resource": [
"*"
]
}
]
}
要为所有我的用户(不是很多)附加/更新策略,我使用Lambda函数来执行此操作:
var AWS = require("aws-sdk");
const cognito = new AWS.CognitoIdentityServiceProvider({apiVersion: '2016-04-18'});
var cognitoidentity = new AWS.CognitoIdentity();
var iot = new AWS.Iot({apiVersion: '2015-05-28'});
exports.handler = (event, context, callback) => {
var params = {
IdentityPoolId: 'eu-central-1:xxxxxxxx-xxxx-xxxx-xxx-xxxxxxxx', /* change this */
MaxResults: 50,
};
cognitoidentity.listIdentities(params, function(err, data) {
if (err) console.log(err, err.stack);
else {
addPolicies(data.Identities);
}
});
function addPolicies(users) {
for (let i = 0; i<users.length;i++) {
var params2 = {
policyName: 'myIOTPolicy',
principal: users[i].IdentityId
};
iot.attachPrincipalPolicy(params2, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
});
}
}
callback(null, event);
};
对于我在身份池中经过身份验证的角色,我对该角色附加了以下策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:AttachPrincipalPolicy",
"iot:Connect",
"iot:Publish",
"iot:Subscribe",
"iot:Receive",
"iot:GetThingShadow",
"iot:UpdateThingShadow",
"iot:DeleteThingShadow"
],
"Resource": [
"*"
]
}
]
}
希望这会有所帮助