Python / Boto3的新手,这应该是一个简单但仍在学习的内容:)
我有一个Lambda函数可以创建许多快照并且工作正常:
def create_snapshot():
volumes = ec2_client.describe_volumes(
Filters=[
{'N'...
...
for volume in volumes...
....
snap_name = 'Backup of ' + snap_desc
....
snap = ec2_client.create_snapshot(
VolumeId=vol_id,
Description=snap_desc
)
然后,我想收到来自AWS SNS的电子邮件,让我知道该功能创建的快照,我使用的是:
message = sns.publish(
TopicArn=SNSARN,
Subject=("Function executed"),
Message=("%s created" % snap_name)
)
问题在于,这会为每个快照创建一封电子邮件,而不是列出所有快照的电子邮件。我应该创建另一个调用snap_desc生成的值的函数,还是可以在函数中发送snap_desc的所有值?最重要的是,最好的方法是什么?
干杯!
斯科特
####################### UPDATE (Thanks @omuthu) #######################
我在循环内外设置了一个数组,并将字符串放入消息中。这产生了以下消息:
已创建以下快照: ['vol-0e0b9a5dfb8379fc0(Instance 1 - / dev / sda1)','vol-03aac6b65df64661e(Instance 4 - / dev / sda1)','vol-0fdde765dfg452631(Instance 2 - / dev / sda1)','vol-0693a9568b11f625f (实例3 - / dev / sda1)'等等。
答案 0 :(得分:0)
好吧,它终于排好了!
def create_snapshot():
volumes = ec2_client.describe_volumes(
Filters=[
{'N'...
...
inst_list = []
for volume in volumes...
vol_id = volume['VolumeId']
....
snap_desc = vol_id
for name in volume['Tags']:
tag_key = name['Key']
tag_val = name['Value']
if tag_key == 'Name':
snap_desc = vol_id + ' (' + tag_val + ')'
....
....
....
if backup_mod is False or (current_hour + 10) % backup_mod != 0:
...
continue
else:
print("%s is scheduled this hour" % vol_id)
for name in volume['Tags']:
inst_tag_key = name['Key']
inst_tag_val = name['Value']
if inst_tag_key == 'Name':
inst_list.append(inst_tag_val)
snap = ec2_client.create_snapshot(
VolumeId=vol_id,
Description=snap_desc,
)
print("%s created" % snap['SnapshotId'])
msg = str("\n".join(inst_list))
if len(inst_list) != 0:
message = sns.publish(
TopicArn=SNSARN,
Subject=("Daily Lambda snapshot function complete"),
Message=("The following snapshots have been created today:\n\n" + msg + "\n")
)
print("Response: {}".format(message))