Skip to content

Commit

Permalink
Update Lambda_function.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Hatimloha authored Sep 7, 2024
1 parent 0adabcf commit def0d30
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions Day-18/Lambda_function.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
# Through this function EC2 instance, EBS, Snapshot will be if not in use.
import boto3

def lambda_handler(event, context):
ec2 = boto3.client('ec2')

# Get all EBS snapshots
response = ec2.describe_snapshots(OwnerIds=['self'])
# Get all EBS snapshots
snapshots_response = ec2.describe_snapshots(OwnerIds=['self'])

# Get all active EC2 instance IDs
instances_response = ec2.describe_instances(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
active_instance_ids = set()

# Get all activce EC2 instance IDs
response = client.describe_instances(filter=[{'name': 'instance-state-name', 'value': ['running']}]
active_instances_ids = set()
for reservation in instances_response['Reservations']:
for instance in reservation['Instances']:
active_instance_ids.add(instance['InstanceId'])

for reservation in instances_response['Reservations']:
for instance in reservation['Instances']:
active_instance_ids.add(instance['InstanceId'])
# Iterate through each snapshot and delete if it's not attached to any volume or if the volume does not exist
for snapshot in snapshots_response['Snapshots']:
snapshot_id = snapshot['SnapshotId']
volume_id = snapshot.get('VolumeId')

# Iterate through each snapshot and delete if it's not attached to any volume or
for snapshot in response['Snapshots']:
snapshot_id = snapshot['SnapshotId']
volume_id = snapshot.get('volumeId')

if not volume_id:
# Delete the snapshot if it's not attached to any volume
ec2.delte_snapshot(SnapshotsId=Snapshot_id)
print(f"Delete EBS snapshot {snapshot_id} as it's not attached to any volume.")
else:
# check of the volume still exists
try:
volume_response = ec2.describe_volumes(VolumeIds=[volume_id])
if not volume_response['volume'][0]['Attachments'] :
ec2.delete_snapshot(SnapshotId=snapshot_id)
print(f"Delted EBS snapshot {snapshot_id} as it was taken from the a volume not attached")
except ec2.exceptions.ClientError as e:
if e.response['error']['code'] == 'InvalidVolume.NotFound':
# The colume associated with the snapshot is not found (it might have been deleted)
ec2.delete_snapshot(SnapshotID=snapshot_Id)
print(f'Delete EBS snapshot {snapshot_id} as its associated volume was not found.')
if not volume_id:
# Delete the snapshot if it's not attached to any volume
ec2.delete_snapshot(SnapshotId=snapshot_id)
print(f"Deleted EBS snapshot {snapshot_id} as it's not attached to any volume.")
else:
# Check if the volume still exists
try:
volume_response = ec2.describe_volumes(VolumeIds=[volume_id])
if not volume_response['Volumes'][0]['Attachments']:
ec2.delete_snapshot(SnapshotId=snapshot_id)
print(f"Deleted EBS snapshot {snapshot_id} as it was taken from a volume not attached.")
except ec2.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'InvalidVolume.NotFound':
# The volume associated with the snapshot is not found (it might have been deleted)
ec2.delete_snapshot(SnapshotId=snapshot_id)
print(f"Deleted EBS snapshot {snapshot_id} as its associated volume was not found.")

0 comments on commit def0d30

Please sign in to comment.