背景:我对 AWS 管理控制台非常陌生,我刚刚创建了一个非常简单的 AWS Lambda 函数(Python 3.7),它根据时间限制删除 EBS 卷快照。我还创建了一个 CloudWatch 事件以在一天中的每个小时触发该函数。这适用于一些卷,但随着卷的增加,这会导致成本/速度问题。
问题:我正在尝试更新我的 EBS 快照删除 lambda 函数代码以找到请求的速率限制以避免节流,并使用它为程序创建指数退避/重试模型(使程序无论有多少快照都可扩展)是)。我假设有一个特殊的 API 调用可以帮助我解决这个问题,但我无法在网上找到任何具体信息。任何帮助将非常感激!在下面粘贴我当前的代码:
import boto3
from datetime import datetime, timezone, timedelta
ec2=boto3.resource('ec2') #resource, higher level
snapshots = ec2.snapshots.filter(OwnerIds=['self']) #all snapshots owned by me, returns list
def lambda_handler(event, context):
# TODO implement
for i in snapshots:#for each snapshot
start_time=i.start_time #timestamp when snapshot was initiated
delete_time=datetime.now(tz=timezone.utc)-timedelta(days=1) #Correct time in UTC timezone - 1 days]
if delete_time>start_time:#if delete time is more than start time (more than a day)
i.delete() #call method to delete that snapshot
print ('Snapshot with Id = {snaps} is deleted'.format(snaps=i.snapshot_id)) #pull ID that was deleted