-
-
Save lfalmeida/e102d9d477c49d32e3dc88e0a48e5f24 to your computer and use it in GitHub Desktop.
Scheduled EC2 Instance Type Modification using Lambda and CloudWatch Events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment