Skip to content

Commit

Permalink
Provide Lambda context in event object
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Ivanov committed Jan 13, 2019
1 parent 9f20ec2 commit 5d25969
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
7 changes: 4 additions & 3 deletions chalice/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ def __init__(self, func, event_class):
self.event_class = event_class

def __call__(self, event, context):
event_obj = self.event_class(event)
event_obj = self.event_class(event, context)
return self.func(event_obj)


Expand All @@ -1001,8 +1001,9 @@ def __call__(self, event, context):
# part of Chalice's public API and must be backwards compatible.

class BaseLambdaEvent(object):
def __init__(self, event_dict):
def __init__(self, event_dict, context):
self._event_dict = event_dict
self.context = context
self._extract_attributes(event_dict)

def _extract_attributes(self, event_dict):
Expand Down Expand Up @@ -1047,7 +1048,7 @@ def _extract_attributes(self, event_dict):

def __iter__(self):
for record in self._event_dict['Records']:
yield SQSRecord(record)
yield SQSRecord(record, self.context)


class SQSRecord(BaseLambdaEvent):
Expand Down
29 changes: 29 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,12 @@ Scheduled Events
For scheduled events, this will include the ARN of the CloudWatch
rule that triggered this event.

.. attribute:: context

A `Lambda context object <https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html>`_
that is passed to the handler by AWS Lambda. This is useful if you need
the AWS request ID for tracing, or any other data in the context object.

.. method:: to_dict()

Return the original event dictionary provided
Expand Down Expand Up @@ -860,6 +866,12 @@ Scheduled Events
access to the original URL encoded key name, you can
access it through the ``to_dict()`` method.

.. attribute:: context

A `Lambda context object <https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html>`_
that is passed to the handler by AWS Lambda. This is useful if you need
the AWS request ID for tracing, or any other data in the context object.

.. method:: to_dict()

Return the original event dictionary provided
Expand Down Expand Up @@ -896,6 +908,12 @@ Scheduled Events

The string value of the SNS message that was published.

.. attribute:: context

A `Lambda context object <https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html>`_
that is passed to the handler by AWS Lambda. This is useful if you need
the AWS request ID for tracing, or any other data in the context object.

.. method:: to_dict()

Return the original event dictionary provided
Expand Down Expand Up @@ -927,6 +945,12 @@ Scheduled Events
the event. Each element in the iterable is of type
:class:`SQSRecord`.

.. attribute:: context

A `Lambda context object <https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html>`_
that is passed to the handler by AWS Lambda. This is useful if you need
the AWS request ID for tracing, or any other data in the context object.

.. method:: to_dict()

Return the original event dictionary provided
Expand All @@ -950,6 +974,11 @@ Scheduled Events
if you need to manually delete an SQS message to account for
partial failures.

.. attribute:: context

A `Lambda context object <https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html>`_
that is passed to the handler by AWS Lambda.

.. method:: to_dict()

Return the original dictionary associated with the given
Expand Down
12 changes: 8 additions & 4 deletions tests/unit/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,7 @@ def test_s3_event_urldecodes_keys():
}},
]
}
event = app.S3Event(s3_event)
event = app.S3Event(s3_event, FakeLambdaContext())
# We should urldecode the key name.
assert event.key == 'file with spaces'
# But the key should remain unchanged in to_dict().
Expand All @@ -1487,7 +1487,7 @@ def test_s3_event_urldecodes_unicode_keys():
}},
]
}
event = app.S3Event(s3_event)
event = app.S3Event(s3_event, FakeLambdaContext())
# We should urldecode the key name.
assert event.key == u'\u2713'
assert event.bucket == u'mybucket'
Expand Down Expand Up @@ -1533,10 +1533,12 @@ def handler(event):
'TopicArn': 'arn:aws:sns:us-west-2:12345:ConsoleTestTopic',
'Type': 'Notification',
'UnsubscribeUrl': 'https://unsubscribe-url/'}}]}
actual_event = handler(sns_event, context=None)
lambda_context = FakeLambdaContext()
actual_event = handler(sns_event, context=lambda_context)
assert actual_event.message == 'This is a raw message'
assert actual_event.subject == 'ThisIsTheSubject'
assert actual_event.to_dict() == sns_event
assert actual_event.context == lambda_context


def test_can_create_sqs_handler(sample_app):
Expand Down Expand Up @@ -1582,14 +1584,16 @@ def handler(event):
'messageId': 'message-id',
'receiptHandle': 'receipt-handle'
}]}
actual_event = handler(sqs_event, context=None)
lambda_context = FakeLambdaContext()
actual_event = handler(sqs_event, context=lambda_context)
records = list(actual_event)
assert len(records) == 1
first_record = records[0]
assert first_record.body == 'queue message body'
assert first_record.receipt_handle == 'receipt-handle'
assert first_record.to_dict() == sqs_event['Records'][0]
assert actual_event.to_dict() == sqs_event
assert actual_event.context == lambda_context


def test_bytes_when_binary_type_is_application_json():
Expand Down

0 comments on commit 5d25969

Please sign in to comment.