-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAWS_Lambda_hack.py
78 lines (64 loc) · 2.53 KB
/
AWS_Lambda_hack.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
import json
def cleanUp():
#sometimes, consecutive runs of Lambda doesn't clean up temp files & throws memory error
import os
import sys
print("The dir is: %s" %os.listdir('/tmp/'))
# listing directories after removing path
print("The dir after removal of path : %s" %os.listdir('/tmp/'))
print('Clearing up temp space.')
import shutil
os.chdir('/tmp/')
listdirs = os.listdir()
for dirname in listdirs:
try:
shutil.rmtree(dirname)
except:
os.remove(dirname) #prob a file, remove it
os.chdir('..')
print("The dir after removal of path : %s" %os.listdir('/tmp/'))
return
def grabLib(BUCKET_NAME = 'YOUR_BUCKET_HERE', # replace with your bucket name
KEY = 'YOUR_OBJECT_KEY_FOR_YOUR_CUSTOM_LIBRARY_HERE', # replace with your object key,
FILENAME ='PGF.py'):
#function I wrote to mannually import into AWS Lambda's temp env from wherever in S3
import boto3
import botocore
s3 = boto3.resource('s3')
try:
s3.Bucket(BUCKET_NAME).download_file(KEY, '/tmp/'+FILENAME)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
print(FILENAME + " cannot be found.")
else:
raise
def lambda_handler(event, context):
cleanUp()
#Grab Pythena Library (all in one python file)
grabLib(KEY = 'YOUR_OBJECT_KEY_FOR_YOUR_CUSTOM_LIBRARY_HERE', # replace with your object key,
FILENAME ='PGF.py')
#GRAB REQUIRED FILES -- zipped non-supported python libraries
grabLib(KEY = 'OBJECT_KEY_FOR_ZIP_PACKAGE_CONTAINING_LIBRARIES_YOURS_IS_DEPENDENT_ON.zip', # replace with your object key,
FILENAME ='PRL.zip')
import os
import zipfile
#We can only modify/downoad to in the /tmp/ folder -- that's fine
path_to_zip_file = '/tmp/PRL.zip'
directory_to_extract_to = '/tmp/'
zip_ref = zipfile.ZipFile(path_to_zip_file, 'r')
zip_ref.extractall(directory_to_extract_to)
zip_ref.close()
print(os.path)
#we can just add the tmp to our system envirnment
import sys
sys.path.append(os.path.abspath('/tmp'))
import PGF
# import test
# test.main()
#PUT IN PARAMATERS RIGHT HERE - AWS LAMBDA BENDS TO MY WILL MUAHAHAHA
import PGF
PGF.YOUR_FUNCTION_FROM_YOUR_LIBRARY_HERE()
return {
"statusCode": 200,
"body": json.dumps('Hello from Lambda!')
}