Last active
February 20, 2024 06:07
-
-
Save wshihadeh/e46ff1ed54c3eb7081e4c26b2c730afe to your computer and use it in GitHub Desktop.
Python script for Stopping and Starting EC2 instances
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 boto3 | |
region = 'eu-central-1' | |
ec2 = boto3.client('ec2', region_name=region) | |
response = ec2.describe_instances(Filters=[ | |
{ | |
'Name': 'tag:Auto-Start', | |
'Values': [ | |
'true', | |
] | |
}, | |
]) | |
instances = [] | |
for reservation in response["Reservations"]: | |
for instance in reservation["Instances"]: | |
instances.append(instance["InstanceId"]) | |
def stop(event, context): | |
ec2.stop_instances(InstanceIds=instances) | |
print('stopped instances: ' + str(instances)) | |
def start(event, context): | |
ec2.start_instances(InstanceIds=instances) | |
print('started instances: ' + str(instances)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment