我正在尝试将AWS IoT配置为与AWS放大器一起使用。我总是看到错误的"AMQJS0008I套接字关闭了.“和CloudWatch说”,这是我配置的
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"cognito-identity:*",
"mobileanalytics:PutEvents",
"cognito-sync:*",
"iot:Connect",
"iot:Publish",
"iot:Subscribe",
"iot:Receive",
"iot:GetThingShadow",
"iot:UpdateThingShadow",
"iot:DeleteThingShadow",
"iot:AttachPolicy",
"iot:AttachPrincipalPolicy"
],
"Resource": "*"
}
]
}{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:Connect",
"Resource": "arn:aws:iot:ap-south-1:XXXXXXX:client/${iot:ClientId}"
},
{
"Effect": "Allow",
"Action": [
"iot:Publish",
"iot:Subscribe",
"iot:Receive"
],
"Resource": "arn:aws:iot:ap-south-1:XXXXXXX:topic/*"
},
{
"Effect": "Allow",
"Action": [
"iot:UpdateThingShadow",
"iot:GetThingShadow",
"iot:DeleteThingShadow"
],
"Resource": "arn:aws:iot:ap-south-1:XXXXXXX:thing/*"
},
{
"Effect": "Allow",
"Action": [
"iot:AttachPrincipalPolicy”,
"iot:AttachPolicy"
],
"Resource": [
"*"
]
}
]
}附加个人认知
aws iot attach-policy --policy-name "hub-iot-policy" --target "ap-south-1:XXXX-USER_COGNITO_IDENTITY使用
软件包的AWS扩增
“@aws-amplify/api": "^3.1.7",
"@aws-amplify/auth": "^3.2.4",
"@aws-amplify/core": "^3.2.4",
"@aws-amplify/pubsub": "^3.0.8”,代码是
PubSub.addPluggable(new AWSIoTProvider({
aws_pubsub_region: config.pubsub.REGION,
aws_pubsub_endpoint: `wss://${config.pubsub.MQTT_ID}.iot.${config.pubsub.REGION}.amazonaws.com/mqtt`,
}));
PubSub.subscribe('hub31-iot-thing').subscribe({
next: data => console.log('Message received', data),
error: error => console.error(error),
close: () => console.log('Done'),
});
}抛出错误
{provider: AWSIoTProvider,error:{…}}错误:{invocationContext:未定义,errorCode: 8,errorMessage:"AMQJS0008I Socket .“}提供程序: AWSIoTProvider {_config:{…},_clientsQueue: ClientsQueue,_topicObservers: Map(1),_clientIdObservers: Map(1)}
AUTHORIZATION_FAILURE
{
"timestamp": "2020-04-21 00:13:24.953",
"logLevel": "ERROR",
"traceId": “308de5a7-XXXX-d2d5-XXXX-7e24b6d6e0e6",
"accountId": “XXXXXXXX",
"status": "Failure",
"eventType": "Connect",
"protocol": "MQTT",
"clientId": “f5e1abef-XXXX-44af-XXXX-4a327b45481c",
"principalId": “XXXXX:CognitoIdentityCredentials",
"sourceIp": “XXXX",
"sourcePort": 59101,
"reason": "AUTHORIZATION_FAILURE",
"details": "Authorization Failure"
}发布于 2020-04-22 08:01:39
同样的错误,我就是这样解决的。
1.作为的认知政策
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"iot:Receive",
"cognito-identity:*",
"iot:Subscribe",
"iot:AttachPolicy",
"iot:AttachPrincipalPolicy",
"iot:Connect",
"mobileanalytics:PutEvents",
"iot:GetThingShadow",
"iot:DeleteThingShadow",
"iot:UpdateThingShadow",
"iot:Publish",
"cognito-sync:*"
],
"Resource": "*"
}
]
}还请注意,AttachPrincipalPolicy是不推荐的,但是为了更安全起见,我将它包括在内。
2. IoT策略作为
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:*",
"Resource": "*"
}
]
}3.通过lambda或AWS将IoT策略附加到单个认知标识。
aws iot attach-policy --policy-name "iot-policy" --target "ap-south-1:XXXX-USER-COGNITO-IDENTITY”再次注意,不建议使用AttachPrincipalPolicy,请使用AttachPolicy
使用lambda:
export const main = async (event, context, callback) => {
const principal = event.requestContext.identity.cognitoIdentityId;
const policyName = 'iot-policy';
const iot = new AWS.Iot();
await iot.attachPrincipalPolicy({ principal, policyName }).promise();
callback(null, "success");
};4.测试,如果您的前端配置正确,您应该能够解决errorCode: 8,errorMessage: AMQJS0008I套接字关闭错误。
5.微调现在微调物联网策略,根据您的要求,并立即检查更改是否有效
https://stackoverflow.com/questions/61335975
复制相似问题