Skip to content

Instantly share code, notes, and snippets.

@eercanayar
Last active October 8, 2020 12:44
Show Gist options
  • Save eercanayar/da4015dfabf0ce9594313c6b16fcbaa4 to your computer and use it in GitHub Desktop.
Save eercanayar/da4015dfabf0ce9594313c6b16fcbaa4 to your computer and use it in GitHub Desktop.
this tiny script cleans up your AWS IoT Core environment by deleting unused certificates which aren't connected to anything, I mean any "Thing" 🤪
import boto3
boto3.setup_default_session(profile_name='awsiot')
client = boto3.client('iot')
list_certificates_paginator = client.get_paginator('list_certificates')
list_certificates_pages = list_certificates_paginator.paginate()
for list_certificates in list_certificates_pages:
for certificate in list_certificates["certificates"]:
principal = client.list_principal_things(principal=certificate["certificateArn"])
print("%s arn things: %d" % (certificate["certificateId"], len(principal["things"])))
if len(principal["things"])==0:
print("> disabling certificate")
client.update_certificate(certificateId=certificate["certificateId"], newStatus='INACTIVE')
print("> disabled, deleting certificate")
client.delete_certificate(certificateId=certificate["certificateId"], forceDelete=True)
print("> deleted")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment