Created
March 7, 2019 11:11
-
-
Save a-reda/dda1aa4584d1698b4fbc89e86c8fe5fb to your computer and use it in GitHub Desktop.
Simple script to control 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 | |
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