Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ec2 additional volume snapshot functionality #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 2 additions & 30 deletions ec2-create-snapshot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -230,16 +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']);
}
// 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);
}
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') {
Expand Down