Skip to content

Instantly share code, notes, and snippets.

@karl-cardenas-coding
Created November 6, 2021 19:21
Show Gist options
  • Save karl-cardenas-coding/83d47a3654da76e52195fd6bfe27ac88 to your computer and use it in GitHub Desktop.
Save karl-cardenas-coding/83d47a3654da76e52195fd6bfe27ac88 to your computer and use it in GitHub Desktop.
A simple script for stopping EC2
import boto3
def lambda_handler(event, context):
client = boto3.client('ec2')
# ec2_regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
ec2_regions = ["us-east-1", "us-east-2", "us-west-1", "us-west-2"]
for region in ec2_regions:
ec2 = boto3.resource('ec2',region_name=region)
instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
RunningInstances = [instance.id for instance in instances]
for i in RunningInstances:
stoppingInstances = ec2.instances.stop(i)
print(stoppingInstances)
else:
print("No instances stopped")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment