Skip to content

Instantly share code, notes, and snippets.

@henkin
Created October 10, 2016 15:14
Show Gist options
  • Save henkin/850b3e49b64f7007f84dab7b628c6965 to your computer and use it in GitHub Desktop.
Save henkin/850b3e49b64f7007f84dab7b628c6965 to your computer and use it in GitHub Desktop.
Delete an AWS IoT thing and all its attached certificates.
'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