-
-
Save maverick1974/3c8bb79125d9703f5bfbdba8f803c8a2 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