Get all All AWS Ec2 snapshot reports with python and boto3

AWS EC2 Start Stop with boto3

You can translate the content of this page by selecting a language in the select box.

How to Get all All AWS Ec2 snapshot reports with python and boto3?

Use this python script to get all EC2 snapshot report in your AWS account.

AWS EC2 snapshop report


import boto3
def lambda_handler(event, context):
session = boto3.Session(profile_name='saml')
ec2client = session.client('ec2')
instances_with_volumes = get_instance_ids(ec2client, "tc:OpsAutomatorTaskList")

for instance_id, volume_ids in instances_with_volumes.items():
response = ec2client.describe_snapshots(
Filters = [
{'Name':'volume-id', 'Values': volume_ids}
#{'Name':'start-time', 'Values': ['2019-01-01']}
]
#MaxResults = 10
)
#print(response['ResponseMetadata'])
#print("instance_id:%s, number_of_snapshots:%s" % (instance_id, len(response['Snapshots'])))
if len(response['Snapshots']) > 0:
#print(response['Tags'])
#Print Header
print('InstanceId' + ' , ' + 'Description' + ' , ' + 'Encrypted' + ' , ' + 'OwnerId' + ' , ' + 'Progress' + ' , ' + 'SnapshotId' + ' , ' + 'StartTime' + ' , ' + 'State' + ' , ' + 'VolumeId' + ' , ' + 'VolumeSize')
for snapshot in response['Snapshots']:
print(instance_id + ' , ' + snapshot['Description'] + ' , ' + str(snapshot['Encrypted']) + ' , ' + str(snapshot['OwnerId']) + ' , ' + snapshot['Progress'] + ' , ' + snapshot['SnapshotId'] + ' , ' + str(snapshot['StartTime']) + ' , ' + snapshot['State'] + ' , ' + snapshot['VolumeId'] + ' , ' + str(snapshot['VolumeSize']) )

def get_instance_ids(ec2client, tag_name):
# When passed a tag key, tag value this will return a list of InstanceIds that were found.


AI Unraveled: Demystifying Frequently Asked Questions on Artificial Intelligence

response = ec2client.describe_instances(
Filters=[
{
‘Name’: ‘tag-key’,
‘Values’: [tag_name]
}
]
)
instancelist = {}
for reservation in (response[“Reservations”]):
for instance in reservation[“Instances”]:

ebs_volume_ids = []
for ebs in instance[‘BlockDeviceMappings’]:
ebs_volume_ids.append(ebs[‘Ebs’][‘VolumeId’])

instancelist[instance[“InstanceId”]] = ebs_volume_ids

If you are looking for an all-in-one solution to help you prepare for the AWS Cloud Practitioner Certification Exam, look no further than this AWS Cloud Practitioner CCP CLFC01 book below.


Achieve AWS Solutions Architect Associate Certification with Confidence: Master SAA Exam with the Latest Practice Tests and Quizzes illustrated

return instancelist

"Become a Canada Expert: Ace the Citizenship Test and Impress Everyone with Your Knowledge of Canadian History, Geography, Government, Culture, People, Languages, Travel, Wildlife, Hockey, Tourism, Sceneries, Arts, and Data Visualization. Get the Top 1000 Canada Quiz Now!"


lambda_handler(”,”)


error: Content is protected !!