Chalice: Can Chalice handle Cloudwatch scheduled events?

Created on 27 Jan 2017  路  4Comments  路  Source: aws/chalice

Primary question: Is there a trick within Chalice to set a fallback handler with no expectations of a URL or content type, to handle scheduled activations?

--
Background:
I have several endpoints in Chalice all working well to send and receive text messages. I have set up a Cloudwatch Scheduled Event that can run Lambda functions regularly. Chalice does not handle these well, or I haven't found a way to do it at least.

The chalice logs shows this for every invocation:

2017-01-27 01:11:27.412000 90b269 Unknown request. (Did you forget to set the Content-Type header?): ChaliceError
Traceback (most recent call last):
  File "/var/task/chalice/app.py", line 236, in __call__
    "Unknown request. (Did you forget to set the Content-Type "

The scheduled trigger has no options to set inputs to the Lambda function, which conceptually doesn't match how Chalice's routing expects to work, thus it falls back to the above error.

So my question is:
Is there a trick within Chalice to set a fallback handler with no expectations of a URL or content type, to handle scheduled activations?

If not, would that be a valid feature request for Chalice? Today, it looks like I will have to separately write a regular Lambda function and manage it manually, separate from my main Chalice project.

Most helpful comment

This is an interesting one.

There's been several requests from people to not require API gateway, and/or pair lambda with something else. Cloudwatch events, Dynamodb streams, kinesis, etc.

Up to this point I've tried to keep the scope of the project focused on API gateway and lambda, but given the repeated feedback I think it makes sense to reassess the scope of chalice and see what we can do.

In the meantime, have you seen https://github.com/kislyuk/domovoi from @kislyuk? I haven't had a chance to try it out myself but it looks similar to what you're trying to accomplish.

All 4 comments

I decided to play with this more. I added logging to the __call__ method and have the regular Lambda event and context data.
event:

{u'account': u'804035054667', u'region': u'us-west-2', 
u'detail': {}, u'detail-type': u'Scheduled Event', u'source': u'aws.events', 
u'version': u'0', u'time': u'2017-01-27T20:02:26Z', 
u'id': u'0cfcac58-adfd-4e03-a4aa-f22b5c3c49f9', u'resources': [u'arn:aws:events:us-west-2:804035054667:rule/10min-scheduler']}

So the source and detail-type field could be the hook to redirect the Lambda activation to a separate function. I think another decorator would be appropriate for handling scheduled events.

Single handler by default:

@app.scheduled
def handle_all_scheduled_events():
    <code>

And optional ability to handle specific named scheduled rules:

@app.scheduled('10min-scheduler')
def handle_specific_scheduler():
    <code>

@app.scheduled('daily-cleanup')
def handle_daily_cleanup():
    <code>

I will take a shot at this in next few weeks, unless someone beats me to it.

This is an interesting one.

There's been several requests from people to not require API gateway, and/or pair lambda with something else. Cloudwatch events, Dynamodb streams, kinesis, etc.

Up to this point I've tried to keep the scope of the project focused on API gateway and lambda, but given the repeated feedback I think it makes sense to reassess the scope of chalice and see what we can do.

In the meantime, have you seen https://github.com/kislyuk/domovoi from @kislyuk? I haven't had a chance to try it out myself but it looks similar to what you're trying to accomplish.

@jamesls count me among the chorus wanting to use Kinesis and similar Lambda integrations. It's common to tie together a few Lambdas via Kinesis/DynamoDB streams being fed by HTTP Lambdas. https://github.com/kislyuk/domovoi looks interesting; gonna check that out next.

Was this page helpful?
0 / 5 - 0 ratings