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

feat(data-classes): Add S3 Object Lambda Event #353

Merged
merged 15 commits into from
Mar 22, 2021

Conversation

michaelbrewer
Copy link
Contributor

@michaelbrewer michaelbrewer commented Mar 20, 2021

Issue #, if available:

Description of changes:

Add data class for Amazon S3 Object Lambda

Example usage based on the AWS Blog Post on S3 Object Lambda

import requests
from aws_lambda_powertools import Logger
from aws_lambda_powertools.logging.correlation_paths import S3_OBJECT_LAMBDA
from aws_lambda_powertools.utilities.data_classes.s3_object_event import S3ObjectLambdaEvent

logger = Logger()

@logger.inject_lambda_context(correlation_id_path=S3_OBJECT_LAMBDA, log_event=True)
def lambda_handler(event, context):
    event = S3ObjectLambdaEvent(event)

    object_context = event.object_context
    request_route = object_context.output_route
    request_token = object_context.output_token
    s3_url = object_context.input_s3_url

    # Get object from S3
    response = requests.get(s3_url)
    original_object = response.content.decode("utf-8")

    # Transform object
    transformed_object = original_object.upper()

    # Write object back to S3 Object Lambda
    s3 = boto3.client("s3")
    s3.write_get_object_response(Body=transformed_object, RequestRoute=request_route, RequestToken=request_token)

    return {"status_code": 200}

Checklist

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Michael Brewer added 2 commits March 20, 2021 08:45
Changes:
- Add support for get_header_value
- Add docs with example
- Add test coverage
- Add example userName example event json
@michaelbrewer michaelbrewer marked this pull request as ready for review March 20, 2021 17:17
@codecov-io
Copy link

codecov-io commented Mar 20, 2021

Codecov Report

Merging #353 (a5d87c0) into develop (961b25e) will decrease coverage by 0.05%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #353      +/-   ##
===========================================
- Coverage    99.88%   99.83%   -0.06%     
===========================================
  Files           95       96       +1     
  Lines         3529     3635     +106     
  Branches       173      174       +1     
===========================================
+ Hits          3525     3629     +104     
- Misses           2        4       +2     
  Partials         2        2              
Impacted Files Coverage Δ
aws_lambda_powertools/logging/correlation_paths.py 100.00% <100.00%> (ø)
...wertools/utilities/data_classes/s3_object_event.py 100.00% <100.00%> (ø)
aws_lambda_powertools/utilities/parser/types.py 71.42% <0.00%> (-28.58%) ⬇️

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 961b25e...a5d87c0. Read the comment docs.

@michaelbrewer
Copy link
Contributor Author

@heitorlessa still need to add S3ObjectSessionContext class potentially, I just wanted to check if this was something reusable as it would also help with CloudTrail event logs processing

@michaelbrewer michaelbrewer changed the title feat(data-classes): Add S3 Object Event feat(data-classes): Add S3 Object Lambda Event Mar 20, 2021
Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

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

This is awesome and so timely - added one major ask and a nitpick

aws_lambda_powertools/utilities/data_classes/s3_event.py Outdated Show resolved Hide resolved
aws_lambda_powertools/utilities/data_classes/s3_event.py Outdated Show resolved Hide resolved
@heitorlessa heitorlessa added feature New feature or functionality area/utilities labels Mar 20, 2021
@heitorlessa
Copy link
Contributor

heitorlessa commented Mar 20, 2021

On the readability thing, I need to get my laptop to take a proper look - We can do on Monday (your morning) as part of an experimental office hours

I'm all for reusing as long as UX doesn't confuse S3 Object with S3 Object Lambda (naming challenging that will confuse for sure)

@heitorlessa
Copy link
Contributor

heitorlessa commented Mar 20, 2021 via email

Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

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

Is correlation path S3_OBJECT or supposed to be S3_OBJECT_LAMBDA?

aws_lambda_powertools/logging/correlation_paths.py Outdated Show resolved Hide resolved
@heitorlessa heitorlessa self-assigned this Mar 22, 2021
Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

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

two suggestions to make this feature and docs even more awesome ;)

@heitorlessa
Copy link
Contributor

heitorlessa commented Mar 22, 2021

Example of UX with these additional changes ;)

They could also be methods, I don't mind either way works as long as we make this as easy as possible to customers as we know the common use cases already

image

@michaelbrewer
Copy link
Contributor Author

@heitorlessa quick question why request_route over output_route for the name?

@heitorlessa
Copy link
Contributor

heitorlessa commented Mar 22, 2021 via email

@heitorlessa
Copy link
Contributor

@heitorlessa quick question why request_route over output_route for the name?

That's to match writer get object response params - RequestRoute & RequestToken.

When you're writing now you'll instinctively look up the event to find these, and they'll find them now effortlessly to comply with the SDK ;-)

Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the super timely addition ;-)

Will release it this week

@heitorlessa heitorlessa merged commit 4179ac1 into aws-powertools:develop Mar 22, 2021
@michaelbrewer michaelbrewer deleted the feat-s3-object branch March 22, 2021 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants