Skip to content

Instantly share code, notes, and snippets.

@dinushchathurya
Last active November 22, 2021 16:01
Show Gist options
  • Save dinushchathurya/a0f39ba12f9bca11fa380cfb8d5795c5 to your computer and use it in GitHub Desktop.
Save dinushchathurya/a0f39ba12f9bca11fa380cfb8d5795c5 to your computer and use it in GitHub Desktop.
import boto3
import logging
#setup simple logging for INFO
logger = logging.getLogger()
logger.setLevel(logging.INFO)
#define the connection
ec2 = boto3.resource('ec2', region_name='ap-south-1')
def lambda_handler(event, context):
# all stopped EC2 instances.
filters = [{
'Name': 'tag:AutoStart',
'Values': ['True']
},
{
'Name': 'instance-state-name',
'Values': ['stopped']
}
]
#filter the instances
instances = ec2.instances.filter(Filters=filters)
#locate all stopped instances
RunningInstances = [instance.id for instance in instances]
#print StoppedInstances
if len(RunningInstances) > 0:
#perform the startup
AutoStarting = ec2.instances.filter(InstanceIds=RunningInstances).start()
print(AutoStarting)
else:
print("Nothing to see here")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment