-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
30 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") |