Last active
March 25, 2018 18:59
-
-
Save slashr/98e1e4281974fca5b28be8df96c377b3 to your computer and use it in GitHub Desktop.
This file contains 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
import boto3 | |
import logging | |
#Simple logging | |
logger = logging.getLogger() | |
logger.setLevel(logging.INFO) | |
#Create EC2 connection object | |
ec2 = boto3.resource('ec2') | |
def lambda_handler(event, context): | |
#Filter instance having specified tag and status stopped | |
filter = [{'Name': 'tag:AutoStop', 'Values': ['Yes', 'yes', 'YES']}, {'Name': 'instance-state-name','Values': ['stopped']}] | |
instances = ec2.instances.filter(Filters=filter) | |
#Get IDs of all stopped and tagged instances | |
instance_ids = [instance.id for instance in instances] | |
#Print list of instances that will be started to logs | |
print instance_ids | |
if len(instance_ids) > 0: | |
#Start instances | |
starting_instances = ec2.instances.filter(InstanceIds=instance_ids).start() | |
print starting_instances | |
else: | |
print "No instances to be started found" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment