-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda-warmer-python.py
118 lines (92 loc) · 2.7 KB
/
lambda-warmer-python.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import datetime
import random
import time
import os
import json
from boto3 import client as boto3_client
import asyncio
instanceID = datetime.datetime.now().strftime("%Y/%m/%d-%I:%M%p") + '-' + ('0000' + str(int(random.random()*1000)))
warm = False
funcName = os.environ["AWS_LAMBDA_FUNCTION_NAME"]
region = os.environ["AWS_REGION"]
async def invoke_lambda(lambda_client, funcName, InvocationTypeString, Payload):
lambda_client.invoke(
FunctionName=funcName,
InvocationType=InvocationTypeString,
LogType='None',
Payload=json.dumps(Payload)
)
def handler(event, context):
global instanceID
global warm
global funcName
config = {
"flag": 'warmer', #default test flag
"concurrency": 1, #default concurrency field
"test": True, #default test flag
"log": True, #default logging to true
"correlationId": instanceID, #default the correlationId
"delay": 75 #default the delay to 75ms
}
# handle the default arguments
for tempKey in config.keys():
if tempKey in event.keys():
config[tempKey] = event[tempKey]
if (event and event["flag"]):
if config["concurrency"] > 1:
concurrency = config["concurrency"]
else:
concurrency = 1
if '__WARMER_INVOCATION__' in event.keys():
invokeCount = event['__WARMER_INVOCATION__']
else:
invokeCount = 1
if '__WARMER_CONCURRENCY__' in event.keys():
invokeTotal = event['__WARMER_CONCURRENCY__']
else:
invokeTotal = concurrency
if '__WARMER_CORRELATIONID__' in event.keys():
correlationId = event['__WARMER_CORRELATIONID__']
else:
correlationId = config["correlationId"]
# flag as warm
warm = True
# Create log record
log = {
"action": 'warmer',
"function": funcName,
"instanceID": instanceID,
"correlationId": correlationId,
"count": invokeCount,
"concurrency": invokeTotal,
"isWarmed": warm
}
# Log it
if config['log']:
print(log)
if concurrency > 1 and not config['test']:
# invoke
lambda_client = boto3_client('lambda', region_name=region)
loop = asyncio.new_event_loop()
for i in range(1, concurrency):
if i == (concurrency-1):
InvocationTypeString = "RequestResponse"
else:
InvocationTypeString = "Event"
Payload = {
"flag": config["flag"], #send warmer flag
"__WARMER_INVOCATION__": i+1, #send invocation number
"__WARMER_CONCURRENCY__": concurrency, #send total concurrency
"__WARMER_CORRELATIONID__": correlationId #send correlation id
}
future = loop.create_task(invoke_lambda(lambda_client, funcName, InvocationTypeString, Payload))
loop.run_until_complete(future)
loop.close()
return True
elif invokeCount > 1:
time.sleep(config['delay']/1000.0)
return True
return True
else:
warm = True
return False