我的目标是在AWS上使用无服务器框架在多个物联网设备上创建一个系统,以将JSON有效负载发送到AWS IoT,然后将其保存到DynamoDB。
我是使用AWS的新手……
的 编辑05/09/2018: 强> 我发现这篇博文,很好地描述了我的方法: 使用证书自动售货机参考应用程序确保与AWS IoT核心的安全通信
的 - 强>
你可以看看 即时配置 或者建立自己的解决方案 程序化供应 。
我已多次处理这个主题,并且必须意识到它在很大程度上取决于用例,这更有意义。安全性也是一个值得关注的方面。您不希望有一个公共API负责整个Internet可以访问的JIT设备注册。
一个简单的基于Programmatic Provisioning的场景可能如下所示:您构建一个东西(可能是传感器),它应该能够连接到AWS IoT并具有内部供应流程。
简单的配置过程:
在服务器上运行的注册代码看起来像这样(JS + AWS JS SDK):
// Modules const AWS = require('aws-sdk') // AWS const iot = new AWS.Iot({ region: process.env.region }) // Config const templateBodyJson = require('./register-thing-template-body.json') // registerThing const registerThing = async ({ serialNumber = null } = {}) => { if (!serialNumber) throw new Error('`serialNumber` required!') const { certificateArn = null, certificateId = null, certificatePem = null, keyPair: { PrivateKey: privateKey = null, PublicKey: publicKey = null } = {} } = await iot.createKeysAndCertificate({ setAsActive: true }).promise() const registerThingParams = { templateBody: JSON.stringify(templateBodyJson), parameters: { ThingName: serialNumber, SerialNumber: serialNumber, CertificateId: certificateId } } const { resourceArns = null } = await iot.registerThing(registerThingParams).promise() return { certificateArn, certificateId, certificatePem, privateKey, publicKey, resourceArns } } const unregisterThing = async ({ serialNumber = null } = {}) => { if (!serialNumber) throw new Error('`serialNumber` required!') try { const thingName = serialNumber const { principals: thingPrincipals } = await iot.listThingPrincipals({ thingName }).promise() const certificates = thingPrincipals.map((tp) => ({ certificateId: tp.split('/').pop(), certificateArn: tp })) for (const { certificateId, certificateArn } of certificates) { await iot.detachThingPrincipal({ thingName, principal: certificateArn }).promise() await iot.updateCertificate({ certificateId, newStatus: 'INACTIVE' }).promise() await iot.deleteCertificate({ certificateId, forceDelete: true }).promise() } await iot.deleteThing({ thingName }).promise() return { deleted: true, thingPrincipals } } catch (err) { // Already deleted! if (err.code && err.code === 'ResourceNotFoundException') { return { deleted: true, thingPrincipals: [] } } throw err } }
的 注册 - 事物 - 模板body.json: 强>
{ "Parameters": { "ThingName": { "Type": "String" }, "SerialNumber": { "Type": "String" }, "CertificateId": { "Type": "String" } }, "Resources": { "thing": { "Type": "AWS::IoT::Thing", "Properties": { "ThingName": { "Ref": "ThingName" }, "AttributePayload": { "serialNumber": { "Ref": "SerialNumber" } }, "ThingTypeName": "NewDevice", "ThingGroups": ["NewDevices"] } }, "certificate": { "Type": "AWS::IoT::Certificate", "Properties": { "CertificateId": { "Ref": "CertificateId" } } }, "policy": { "Type": "AWS::IoT::Policy", "Properties": { "PolicyName": "DefaultNewDevicePolicy" } } } }
确保您拥有所有“NewDevice”Thing类型,组和策略。还要记住ThingName = SerialNumber(对于unregisterThing很重要)。