Last active
November 22, 2021 16:01
-
-
Save dinushchathurya/a0f39ba12f9bca11fa380cfb8d5795c5 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 | |
#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