Skip to content

Instantly share code, notes, and snippets.

@wshihadeh
Last active February 20, 2024 06:07
Show Gist options
  • Save wshihadeh/e46ff1ed54c3eb7081e4c26b2c730afe to your computer and use it in GitHub Desktop.
Save wshihadeh/e46ff1ed54c3eb7081e4c26b2c730afe to your computer and use it in GitHub Desktop.
Python script for Stopping and Starting EC2 instances
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