-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpower_instance.py
More file actions
85 lines (66 loc) · 2.16 KB
/
power_instance.py
File metadata and controls
85 lines (66 loc) · 2.16 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import json
import boto3
import os
REGION = os.environ["REGION"]
ec2 = boto3.client("ec2", region_name=REGION)
def get_state(instance_id):
#Running Code: 16
#Stopping Code: 64
#Stopped Code: 80
#Pending Code: 0
try:
instances = ec2.describe_instances(InstanceIds=[instance_id])
return instances['Reservations'][0]['Instances'][0]['State']
except Exception:
return None
def start_instance(instance_id):
state_code = get_state(instance_id)['Code']
if state_code == 0 or state_code == 16:
#pending or running
return False
try:
ec2.start_instances(InstanceIds=[instance_id])
return True
except Exception:
return False
def stop_instance(instance_id):
state_code = get_state(instance_id)['Code']
if state_code == 80 or state_code == 64:
#stopping or stopped
return False
try:
ec2.stop_instances(InstanceIds=[instance_id])
return True
except Exception:
return False
def return_data(status_code, operation, instance_id):
data={
'operation': operation,
'instanceId': instance_id,
'state': get_state(instance_id)
}
return {
"isBase64Encoded": False,
'statusCode': status_code,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': json.dumps(data)
}
def operation_instance(operation, instance_id):
if not get_state(instance_id):
return return_data(400, False, None)
if operation == 'start':
return return_data(200, start_instance(instance_id), instance_id)
elif operation == 'stop':
return return_data(200, stop_instance(instance_id), instance_id)
elif operation == 'state':
return return_data(200, None, instance_id)
def lambda_handler(event, context):
try:
operation = event['queryStringParameters']['operation']
instance_id = event['queryStringParameters']['instanceId']
return operation_instance(operation, instance_id)
except Exception:
return return_data(400, False, None)