Skip to content

Instantly share code, notes, and snippets.

@corespace
Created April 24, 2017 18:09
Show Gist options
  • Save corespace/b454408775058019e05e2d583b835f2c to your computer and use it in GitHub Desktop.
Save corespace/b454408775058019e05e2d583b835f2c to your computer and use it in GitHub Desktop.
Chalice tip from Chris

Because as you know I hate doing complicated maintenance, I've started adding this function to the bottom of my Chalice app.py file:

"""
This function allows me to use the same Lambda for HTTP (Api gateway), CRON (Cloudwatch poll) and
Batch (Kinesis - putting an item in the kinesis stream triggers a call to this function) 
"""
def command_router(event, context):
    if 'requestContext' in event:
        logging.warn("Processing as HTTP request")
        return app(event,context) # Process via Chalice
    elif 'detail-type' in event and event['detail-type']=='Scheduled Event':
        CronHandler(event=event,context=context).execute()
    elif 'Records' in event and len(event['Records'])>0 and 'kinesis' in event['Records'][0]:
        BatchHandler(event=event,context=context).execute()
    else:
        raise BadRequestError('Currently unhandled event: {}'.format(event))

Then I configure Lambda to run app.command_router instead of app.app - This way, I can use the same function for http and batch processing (I use a kinesis stream as a batch processing queue). Just thought it might interest you!

Chris

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment