Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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:AutoOff',
- 'Values': ['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
- ec2.create_tags(InstanceIds=RunningInstances, Tags=[{'Key': 'MonitoringEnabled', 'Value': False}])
- else:
- print "Nothing to see here"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement