-
Notifications
You must be signed in to change notification settings - Fork 436
docs(event_handler): add micro function examples #3056
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
Merged
leandrodamascena
merged 16 commits into
aws-powertools:develop
from
BusPatrol:docs/micro-function-examples
Sep 15, 2023
Merged
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
15488fe
docs: add micro function examples
TonySherman 65e79b7
add sam template
TonySherman 868d5ad
Merge branch 'develop' into docs/micro-function-examples
TonySherman 0a34cfb
Merge branch 'develop' into docs/micro-function-examples
TonySherman 9d3a0f9
Merge branch 'develop' into docs/micro-function-examples
TonySherman 7e59795
docs: update example to return correct response code
TonySherman f0a4417
Merge branch 'develop' into docs/micro-function-examples
TonySherman a57c10e
Merge branch 'develop' into docs/micro-function-examples
TonySherman ed3ba21
Add missing .
TonySherman aac0d4b
Update python version in example template
TonySherman cfc93a6
Apply suggestions from code review
TonySherman 62d5bef
Merge branch 'develop' into docs/micro-function-examples
TonySherman db82dfd
additional updates to review suggestions
TonySherman ee1c9ea
Merge branch 'develop' into docs/micro-function-examples
TonySherman 09bea97
Apply suggestions from code review
TonySherman 04cdd00
Merge branch 'develop' into docs/micro-function-examples
TonySherman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
examples/event_handler_rest/sam/micro_function_template.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
Transform: AWS::Serverless-2016-10-31 | ||
Description: > | ||
micro-function-example | ||
|
||
Globals: | ||
Api: | ||
TracingEnabled: true | ||
Cors: # see CORS section | ||
AllowOrigin: "'https://example.com'" | ||
AllowHeaders: "'Content-Type,Authorization,X-Amz-Date'" | ||
MaxAge: "'300'" | ||
BinaryMediaTypes: # see Binary responses section | ||
- "*~1*" # converts to */* for any binary type | ||
|
||
Function: | ||
Timeout: 5 | ||
Runtime: python3.10 | ||
TonySherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Resources: | ||
# Lambda Function Solely For /users endpoint | ||
AllUsersFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
Handler: app.lambda_handler | ||
CodeUri: users | ||
Description: Function for /users endpoint | ||
Architectures: | ||
- x86_64 | ||
Tracing: Active | ||
Events: | ||
UsersPath: | ||
Type: Api | ||
Properties: | ||
Path: /users | ||
Method: GET | ||
MemorySize: 512 # This Lambda Function can have a higher memory size set | ||
TonySherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Environment: | ||
Variables: | ||
LOG_LEVEL: INFO | ||
Tags: | ||
LambdaPowertools: python | ||
|
||
# Lambda Function Solely For /users/{id} endpoint | ||
UserByIdFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
Handler: app.lambda_handler | ||
CodeUri: users_by_id | ||
Description: Function for /users/{id} endpoint | ||
Architectures: | ||
- x86_64 | ||
Tracing: Active | ||
Events: | ||
UsersByIdPath: | ||
Type: Api | ||
Properties: | ||
Path: /users/{id+} | ||
Method: GET | ||
MemorySize: 128 | ||
Environment: | ||
Variables: | ||
LOG_LEVEL: INFO |
65 changes: 65 additions & 0 deletions
65
examples/event_handler_rest/src/micro_function_all_users_route.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import json | ||
from http import HTTPStatus | ||
|
||
from aws_lambda_powertools import Logger | ||
from aws_lambda_powertools.event_handler import APIGatewayRestResolver, Response | ||
from aws_lambda_powertools.utilities.parser import BaseModel | ||
TonySherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
logger = Logger() | ||
|
||
# This would likely be a db lookup | ||
users = [ | ||
{ | ||
"user_id": "b0b2a5bf-ee1e-4c5e-9a86-91074052739e", | ||
"email": "john.doe@example.com", | ||
"active": True, | ||
}, | ||
{ | ||
"user_id": "3a9df6b1-938c-4e80-bd4a-0c966f4b1c1e", | ||
"email": "jane.smith@example.com", | ||
"active": True, | ||
TonySherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
{ | ||
"user_id": "aa0d3d09-9cb9-42b9-9e63-1fb17ea52981", | ||
"email": "alex.wilson@example.com", | ||
"active": True, | ||
}, | ||
{ | ||
"user_id": "67a6c17d-b7f0-4f79-aae0-79f4a53c113b", | ||
"email": "lisa.johnson@example.com", | ||
"active": True, | ||
}, | ||
{ | ||
"user_id": "6e85cf66-47af-4dbf-8aa2-2db3c24f29c1", | ||
"email": "michael.brown@example.com", | ||
"active": False, | ||
}, | ||
TonySherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
|
||
|
||
class User(BaseModel): | ||
user_id: str | ||
email: str | ||
active: bool | ||
TonySherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
app = APIGatewayRestResolver() | ||
|
||
|
||
@app.get("/users") | ||
def all_active_users(): | ||
"""HTTP Response for all active users""" | ||
all_users = [User(**user) for user in users] | ||
all_active_users = [user.dict() for user in all_users if user.active] | ||
TonySherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return Response( | ||
status_code=HTTPStatus.OK.value, | ||
content_type="application/json", | ||
body=json.dumps(all_active_users), | ||
) | ||
|
||
|
||
@logger.inject_lambda_context() | ||
def handler(event: dict, context: LambdaContext) -> dict: | ||
TonySherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return app.resolve(event, context) |
74 changes: 74 additions & 0 deletions
74
examples/event_handler_rest/src/micro_function_user_by_id_route.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import json | ||
from http import HTTPStatus | ||
|
||
from aws_lambda_powertools import Logger | ||
from aws_lambda_powertools.event_handler import APIGatewayRestResolver, Response | ||
from aws_lambda_powertools.utilities.parser import BaseModel | ||
TonySherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
logger = Logger() | ||
|
||
# This would likely be a db lookup | ||
users = [ | ||
{ | ||
"user_id": "b0b2a5bf-ee1e-4c5e-9a86-91074052739e", | ||
"email": "john.doe@example.com", | ||
"active": True, | ||
}, | ||
{ | ||
"user_id": "3a9df6b1-938c-4e80-bd4a-0c966f4b1c1e", | ||
"email": "jane.smith@example.com", | ||
"active": True, | ||
}, | ||
{ | ||
"user_id": "aa0d3d09-9cb9-42b9-9e63-1fb17ea52981", | ||
"email": "alex.wilson@example.com", | ||
"active": True, | ||
}, | ||
{ | ||
"user_id": "67a6c17d-b7f0-4f79-aae0-79f4a53c113b", | ||
"email": "lisa.johnson@example.com", | ||
"active": True, | ||
}, | ||
{ | ||
"user_id": "6e85cf66-47af-4dbf-8aa2-2db3c24f29c1", | ||
"email": "michael.brown@example.com", | ||
"active": False, | ||
}, | ||
TonySherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
|
||
|
||
class User(BaseModel): | ||
user_id: str | ||
email: str | ||
active: bool | ||
TonySherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def get_user_by_id(user_id: str): | ||
for user in users: | ||
if user["user_id"] == user_id: | ||
return User(**user) | ||
TonySherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
app = APIGatewayRestResolver() | ||
|
||
|
||
@app.get("/users/<user_id>") | ||
def all_active_users(user_id: str): | ||
"""HTTP Response for all active users""" | ||
user = get_user_by_id(user_id) | ||
|
||
if user: | ||
return Response( | ||
status_code=HTTPStatus.OK.value, | ||
content_type="application/json", | ||
body=json.dumps(user.dict()), | ||
) | ||
|
||
else: | ||
return Response(status_code=HTTPStatus.NO_CONTENT) | ||
TonySherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
@logger.inject_lambda_context() | ||
def handler(event: dict, context: LambdaContext) -> dict: | ||
TonySherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return app.resolve(event, context) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.