mirror of
https://github.com/hastagAB/Awesome-Python-Scripts.git
synced 2024-11-27 22:11:07 +00:00
start or stop aws ec2
This commit is contained in:
parent
31fc25db8c
commit
fe26fd64ba
13
awsonoff/README.md
Normal file
13
awsonoff/README.md
Normal file
|
@ -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"
|
45
awsonoff/awsonoff.py
Normal file
45
awsonoff/awsonoff.py
Normal file
|
@ -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()
|
||||
|
9
awsonoff/requirements.txt
Normal file
9
awsonoff/requirements.txt
Normal file
|
@ -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
|
Loading…
Reference in New Issue
Block a user