Skip to content

Instantly share code, notes, and snippets.

@eddmann
Created November 3, 2017 15:32
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save eddmann/ce2c65e4000d07c421ad266d449550ab to your computer and use it in GitHub Desktop.
Save eddmann/ce2c65e4000d07c421ad266d449550ab to your computer and use it in GitHub Desktop.
Scheduled EC2 Instance Type Modification using Lambda and CloudWatch Events
// Demonstration video can be found at: https://youtu.be/_gJyK1-NGq8
// ModifyEC2InstanceType
const AWS = require('aws-sdk');
exports.handler = (event, context, callback) => {
const { instanceId, instanceRegion, instanceType } = event;
const ec2 = new AWS.EC2({ region: instanceRegion });
Promise.resolve()
.then(() => ec2.stopInstances({ InstanceIds: [instanceId] }).promise())
.then(() => ec2.waitFor('instanceStopped', { InstanceIds: [instanceId] }).promise())
.then(() => ec2.modifyInstanceAttribute({InstanceId: instanceId, InstanceType: { Value: instanceType } }).promise())
.then(() => ec2.startInstances({ InstanceIds: [instanceId] }).promise())
.then(() => callback(null, `Successfully modified ${event.instanceId} to ${event.instanceType}`))
.catch(err => callback(err));
};
@devashishchandgupta
Copy link

Even after 4 years, still works like a charm. Thnx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment