-
-
Save eddmann/a9e404eb62056f77610f752606a2e504 to your computer and use it in GitHub Desktop.
// 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)); | |
}; |
thank You! nice tutorial by the way
help:
1)EC2 instances - i have mutiple servers and i would like to start/stop using above code...Can anyone help me how to use the code
2) can i merge rds & ec2 stop/start code in one funtion ?
Watch this Video if u want to use it https://www.youtube.com/watch?v=roAerKVfq-Y
hello, I have a question, it is possible to start an instance as demand increases??
I have a service in nodejs in a t2micro instance, but I want that when the cpu exceeds a% immediately start another EC2
@eddmann thanks a lot, helped me automate my clients' services.
@ChenLi0830 thanks to update script to node 12+
Congrats !
help:
1)EC2 instances - i have mutiple servers and i would like to start/stop using above code...Can anyone help me how to use the code
2) can i merge rds & ec2 stop/start code in one funtion ?
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.
Node 12+ version:
Stop Instance
Start Instance