Skip to content

Instantly share code, notes, and snippets.

@xiaket
Created October 31, 2017 04:41
Show Gist options
  • Save xiaket/a976e86797f03ddeb8f7dffc6df0b6de to your computer and use it in GitHub Desktop.
Save xiaket/a976e86797f03ddeb8f7dffc6df0b6de to your computer and use it in GitHub Desktop.
I frequently ran out of time when I have a packer debug session open and the saml role only grant me one hour of access. So instead of manually removing the keypair and the security group, I come up with this script.
#!/usr/bin/env python
# encoding=utf8
"""
This script will cleanup leftover resources(instance, key-pair and security group)
related to an instance specified by its ipaddr or instance-id.
I frequently ran out of time when I have a packer debug session open and
the saml role only grant me one hour of access. So instead of manually
removing the keypair and the security group, I come up with this script.
"""
from __future__ import print_function
import sys
import time
import boto3
def get_instance(spec):
"""Get packer build instance.
The instance can be specified by its id, private_ip_address or
public_ip_address.
"""
boto3.setup_default_session(region_name='ap-southeast-2')
ec2 = boto3.resource('ec2')
instances = ec2.instances.filter(
Filters=[{
'Name': 'instance-state-name', 'Values': [
'running', 'stopped', 'terminated'
],
}]
)
for inst in instances:
print(inst.id, inst.private_ip_address)
candidates = [
inst.id, inst.private_ip_address, inst.public_ip_address,
]
if spec in candidates:
return inst
else:
raise RuntimeError("Instance not found!")
def cleanup(instance):
"""Cleanup resources used by an instance.
This will verify that this is the right instance, then it will do the
following cleanups:
* remove the keypair created by packer
* terminate the instance
* remove the security group attached to the instance.
"""
# Verify we've got the right instance.
assert instance.key_pair.name.startswith('packer_')
# Print out some info about the instance
print("About to terminate this instance /w its resources:")
print("Instance ID: {}".format(instance.id))
print("Internal IPaddr: {}".format(instance.private_ip_address))
print("SecurityGroup: {}".format(instance.security_groups))
print("Key Pair: {}".format(instance.key_pair.key_name))
# remove the keypair first
instance.key_pair.delete()
# terminate the instance so we can delete the security group.
print("Terminating the instance.", end="")
while True:
response = instance.terminate()
status = response['TerminatingInstances'][0]['CurrentState']['Name']
if status == 'terminated':
print("\nInstance terminated.")
break
time.sleep(2)
print(".", end='')
# and finally, remove the security group.
# FIXME: could add a try-except block here.
if instance.security_groups:
instance.security_groups[0].delete()
print("Resources cleaned.")
def main():
instance = get_instance(sys.argv[1])
cleanup(instance)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment