当尝试使用 Pub/Sub 并根据文档进行设置时,在使用我自己的 CDK 设置时,我似乎无法连接到 IoT Core。我立即进入断开连接状态,并且没有记录任何其他内容。我是 AWS 的新手,所以我可能缺少一些明显的东西,但如果没有任何可用的日志,我无法弄清楚。
这是一个
Vite
和 React
项目,我没有使用 Amplify CLI
但我正在使用 CDK 以编程方式设置我需要的一切。
这是我的整个设置和相关代码/配置
package.json
"dependencies": {
"@aws-amplify/ui-react": "4.6.0",
"amazon-cognito-identity-js": "6.2.0",
"aws-amplify": "5.1.2",
"aws-sdk": "2.1362.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"vite-plugin-ssr": "0.4.114",
},
"devDependencies": {
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@vitejs/plugin-react": "^3.1.0",
"dotenv-cli": "^7.1.0",
"typescript": "^4.9.3",
"vite": "^4.2.0"
}
这是我在应用程序入口点导入的连接代码
import { Amplify, Hub, Logger, PubSub } from 'aws-amplify';
import { env, isDev } from "@env"
import { AWSIoTProvider } from '@aws-amplify/pubsub';
Logger.LOG_LEVEL = isDev ? 'DEBUG' : 'INFO';
try {
const cancel = Hub.listen('pubsub', (data: any) => {
console.log('PubSub', data)
});
console.log("configuring amplify");
Amplify.configure({
Auth: {
region: env.AWS_REGION,
userPoolId: env.AWS_COGNITO_USER_POOL_ID,
userPoolWebClientId: env.AWS_COGNITO_USER_POOL_CLIENT_ID,
}
});
console.log("adding pubsub");
Amplify.addPluggable(
new AWSIoTProvider({
aws_pubsub_region: env.AWS_REGION,
aws_pubsub_endpoint: `wss://${env.AWS_IOT_ENDPOINT}/mqtt`,
})
);
console.log("finished amplify setup")
} catch (error) {
console.error("error occured during amplify setup", error);
}
在我的案例中,入口点是
_defalut.page.client.ts
for vite-ssr-plugin
import "@admin/lib/amplify"
这是我用来为
Policies
和 IoT
设置
Cognito User/ Identity pools
的 CDK
export class IoTConstruct extends Construct {
constructor(scope: Construct, id: string, props: IoTConstructProps) {
super(scope, id);
const iotPolicy = new Policy(this, `iot-amplify-policy`, {
statements: [
new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['iot:*'],
resources: [`arn:aws:iot:${appConfig.region}:${appConfig.account}:*`],
}),
],
});
const { authenticatedRole } = props;
authenticatedRole.attachInlinePolicy(iotPolicy);
authenticatedRole.addManagedPolicy(
iam.ManagedPolicy.fromAwsManagedPolicyName('AWSIoTDataAccess')
);
authenticatedRole.addManagedPolicy(
iam.ManagedPolicy.fromAwsManagedPolicyName('AWSIoTConfigAccess')
);
}
}
export class CognitoConstruct extends Construct {
public authenticatedRole: IRole;
constructor(scope: Construct, id: string) {
super(scope, id);
const userPool = new UserPool(this, `user-pool`, {
removalPolicy: isDev ? RemovalPolicy.DESTROY : RemovalPolicy.RETAIN,
selfSignUpEnabled: true,
autoVerify: { email: isDev },
signInAliases: { email: true },
passwordPolicy: GetPasswordPolicy(),
});
const adminUserPoolClient = new UserPoolClient(this, `admin-user-pool-client`, {
userPool: userPool,
authFlows: {
adminUserPassword: true,
custom: true,
userSrp: true,
},
supportedIdentityProviders: [
UserPoolClientIdentityProvider.COGNITO,
]
});
const identityPool = new IdentityPool(this, `identity-pool`, {
authenticationProviders: {
userPools: [new UserPoolAuthenticationProvider({ userPool, userPoolClient: adminUserPoolClient })],
},
});
this.authenticatedRole = identityPool.authenticatedRole;
...
结果
IAM Role
似乎是正确的
这是我目前得到的输出
我也在控制台中得到这个,但我怀疑它是否相关
[DEBUG] 57:08.309 Predictions - No plugin found with providerName=> AWSIoTProvider
我已经过身份验证,根据我对文档的理解,它应该会自动重新连接。
请询问更多信息,不确定我还能提供什么,我正在尝试发送和收听
testTopic
,但IoT
控制台没有收到任何信息,我的网站也没有收到任何信息