【发布时间】:2016-11-05 01:16:04
【问题描述】:
我尝试使用以下脚本为具有标签名称[备份或备份]的实例创建快照> 根据https://serverlesscode.com/post/lambda-schedule-ebs-snapshot-backups/ && https://serverlesscode.com/post/lambda-schedule-ebs-snapshot-backups-2/ 如第一个和第二个链接中所述,我已经成功创建了带有 DeleteOn 标签的快照。第二个链接的后半部分解释了如何在指定日期删除这些快照。根据该代码,我有以下内容可以在 7 天后删除快照。
这是代码:
import boto3
import re
import datetime
ec = boto3.client('ec2')
iam = boto3.client('iam')
def lambda_handler(event, context):
account_ids = list('123456789011')
try:
iam.get_user()
except Exception as e:
account_ids.append(re.search(r'(arn:aws:sts::)([0-9]+)', str(e)).groups()[1])
delete_on = datetime.date.today().strftime('%Y-%m-%d')
filters = [
{'Name': 'tag-key', 'Values': ['DeleteOn']},
{'Name': 'tag-value', 'Values': [delete_on]},
]
snapshot_response = ec.describe_snapshots(OwnerIds=account_ids, Filters=filters)
for snap in snapshot_response['Snapshots']:
print "Deleting snapshot %s" % snap['SnapshotId']
ec.delete_snapshot(SnapshotId=snap['SnapshotId'])
通过这样做,我收到以下错误:
'NoneType' object has no attribute 'groups': AttributeError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 27, in lambda_handler
account_ids.append(re.search(r'(arn:aws:sts::)([0-9]+)', str(e)).groups()[1])
AttributeError: 'NoneType' object has no attribute 'groups'
【问题讨论】:
标签: amazon-web-services lambda