-
Notifications
You must be signed in to change notification settings - Fork 4
/
terminateInstance.py
66 lines (62 loc) · 2.79 KB
/
terminateInstance.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
56
57
58
59
60
61
62
63
64
65
66
# this function is used to terminate Signalling server instances. The user data script for Signalling server includes a logic
# to stop the instance after a predefined period like 20 minutes. Once instance moved to stopped state, an Event bridge rule
# triggers this function to terminate the stopped instance
# a varriation of the Event Bridge rule also triggers ths function on schedule, passing the paramater stopAllServers=true in event
# causing all Signalling servers to be terminated at the same time
import boto3
import json
import os
import json
from boto3.dynamodb.conditions import Attr
def lambda_handler(event, context):
# get all TG to instance mapping from dynamoDB to remove mapping later during termination
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('instanceMapping')
response = table.scan(FilterExpression=Attr('InstanceID').ne(''))
instanceMapping={}
for item in response['Items']:
instanceMapping[item['InstanceID']]=item['TargetGroup']
print(json.dumps(instanceMapping))
if("stopAllServers" in event):
if(event["stopAllServers"]):
ec2 = boto3.client('ec2')
allInstances=[]
response = ec2.describe_instances(Filters=[{'Name': 'tag:type', 'Values': ['signalling']}])
for reservation in response['Reservations']:
instanceID=reservation['Instances'][0]['InstanceId']
print('Found instance id '+instanceID)
allInstances.append(instanceID)
# remove instance mapping from dynamoDb table
table.update_item(
Key={
'TargetGroup': instanceMapping[instanceID],
},
UpdateExpression="set InstanceID = :i",
ExpressionAttributeValues={
':i': ''
}
)
print('will terminate all signalling instances ')
ec2.terminate_instances(InstanceIds=allInstances)
else:
instanceId=event["detail"]["instance-id"]
ec2 = boto3.client('ec2')
# describe instance based on instance id
response = ec2.describe_instances(InstanceIds=[instanceId],Filters=[{'Name': 'tag:type', 'Values': ['signalling']}])
if(len(response['Reservations'])==1):
print('will terminate instance '+instanceId)
# remove instance mapping from dynamoDb table
table.update_item(
Key={
'TargetGroup': instanceMapping[instanceId],
},
UpdateExpression="set InstanceID = :i",
ExpressionAttributeValues={
':i': ''
}
)
ec2.terminate_instances(InstanceIds=[instanceId])
return {
'statusCode': 200,
'body': json.dumps('Instance was terminated successfully !')
}