-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
27 lines (27 loc) · 1.36 KB
/
lambda_function.py
File metadata and controls
27 lines (27 loc) · 1.36 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
import boto3,subprocess,os
def lambda_handler(event, context):
#get lambda variables
executionArg = event['executionArg']
executionPayload = event['executionPayload']
#environment variables required for powershell to start
os.environ["HOME"] = "/tmp"
os.environ["PSHOME"] = "/tmp"
#need to make powershell binary executable
process = subprocess.Popen(["/bin/chmod +x /var/task/pwsh"],stdout=subprocess.PIPE,stderr=subprocess.STDOUT,shell=True)
if executionArg == "-c":
process = subprocess.Popen(["/var/task/pwsh -c '{}'".format(executionPayload)],stdout=subprocess.PIPE,stderr=subprocess.STDOUT,shell=True)
elif executionArg == "-enc":
process = subprocess.Popen(["/var/task/pwsh -enc {}".format(executionPayload)],stdout=subprocess.PIPE,stderr=subprocess.STDOUT,shell=True)
elif executionArg == "-f":
s3_client = boto3.client('s3')
bucket = executionPayload['bucket']
key = executionPayload['key']
localPath = os.path.join('/tmp',key.split("/")[-1])
s3_client.download_file(bucket, key, localPath)
process = subprocess.Popen(["/var/task/pwsh -f {}".format(localPath)],stdout=subprocess.PIPE,stderr=subprocess.STDOUT,shell=True)
else:
return "Unrecognized executionArg"
returncode = process.wait()
output = process.stdout.read()
print output
return output