Skip to content

Instantly share code, notes, and snippets.

@marnitto
Created April 25, 2020 15:56
Show Gist options
  • Save marnitto/72ac338cf591c050b81a51c0bc60d70a to your computer and use it in GitHub Desktop.
Save marnitto/72ac338cf591c050b81a51c0bc60d70a to your computer and use it in GitHub Desktop.
Zappa: get 'context' variable from asynchronous task function
# -*- coding: utf-8 -*-
import inspect
def get_context():
# *crawl* hidden context variable from stack frame
for fi in inspect.getouterframes(inspect.currentframe()):
local = fi.frame.f_locals
context = local.get('context')
if 'LambdaContext' in str(type(context)):
return context
@task
def test_task():
context = get_context()
log.info('context=%r' % (context,)) # <bootstrap.LambdaContext object at 0x7f9a0586b250>
return True
def test():
test_task()
return True
'''
Tested version: py3.7
To manually test:
1. zappa deploy (or zappa update)
2. zappa invoke app.test
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment