forked from michaelkemp/byu-cloud-conference-educate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stop-instances.py
56 lines (49 loc) · 1.99 KB
/
stop-instances.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
import boto3
region = "us-east-1"
session = boto3.Session()
try:
# on/ON
action = sys.argv[1].upper()
except:
action = "OFF"
if action == "ON":
ec2 = session.client('ec2',region_name=region)
response = ec2.describe_instances()
for r in response['Reservations']:
for i in r['Instances']:
if i['State']['Name'] == "stopped":
try:
start = ec2.start_instances(InstanceIds=[i['InstanceId']], DryRun=False)
print("Starting EC2: " + start["StartingInstances"][0]["InstanceId"])
except:
print("Something went wrong.")
rds = session.client('rds',region_name=region)
response = rds.describe_db_instances()
for r in response['DBInstances']:
if r["DBInstanceStatus"] == "stopped":
try:
start = rds.start_db_instance(DBInstanceIdentifier=r["DBInstanceIdentifier"])
print("Starting RDS: " + start["DBInstance"]["DBInstanceIdentifier"])
except:
print("Something went wrong.")
else:
ec2 = session.client('ec2',region_name=region)
response = ec2.describe_instances()
for r in response['Reservations']:
for i in r['Instances']:
if i['State']['Name'] == "running":
try:
stop = ec2.stop_instances(InstanceIds=[i['InstanceId']], DryRun=False)
print("Stopping EC2: " + stop["StoppingInstances"][0]["InstanceId"])
except:
print("Something went wrong.")
rds = session.client('rds',region_name=region)
response = rds.describe_db_instances()
for r in response['DBInstances']:
if r["DBInstanceStatus"] == "available":
try:
stop = rds.stop_db_instance(DBInstanceIdentifier=r["DBInstanceIdentifier"])
print("Stopping RDS: " + stop["DBInstance"]["DBInstanceIdentifier"])
except:
print("Something went wrong.")