Last active
November 22, 2021 16:02
-
-
Save dinushchathurya/1a27f153fc35c5ee7f7ca6effba3527b 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 and set the region | |
ec2 = boto3.resource('ec2', region_name='ap-southeast-1') | |
def lambda_handler(event, context): | |
# all running EC2 instances. | |
filters = [{ | |
'Name': 'tag:AutoStop', | |
'Values': ['True'] | |
}, | |
{ | |
'Name': 'instance-state-name', | |
'Values': ['running'] | |
} | |
] | |
#filter the instances which are stopped | |
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 | |
if len(RunningInstances) > 0: | |
#perform the shutdown | |
shuttingDown = ec2.instances.filter(InstanceIds=RunningInstances).stop() | |
print(shuttingDown) | |
else: | |
print("Nothing to see here") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment