Created
May 3, 2023 15:57
-
-
Save AngelChaidez/45c00db308b3251ceb802bb72a7b5e84 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