Skip to content

Instantly share code, notes, and snippets.

@a-reda
Created March 7, 2019 11:11
Show Gist options
  • Save a-reda/dda1aa4584d1698b4fbc89e86c8fe5fb to your computer and use it in GitHub Desktop.
Save a-reda/dda1aa4584d1698b4fbc89e86c8fe5fb to your computer and use it in GitHub Desktop.
Simple script to control ec2 instances
import boto3
import sys
import pprint
# Let's use Amazon S3
ec2 = boto3.client('ec2')
ec2Res = boto3.resource('ec2')
if len(sys.argv) < 2:
print("No argument provided ... just listing")
action = "LIST"
else:
action = sys.argv[1].upper()
print("{} action requested \n".format(action))
instances = ec2Res.instances.filter()
instancesIds = []
for i in instances:
instancesIds.append(i.id)
if action == "ON":
print("Starting instances ...")
response = ec2.start_instances(InstanceIds=instancesIds)
pprint.pprint(response)
elif action == "OFF":
print("Stopping instances ...")
response = ec2.stop_instances(InstanceIds=instancesIds)
pprint.pprint(response)
elif action == "LIST":
print("Status of instances: \n")
for i in instances:
print("{} is {}".format(i.id, i.state["Name"]))
print("\n")
elif action == "REBOOT":
print("Rebooting instances: \n")
response = ec2.reboot_instances(InstanceIds=instancesIds)
pprint.pprint(response)
elif action == "GIT":
print("Running git ... \n")
ssm = boto3.client("ssm")
else:
print("Action {} not available".format(action))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment