Last active
August 5, 2020 09:34
-
-
Save hetul99/34bfd8190af7cd89478961936b954104 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
def lambda_handler(event, context): | |
#Get list of Regions | |
ec2_client = boto3.client('ec2') | |
regions = [region['RegionName'] | |
for region in ec2_client.describe_regions()['Regions']] | |
#Iterate over each Region | |
for region in regions: | |
ec2 = boto3.resource('ec2', region_name=region) | |
print("Region:", region) | |
#Get only running instances | |
instances = ec2.instances.filter( | |
Filters=[{'Name': 'instance-state-name', 'Values':['running']}]) | |
#Stop the instances | |
for instance in instances: | |
instance.stop() | |
print('Stopped instance', instance.id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment