Created
May 19, 2023 20:35
-
-
Save AngelChaidez/fe3d902573e9429133a67394750ae2a0 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 json | |
import sys | |
import boto3 | |
import os | |
from botocore.exceptions import ClientError | |
ec2 = boto3.client('ec2') | |
regions = [region['RegionName'] | |
for region in ec2.describe_regions()['Regions']] | |
def lambda_handler(event, context): | |
for region in regions: | |
ec2_resource = boto3.resource('ec2', region_name=region) | |
print("Region :", region) | |
instances = ec2_resource.instances.filter( | |
Filters=[{'Name': 'instance-state-name', | |
'Values': ['running']}]) | |
# stop all running instances | |
for instance in instances: | |
instance.stop() | |
print("Instance stopped:", instance.id) | |
return { | |
'statusCode': 200, | |
'body': json.dumps('Hello from Lambda!') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment