|from botocore.exceptions import ClientError|
|ec2 = boto3.client('ec2')|
|if not volume_id: return ''|
|if not instance_id: return ''|
|if not image_id: return ''|
|requestObj = ec2.describe_images(ImageIds=[image_id,])|
|if not requestObj["Images"]:|
|regex = r"^Created by CreateImage\((.*?)\) for (.*?) "|
|matches = re.finditer(regex, description, re.MULTILINE)|
|for matchNum, match in enumerate(matches):|
|return '', ''|
|with open('raport.csv', 'w') as csvfile:|
|writer = csv.writer(csvfile)|
|for snap in get_snapshots():|
|instance_id, image_id = parse_description(snap['Description'])|
|if __name__ == '__main__':|
Thank you for this script! It is great, however I would like to report a bug. It appears that sometimes the AWS API can return an empty array for a describe-images request (perhaps b/c an image was recently deleted), for instance:
aws ec2 describe-images --image-id ami-e1f3d98b
Since your python script is looking for an exception thrown, and otherwise it assumes the image exists, it will mark the "ami exists" column as true when it should actually be false since an empty list was returned for images matching that ami id. The image_exists method would need to be updated to check for an empty list.
I was able to get it working properly by modifying the image_exists method like so:
It seems it's possible that the other exists methods may also need to be updated.
Sorry, it looks like the indentation of the code is broken in the comment, but you get the idea =]
This is very useful, Thank You!
Currently the IDs alone are out of context when you deal with a massive old images cleanup backlog work and without the name it is very difficult to understand what you are viewing.
I was able to also add the names by these 3 functions:
I am new to scripting. I tried this script on our test environment and got the following error. I run this script on a W10 machine configuring AWS CLI.