Skip to content

Instantly share code, notes, and snippets.

@AngelChaidez
Created May 19, 2023 20:35
Show Gist options
  • Save AngelChaidez/fe3d902573e9429133a67394750ae2a0 to your computer and use it in GitHub Desktop.
Save AngelChaidez/fe3d902573e9429133a67394750ae2a0 to your computer and use it in GitHub Desktop.
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