Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@smiller171
Created February 16, 2016 18:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save smiller171/86090f4415ddd54f5024 to your computer and use it in GitHub Desktop.
Save smiller171/86090f4415ddd54f5024 to your computer and use it in GitHub Desktop.
Nightly EC2 shutdown
import boto3
import logging
#setup simple logging for INFO
logger = logging.getLogger()
logger.setLevel(logging.INFO)
#define the connection
ec2 = boto3.resource('ec2')
def lambda_handler(event, context):
# Use the filter() method of the instances collection to retrieve
# all running EC2 instances.
filters = [{
'Name': 'tag:PowerSave',
'Values': ['nightly','Nightly','true','True']
},
{
'Name': 'instance-state-name',
'Values': ['running']
}
]
#filter the instances
instances = ec2.instances.filter(Filters=filters)
#locate all running instances
RunningInstances = [instance.id for instance in instances]
#print the instances for logging purposes
#print RunningInstances
#make sure there are actually instances to shut down.
if len(RunningInstances) > 0:
#perform the shutdown
shuttingDown = ec2.instances.filter(InstanceIds=RunningInstances).stop()
print shuttingDown
else:
print "Nothing to see here"
import boto3
import logging
#setup simple logging for INFO
logger = logging.getLogger()
logger.setLevel(logging.INFO)
#define the connection
ec2 = boto3.resource('ec2')
def lambda_handler(event, context):
# Use the filter() method of the instances collection to retrieve
# all running EC2 instances.
filters = [{
'Name': 'tag:PowerSave',
'Values': ['nightly','Nightly','true','True']
},
{
'Name': 'instance-state-name',
'Values': ['stopped']
}
]
#filter the instances
instances = ec2.instances.filter(Filters=filters)
#locate all stopped instances
StoppedInstances = [instance.id for instance in instances]
#print the instances for logging purposes
#print StoppedInstances
#make sure there are actually instances to shut down.
if len(StoppedInstances) > 0:
#perform the shutdown
startingUp = ec2.instances.filter(InstanceIds=StoppedInstances).start()
print startingUp
else:
print "Nothing to see here"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment