#!/usr/bin/env python3
import boto3
import time
from botocore.exceptions import ClientError
#take snapshots of long time once a week as delete "cron(16 23 ? * 1 *)"
def delete_snapshot(snapshot_id):
try:
ec2resource = boto3.resource('ec2')
snapshot = ec2resource.Snapshot(snapshot_id)
snapshot.delete()
except ClientError as e:
print
"Caught exception: %s" % e
return
def lambda_handler(event, context):
ec2 = boto3.client('ec2', region_name='ap-southeast-1')
# Get all snapshots
result = ec2.describe_snapshots(OwnerIds=['***************'])
for snapshot in result['Snapshots']:
if 'Tags' in snapshot:
for tags in snapshot['Tags']:
if tags["Key"] == "Date":
#Get the current timestamp
now = int(time.time())
aws pathon脚本定期删除snapshots
最新推荐文章于 2024-04-03 09:00:00 发布