Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ahmed-abdelazim/0744c76047bdd08b01c2eaba7eb2cb66 to your computer and use it in GitHub Desktop.
Save ahmed-abdelazim/0744c76047bdd08b01c2eaba7eb2cb66 to your computer and use it in GitHub Desktop.
Scheduled Start/Stop of EC2 Instances using Lambda and CloudWatch Events
// 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