Created
October 10, 2016 15:14
-
-
Save henkin/850b3e49b64f7007f84dab7b628c6965 to your computer and use it in GitHub Desktop.
Delete an AWS IoT thing and all its attached certificates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var AWS = require('aws-sdk'); | |
AWS.config.region = 'us-east-1'; | |
var iot = new AWS.Iot(); | |
function deleteThing(name, policyName) { | |
console.log('Deleting thing "' + name + '"'); | |
let params = {thingName: name}; | |
iot.listThingPrincipals(params).promise() | |
.then((data) => { | |
//console.log(data.principals); | |
var principals = data.principals; | |
return Promise.all(principals.map(p => { | |
console.log("removing principal " + p); | |
let certId = p.substr(p.indexOf("/") + 1); | |
console.log("certName: " + certId); | |
return iot.detachThingPrincipal({thingName: name, principal: p}).promise() | |
.then(() => iot.detachPrincipalPolicy({policyName, principal: p}).promise()) | |
.then(() => iot.updateCertificate({certificateId: certId, newStatus: "INACTIVE"}).promise()) | |
.then(() => iot.deleteCertificate({certificateId: certId}).promise()); | |
})) | |
.then(() => console.log("removed certs")); | |
}) | |
.then(() => iot.deleteThing(params).promise()) | |
.then(() => console.log("deleted thing")) | |
.catch((err) => console.error(err)); | |
} | |
// deleteThing('boom', 'WedgePolicy'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment