Created
November 3, 2017 14:00
-
-
Save eddmann/a9e404eb62056f77610f752606a2e504 to your computer and use it in GitHub Desktop.
Scheduled Start/Stop of EC2 Instances 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/roAerKVfq-Y | |
// StopEC2Instance | |
const AWS = require('aws-sdk'); | |
exports.handler = (event, context, callback) => { | |
const ec2 = new AWS.EC2({ region: event.instanceRegion }); | |
ec2.stopInstances({ InstanceIds: [event.instanceId] }).promise() | |
.then(() => callback(null, `Successfully stopped ${event.instanceId}`)) | |
.catch(err => callback(err)); | |
}; | |
// StartEC2Instance | |
const AWS = require('aws-sdk'); | |
exports.handler = (event, context, callback) => { | |
const ec2 = new AWS.EC2({ region: event.instanceRegion }); | |
ec2.startInstances({ InstanceIds: [event.instanceId] }).promise() | |
.then(() => callback(null, `Successfully started ${event.instanceId}`)) | |
.catch(err => callback(err)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yes, with lambda you have the power of code in your hands, so you can do all things that you want. You could add commands to start another EC2 instances for example. Just you need is aws sdk to execute like as CLI commands.