因此,AWS 上的文档非常模糊。我遇到过同样的问题。这是我修复它的方法。假设您使用 AWS IoT 注册了 CA,即使您为上传的 CA 启用自动注册,AWS IoT 也不会允许设备连接。证书将有几个用于 JiT(及时)注册的选项。
- 创建Lamba以在一定条件下激活设备;
- 列出 MQTT 上的事件以激活证书;
- 注册公钥。
AWS Docs https://docs.aws.amazon.com/iot/latest/developerguide/auto-register-device-cert.html中描述了选项 1 和 2
执行选项 3 的步骤:
- 注册事物
software.amazon.awssdk.services.iot.IotClient iotClient = IotClient.create()
//This allows AWS Credentials to be picked up using DefaultAWSCredentialsProviderChain
CreateThingRequest thingToBeCreated =
CreateThingRequest.builder().thingName("Unique Id of Device").build();
iotClient.createThing(thingToBeCreated);
- 注册并激活设备的公钥。
RegisterCertificateRequest registerCertificateRequest = RegisterCertificateRequest.builder()
.caCertificatePem("CA Pem as String")
.certificatePem("Device Public Key in Pem as String")
.setAsActive(true)
.build();
final RegisterCertificateResponse registerCertificateResponse = iotClient.registerCertificate(registerCertificateRequest);
- 将证书附加到事物上。
AttachThingPrincipalRequest attachThingPrincipalRequest = AttachThingPrincipalRequest.builder()
.thingName("Unique Id of Device")
.principal(registerCertificateResponse.certificateArn())
.build();
iotClient.attachThingPrincipal(attachThingPrincipalRequest);
- 可选,将策略附加到事物以便它可以连接。
AttachPolicyRequest attachPolicyRequest = AttachPolicyRequest.builder()
.policyName("policy_that_allow_device_connections")
.target(registerCertificateResponse.certificateArn())
.build();
iotClient.attachPolicy(attachPolicyRequest);