Created
November 6, 2021 19:21
-
-
Save karl-cardenas-coding/83d47a3654da76e52195fd6bfe27ac88 to your computer and use it in GitHub Desktop.
A simple script for stopping EC2
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 | |
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