Skip to content

Instantly share code, notes, and snippets.

@misskecupbung
Created January 23, 2023 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misskecupbung/0b2c5bec7d961d488e5c528c437b26e2 to your computer and use it in GitHub Desktop.
Save misskecupbung/0b2c5bec7d961d488e5c528c437b26e2 to your computer and use it in GitHub Desktop.
import boto3
def GetInstanceRunning():
client = boto3.client('ec2')
ec2_regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
for region in ec2_regions:
ec2 = boto3.resource('ec2')
instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
return [instance.id for instance in instances]
def StopInstance(ids=GetInstanceRunning()):
ec2 = boto3.client('ec2')
if not ids:
print("No Instance in the state Running or pending")
else:
ec2.stop_instances(InstanceIds=ids)
ec2.get_waiter('instance_stopped').wait(InstanceIds=ids)
print('instance {} was shutdown'.format(ids))
def lambda_handler(event, context):
StopInstance()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment