From 16f0624da8f4da5d9e3ed4bc935906114da01401 Mon Sep 17 00:00:00 2001 From: Anup Sarkar Date: Wed, 10 Jul 2019 19:50:33 +0530 Subject: [PATCH 1/2] Remove ec2 additional volume snapshot functionality --- ec2-create-snapshot/index.js | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/ec2-create-snapshot/index.js b/ec2-create-snapshot/index.js index 900d550..bb5cdec 100644 --- a/ec2-create-snapshot/index.js +++ b/ec2-create-snapshot/index.js @@ -162,26 +162,6 @@ async function checkRootVolume(ec2Reservations, volumeId) { } } -async function handleEc2CreateSnapshotForOthersVolume(ec2Reservations) { - try { - if (!isSnapshotInstance(ec2Reservations)) { - return; - } - let instance = ec2Reservations.Reservations[0].Instances[0]; - let rootDeviceName = instance.RootDeviceName; - for (let j = 0; j < instance.BlockDeviceMappings.length; j++) { // Create snapshot for other device volume - if (instance.BlockDeviceMappings[j].DeviceName === rootDeviceName) - continue; - let createdEc2Snapshot = await ec2CreateSnapshot(instance.BlockDeviceMappings[j].Ebs.VolumeId); - console.log('others volume snapshot created:', createdEc2Snapshot); - } - - } catch (err) { - console.log(JSON.stringify(err)); - throw err; - } -} - async function handleEc2CreateSnapshotForRootVolume(instanceId) { try { let ec2Reservations = await getEc2Instances(undefined, instanceId); @@ -220,6 +200,7 @@ function getVolumeId(volumeSource) { return volumeSource.split('/')[1]; } + exports.handler = async (event) => { console.log('Received event: ', JSON.stringify(event, null, 2)); try { @@ -231,16 +212,6 @@ exports.handler = async (event) => { else if (event['detail-type'] === 'EC2 Instance State-change Notification' && event.detail['instance-id'] && event.detail.state === 'stopped') { await handleEc2CreateSnapshotForRootVolume(event.detail['instance-id']); } - // When Root volume snapshot is completed, we will take other volumes snapshots and start the instance - else if (event['detail-type'] === 'EBS Snapshot Notification' && event.detail.event === 'createSnapshot' && event.detail.result === 'succeeded') { - let volumeId = getVolumeId(event.detail.source); - let ec2Reservations = await getEc2Instances(undefined, undefined, volumeId); - let isRootVolume = await checkRootVolume(ec2Reservations, volumeId); - if (isRootVolume) { - await handleEc2CreateSnapshotForOthersVolume(ec2Reservations); - await handleStartEc2Instance(ec2Reservations); - } - } // When Root volume snapshot is failed, we will send SNS message for manual investigation and start the instance else if (event['detail-type'] === 'EBS Snapshot Notification' && event.detail.event === 'createSnapshot' && event.detail.result === 'failed') { await handleSNSPublish(event); From c3edff7f9a24f964e9f81812463ec7fe21618377 Mon Sep 17 00:00:00 2001 From: Anup Sarkar Date: Wed, 10 Jul 2019 22:08:58 +0530 Subject: [PATCH 2/2] Add ec2 start instance functionality --- ec2-create-snapshot/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/ec2-create-snapshot/index.js b/ec2-create-snapshot/index.js index bb5cdec..1d8a646 100644 --- a/ec2-create-snapshot/index.js +++ b/ec2-create-snapshot/index.js @@ -211,6 +211,7 @@ exports.handler = async (event) => { // When Instance is stopped, we will take root volume snapshot first. else if (event['detail-type'] === 'EC2 Instance State-change Notification' && event.detail['instance-id'] && event.detail.state === 'stopped') { await handleEc2CreateSnapshotForRootVolume(event.detail['instance-id']); + await handleStartEc2Instance(ec2Reservations); } // When Root volume snapshot is failed, we will send SNS message for manual investigation and start the instance else if (event['detail-type'] === 'EBS Snapshot Notification' && event.detail.event === 'createSnapshot' && event.detail.result === 'failed') {