Skip to content

Instantly share code, notes, and snippets.

@hetul99
Last active August 5, 2020 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hetul99/34bfd8190af7cd89478961936b954104 to your computer and use it in GitHub Desktop.
Save hetul99/34bfd8190af7cd89478961936b954104 to your computer and use it in GitHub Desktop.
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