Chalice: KeyError exception when running chalice --invoke with scheduled cron

Created on 1 Nov 2019  路  4Comments  路  Source: aws/chalice

Hello,

I am receiving a KeyError exception when trying to invoke my function using chalice invoke command.

To test, I created a test function:

from chalice import Chalice

app = Chalice(app_name='test')


@app.schedule('cron(0 22 ? * TUE,SAT *)')
def index(event):
    print(event.to_dict())

I can deploy my function with no issue, however when I come to invoke using the chalice CLI I receive the following KeyError exception:

$ chalice invoke --name index
Traceback (most recent call last):
  File "/var/task/chalice/app.py", line 1421, in __call__
    event_obj = self.event_class(event, context)
  File "/var/task/chalice/app.py", line 1449, in __init__
    self._extract_attributes(event_dict)
  File "/var/task/chalice/app.py", line 1460, in _extract_attributes
    self.version = event_dict['version']
KeyError: 'version'
Error: Unhandled exception in Lambda function, details above.

It looks like chalice is attempting to reference the 'version' key which doesn't exist in the event dict.

Same exception happens when I try to invoke using the AWS Lambda console.

Is this a bug or something that I'm not doing correctly?

Most helpful comment

This is because when CloudWatch Events triggers a lambda function it sends an event that looks like the following:

{
    "version": "0",
    "id": "53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa",
    "detail-type": "Scheduled Event",
    "source": "aws.events",
    "account": "123456789012",
    "time": "2015-10-08T16:53:06Z",
    "region": "us-east-1",
    "resources": [
        "arn:aws:events:us-east-1:123456789012:rule/my-scheduled-rule"
    ],
    "detail": {}
}

Since these are required to be sent, chalice extracts them from the event dict and puts them in a class:

class CloudWatchEvent(BaseLambdaEvent):
    def _extract_attributes(self, event_dict):
        self.version = event_dict['version']
        self.account = event_dict['account']
        self.region = event_dict['region']
        self.detail = event_dict['detail']
        self.detail_type = event_dict['detail-type']
        self.source = event_dict['source']
        self.time = event_dict['time']
        self.event_id = event_dict['id']
        self.resources = event_dict['resources']

If you trigger an event manually with invoke, you need to make sure to send an event payload that matches one CloudWatch events would send.

All 4 comments

Same problem.

Invoke on app.lambda_function works. Invoke on app.schedule breaks on missing event_dict key.

Any help appreciated.

Edit: This does not appear related to invoke. Deploying and then testing function in AWS console results in same "KeyError: 'version'"

This is because when CloudWatch Events triggers a lambda function it sends an event that looks like the following:

{
    "version": "0",
    "id": "53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa",
    "detail-type": "Scheduled Event",
    "source": "aws.events",
    "account": "123456789012",
    "time": "2015-10-08T16:53:06Z",
    "region": "us-east-1",
    "resources": [
        "arn:aws:events:us-east-1:123456789012:rule/my-scheduled-rule"
    ],
    "detail": {}
}

Since these are required to be sent, chalice extracts them from the event dict and puts them in a class:

class CloudWatchEvent(BaseLambdaEvent):
    def _extract_attributes(self, event_dict):
        self.version = event_dict['version']
        self.account = event_dict['account']
        self.region = event_dict['region']
        self.detail = event_dict['detail']
        self.detail_type = event_dict['detail-type']
        self.source = event_dict['source']
        self.time = event_dict['time']
        self.event_id = event_dict['id']
        self.resources = event_dict['resources']

If you trigger an event manually with invoke, you need to make sure to send an event payload that matches one CloudWatch events would send.

Ah I understand, so piping a sample event JSON into the chalice invoke command should work similar to this example ?

$ echo '{"version": "0","id": "53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa", "detail-type": "Scheduled Event", "source": "aws.events", "account": "123456789012", "time": "2015-10-08T16:53:06Z", "region": "us-east-1", "resources": ["arn:aws:events:us-east-1:123456789012:rule/my-scheduled-rule"], "detail": {}}' | chalice invoke -n index

Ah I understand, so piping a sample event JSON into the chalice invoke command should work similar to this example ?

$ echo '{"version": "0","id": "53dc4d37-cffa-4f76-80c9-8b7d4a4d2eaa", "detail-type": "Scheduled Event", "source": "aws.events", "account": "123456789012", "time": "2015-10-08T16:53:06Z", "region": "us-east-1", "resources": ["arn:aws:events:us-east-1:123456789012:rule/my-scheduled-rule"], "detail": {}}' | chalice invoke -n index

Sorry for the late response. This works, I can successfully test locally this way. This issue can be closed now, thanks for your help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

laolsson picture laolsson  路  4Comments

adsahay picture adsahay  路  4Comments

Miserlou picture Miserlou  路  4Comments

carlkibler picture carlkibler  路  4Comments

cdalar picture cdalar  路  4Comments