Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide Lambda context in event object #1044

Merged
merged 1 commit into from
Jan 31, 2019
Merged

Provide Lambda context in event object #1044

merged 1 commit into from
Jan 31, 2019

Conversation

alephyud
Copy link

Fixes #856.

Adds a context attribute for the event argument of Cloudwatch, S3, SNS and SQS Lambda handlers, as suggested in the issue discussion. That can be used for logging or other purposes:

@app.on_sns_message(topic='my-demo-topic')
def handle_sns_message(event):
    app.log.debug("Received message with subject: %s, message: %s, request id: ",
                  event.subject, event.message, event.context.aws_request_id)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@codecov-io
Copy link

Codecov Report

Merging #1044 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1044      +/-   ##
==========================================
+ Coverage   95.47%   95.47%   +<.01%     
==========================================
  Files          27       27              
  Lines        4488     4489       +1     
  Branches      563      563              
==========================================
+ Hits         4285     4286       +1     
  Misses        131      131              
  Partials       72       72
Impacted Files Coverage Δ
chalice/app.py 96.77% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9f20ec2...5d25969. Read the comment docs.

@jamesls
Copy link
Member

jamesls commented Jan 30, 2019

Thanks for the pull request, taking a look now.

Copy link
Member

@jamesls jamesls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, let's get one more person to review this.

cc @kyleknap @stealthycoin

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

def __iter__(self):
for record in self._event_dict['Records']:
yield SQSRecord(record)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize we have to do this because SQSRecord subclasses BaseLambdaEvent, but I wonder if we need this attribute on child events. You can always access it from the parent event.context attribute.

I suppose there's not really any downsides to passing this through all the child events that are created. We'll also have to do this when we implement dynamodbstream/kinesis events, which have a similar pattern.

cc @kyleknap @stealthycoin

Copy link
Contributor

@stealthycoin stealthycoin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@stealthycoin stealthycoin merged commit 97902b1 into aws:master Jan 31, 2019
@alephyud alephyud deleted the expose-lambda-context branch January 20, 2020 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants