diff --git a/awsonoff/README.md b/awsonoff/README.md new file mode 100644 index 0000000..9f0d681 --- /dev/null +++ b/awsonoff/README.md @@ -0,0 +1,13 @@ +Hello! Thank you for using awsonoff.py! +This python script is a handy tool for starting and stopping your aws-ec2 instances + +USAGE INSTRUCTIONS +1. Configure your aws connection using the command "aws configure" + -Ensure that you provide the proper public and secret keys, and region name + -these keys can be retrieved through the aws IAM service + +2. Navigate to the location that you have saved the awsonoff.py file + +3. To run type... + - python awsonoff.py [region of instance] [instance id] [on/off] + - example: "python awsonoff.py us-west-1 i-asc563varg4 on" diff --git a/awsonoff/awsonoff.py b/awsonoff/awsonoff.py new file mode 100644 index 0000000..205baae --- /dev/null +++ b/awsonoff/awsonoff.py @@ -0,0 +1,45 @@ +#***************************************************************** +# Author: Cody WIlliams +# +# Date: 2020-01-06 +# +# Program Name: awspower.py +# +# Description: This script should be ran from the command line +# with the purpose of starting / stopping an +# AWS ec2 instance. This utilizes the +# AWS boto3 API. To use, type... +# python awspower.py [region] [instance-id] [on/off] +#***************************************************************** + + +import boto3 #used to interact with AWS API +import sys #used to support command line functionality + +#Prints the usage of the script +def print_usage(): + print('USAGE: python awspower.py [region] [instance-id] [on/off]') + +#function for the creation of a session in a specified region +def get_session(r): + return boto3.session.Session(region_name=region) + +#Retrieve arguements from the command line +region = sys.argv[1] +ID = sys.argv[2] +action = sys.argv[3] + +#Create the session and client +session = get_session(region) +client = session.client('ec2') + +#start the instance or stop the instance +if action == 'on': + client.start_instances(InstanceIds=[ID]) + print('ec2 instance ' + ID + ' turned on!') +elif action == 'off': + client.stop_instances(InstanceIds=[ID]) + print('ec2 instance ' + ID + ' turned off!') +else: + print_usage() + \ No newline at end of file diff --git a/awsonoff/requirements.txt b/awsonoff/requirements.txt new file mode 100644 index 0000000..0188631 --- /dev/null +++ b/awsonoff/requirements.txt @@ -0,0 +1,9 @@ +boto3==1.10.46 +botocore==1.13.46 +docutils==0.15.2 +futures==3.3.0 +jmespath==0.9.4 +python-dateutil==2.8.1 +s3transfer==0.2.1 +six==1.13.0 +urllib3==1.25.7