Skip to content

Commit

Permalink
Merge pull request #1044 from alephyud/expose-lambda-context
Browse files Browse the repository at this point in the history
Provide Lambda context in event object
  • Loading branch information
stealthycoin authored Jan 31, 2019
2 parents 32fb5be + 5d25969 commit 97902b1
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 @@ -1126,7 +1126,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 @@ -1135,8 +1135,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 @@ -1181,7 +1182,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 @@ -827,6 +827,12 @@ Event Sources
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 @@ -875,6 +881,12 @@ Event Sources
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 @@ -911,6 +923,12 @@ Event Sources

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 @@ -942,6 +960,12 @@ Event Sources
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 @@ -965,6 +989,11 @@ Event Sources
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 @@ -1480,7 +1480,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 @@ -1504,7 +1504,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 @@ -1550,10 +1550,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 @@ -1599,14 +1601,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 97902b1

Please sign in to comment.