我想为我在AWS上的实例创建快照失败通知。我在要使用生命周期管理器策略创建快照的实例上放置了标签。我想使用创建快照事件,因为它有两个结果:快照失败或通过。我想用它来了解快照是否失败,然后通过SNS向我们发送通知。
我想使用这种逻辑:
对于 带有标签的目标实例= Snapshot-daily:true
检查其中的快照创建事件,如果发生故障,请使用SNS通知我们
const AWS = require('aws-sdk');
const EC2 = new AWS.EC2();
const SNS = new AWS.SNS();
function SNSPublish(subject, message, topicARN) {
let params = {
Message: message
};
if (subject)
params.Subject = subject;
if (topicARN)
params.TopicArn = topicARN;
return SNS.publish(params).promise();
}
// Check tag key is there or not
function checkTag(tags, tagkey) {
let hasTag = false;
for (let i = 0; i < tags.length; i++) {
if (tags[i].Key === tagkey) {
hasTag = true;
break;
}
}
return hasTag;
}
function isSnapshotInstance(ec2Reservations) {
return checkTag(ec2Reservations.Reservations[0].Instances[0].Tags, 'snapshot', 'Snapshot-daily');
}
async function handleSNSPublish(event) {
try {
let subject = 'ec2 create snapshot task failure';
let message = 'volume: ' + event.detail.source + '<br>snapshot id: ' + event.detail['snapshot_id'];
let publishedSNS = await SNSPublish(subject, message, 'SNS_TOPIC_ARN');
console.log('publishedSNS', JSON.stringify(publishedSNS));
} catch (err) {
console.log(JSON.stringify(err));
throw err;
}
};
``````````