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(event_handler): add support to VPC Lattice payload v2 #3153

Merged
merged 32 commits into from
Oct 5, 2023

Conversation

stephenbawks
Copy link
Contributor

@stephenbawks stephenbawks commented Oct 1, 2023

Issue number: #3160

Summary

Changes

As of September 8 there is a new VPC Lattice Event Type for Lambda.

When you create or update a target group of type LAMBDA, you can specify the version of the event structure that your Lambda function receives. The possible versions are V1 and V2.
https://docs.aws.amazon.com/vpc-lattice/latest/ug/lambda-functions.html

User experience

Using VPCLatticeV2Resolver Event Handler

import requests
from requests import Response

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import VPCLatticeV2Resolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext

tracer = Tracer()
logger = Logger()
app = VPCLatticeV2Resolver()


@app.get("/todos")
@tracer.capture_method
def get_todos():
    todos: Response = requests.get("https://jsonplaceholder.typicode.com/todos")
    todos.raise_for_status()

    # for brevity, we'll limit to the first 10 only
    return {"todos": todos.json()[:10]}


# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.APPLICATION_LOAD_BALANCER)
@tracer.capture_lambda_handler
def lambda_handler(event: dict, context: LambdaContext) -> dict:
    return app.resolve(event, context)

Using VPCLatticeEventV2 Data Class

from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.data_classes import VPCLatticeEventV2, event_source
from aws_lambda_powertools.utilities.typing import LambdaContext

logger = Logger()


@event_source(data_class=VPCLatticeEventV2)
def lambda_handler(event: VPCLatticeEventV2, context: LambdaContext):
    logger.info(event.body)

    response = {
        "isBase64Encoded": False,
        "statusCode": 200,
        "statusDescription": "200 OK",
        "headers": {"Content-Type": "application/text"},
        "body": "VPC Lattice V2 Event ✨🎉✨",
    }

    return response

Using VPCLatticeEventV2 Parser

from aws_lambda_powertools.utilities.parser import event_parser
from aws_lambda_powertools.utilities.parser.models import VpcLatticeV2Model
from aws_lambda_powertools.utilities.typing import LambdaContext

@event_parser(model=VpcLatticeV2Model)
def handler(event: dict, context: LambdaContext):
    body = event.body

    assert body is not None

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

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

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@stephenbawks stephenbawks requested a review from a team as a code owner October 1, 2023 13:39
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 1, 2023
@stephenbawks stephenbawks changed the title VPC Lattice V2 Event Type feat(event_handler): VPC Lattice V2 Event Type Oct 1, 2023
@leandrodamascena leandrodamascena changed the title feat(event_handler): VPC Lattice V2 Event Type feat(event_handler): add support to Vpc Lattice payload v2 Oct 2, 2023
@leandrodamascena
Copy link
Contributor

Hi @stephenbawks! I believe there is some testing and documentation left to be written about this new event source data class. Can you check it please?

Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

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

Missing tests before reviewing this.

@leandrodamascena
Copy link
Contributor

leandrodamascena commented Oct 2, 2023

I see the CI failed because Ruff is complaining. It's easy to fix :D

Thanks for updating this event handler, @stephenbawks! You always tune in to new AWS features.

@codecov-commenter
Copy link

codecov-commenter commented Oct 2, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (583f087) 95.88% compared to head (f9a3ed0) 95.95%.
Report is 1 commits behind head on develop.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #3153      +/-   ##
===========================================
+ Coverage    95.88%   95.95%   +0.07%     
===========================================
  Files          193      195       +2     
  Lines         8241     8364     +123     
  Branches      1536     1559      +23     
===========================================
+ Hits          7902     8026     +124     
  Misses         276      276              
+ Partials        63       62       -1     
Files Coverage Δ
aws_lambda_powertools/event_handler/__init__.py 100.00% <100.00%> (ø)
aws_lambda_powertools/event_handler/api_gateway.py 98.78% <100.00%> (+0.01%) ⬆️
aws_lambda_powertools/event_handler/vpc_lattice.py 100.00% <100.00%> (ø)
...mbda_powertools/utilities/data_classes/__init__.py 100.00% <100.00%> (ø)
..._powertools/utilities/parser/envelopes/__init__.py 100.00% <100.00%> (ø)
...rtools/utilities/parser/envelopes/vpc_latticev2.py 100.00% <100.00%> (ø)
...bda_powertools/utilities/parser/models/__init__.py 100.00% <100.00%> (ø)
...owertools/utilities/parser/models/vpc_latticev2.py 100.00% <100.00%> (ø)
...a_powertools/utilities/data_classes/vpc_lattice.py 98.23% <98.76%> (+4.75%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation tests labels Oct 3, 2023
@leandrodamascena leandrodamascena changed the title feat(event_handler): add support to Vpc Lattice payload v2 feat(event_handler): add support to VPC Lattice payload v2 Oct 3, 2023
Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

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

We need to review the event handler with this new payload. Do you want to add tests or do I?

@pull-request-size pull-request-size bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 4, 2023
@leandrodamascena
Copy link
Contributor

Hey @heitorlessa! Can you review this PR, please?

@leandrodamascena
Copy link
Contributor

Hey @heitorlessa! I updated the PR body and addressed your feedback. Can you review it again, please?

@sonarqubecloud
Copy link

sonarqubecloud bot commented Oct 5, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 3 Code Smells

No Coverage information No Coverage information
0.5% 0.5% Duplication

@leandrodamascena leandrodamascena self-requested a review October 5, 2023 12:21
@leandrodamascena leandrodamascena added parser Parser (Pydantic) utility event_sources Event Source Data Class utility labels Oct 5, 2023
@leandrodamascena leandrodamascena merged commit 2ceb630 into aws-powertools:develop Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation event_handlers event_sources Event Source Data Class utility parser Parser (Pydantic) utility size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: add support for VPC Lattice v2 in Event Handler, Data Class and parser
4 participants