in sensor/delete-sensor.js [30:87]
async function deleteSensor(){
try {
var iot = new AWS.Iot();
//remove the iot core endpoint
settings.host = "";
//attach thing to certificate
await iot.detachThingPrincipal({thingName: settings.clientId, principal: settings.certificateArn}).promise();
//delete the thing
await iot.deleteThing({thingName: settings.clientId}).promise();
//detach policy from certificate
var policyName = 'Policy-' + settings.clientId;
await iot.detachPolicy({ policyName: policyName, target: settings.certificateArn}).promise();
//delete the IOT policy
result = await iot.deletePolicy({policyName: policyName}).promise()
//delete the certificates
var certificateId = settings.certificateArn.split('/')[1];
result = await iot.updateCertificate({certificateId:certificateId, newStatus:"INACTIVE"}).promise();
result = await iot.deleteCertificate({certificateId:certificateId, forceDelete:true}).promise();
settings.certificateArn = ""
//delete the certificate files
await fs.unlink(settings.keyPath);
settings.keyPath = "";
await fs.unlink(settings.certPath);
settings.certPath = "";
settings.caPath = "";
//save the updated settings file
settings.clientId = "";
var data = JSON.stringify(settings, null, 2);
await fs.writeFile(SETTINGS_FILE, data);
mobileSettings.sensorId = "";
data = JSON.stringify(mobileSettings, null, 2);
await fs.writeFile(MOBILE_SETTINGS_FILE, data);
//display results
console.log('IoT Things Removed');
console.log('AWS Region: ' + REGION);
console.log('AWS Profile: ' + PROFILE);
}
catch (err) {
console.log('Error deleting sensor');
console.log(err.message);
}
}