-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(event_handler): add micro function examples (#3056)
* docs: add micro function examples * add sam template * docs: update example to return correct response code * Add missing . Co-authored-by: Leandro Damascena <[email protected]> Signed-off-by: Tony Sherman <[email protected]> * Update python version in example template Co-authored-by: Leandro Damascena <[email protected]> Signed-off-by: Tony Sherman <[email protected]> * Apply suggestions from code review Co-authored-by: Leandro Damascena <[email protected]> Signed-off-by: Tony Sherman <[email protected]> * additional updates to review suggestions * Apply suggestions from code review Co-authored-by: Leandro Damascena <[email protected]> Signed-off-by: Tony Sherman <[email protected]> --------- Signed-off-by: Tony Sherman <[email protected]> Co-authored-by: Tony Sherman <[email protected]> Co-authored-by: Leandro Damascena <[email protected]>
- Loading branch information
1 parent
1d502b6
commit 4e8ca0f
Showing
5 changed files
with
219 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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.11 | ||
|
||
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: 128 # Each Lambda Function can have it's own memory configuration | ||
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 # Each Lambda Function can have it's own memory configuration | ||
Environment: | ||
Variables: | ||
LOG_LEVEL: INFO |
56 changes: 56 additions & 0 deletions
56
examples/event_handler_rest/src/micro_function_all_users_route.py
This file contains 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,56 @@ | ||
import json | ||
from dataclasses import dataclass | ||
from http import HTTPStatus | ||
|
||
from aws_lambda_powertools import Logger | ||
from aws_lambda_powertools.event_handler import APIGatewayRestResolver, Response | ||
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": "[email protected]", | ||
"active": True, | ||
}, | ||
{ | ||
"user_id": "3a9df6b1-938c-4e80-bd4a-0c966f4b1c1e", | ||
"email": "[email protected]", | ||
"active": False, | ||
}, | ||
{ | ||
"user_id": "aa0d3d09-9cb9-42b9-9e63-1fb17ea52981", | ||
"email": "[email protected]", | ||
"active": True, | ||
}, | ||
] | ||
|
||
|
||
@dataclass | ||
class User: | ||
user_id: str | ||
email: str | ||
active: bool | ||
|
||
|
||
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] | ||
|
||
return Response( | ||
status_code=HTTPStatus.OK.value, | ||
content_type="application/json", | ||
body=json.dumps(all_active_users), | ||
) | ||
|
||
|
||
@logger.inject_lambda_context() | ||
def lambda_handler(event: dict, context: LambdaContext) -> dict: | ||
return app.resolve(event, context) |
71 changes: 71 additions & 0 deletions
71
examples/event_handler_rest/src/micro_function_user_by_id_route.py
This file contains 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,71 @@ | ||
import json | ||
from dataclasses import dataclass | ||
from http import HTTPStatus | ||
|
||
from aws_lambda_powertools import Logger | ||
from aws_lambda_powertools.event_handler import APIGatewayRestResolver, Response | ||
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": "[email protected]", | ||
"active": True, | ||
}, | ||
{ | ||
"user_id": "3a9df6b1-938c-4e80-bd4a-0c966f4b1c1e", | ||
"email": "[email protected]", | ||
"active": False, | ||
}, | ||
{ | ||
"user_id": "aa0d3d09-9cb9-42b9-9e63-1fb17ea52981", | ||
"email": "[email protected]", | ||
"active": True, | ||
}, | ||
] | ||
|
||
|
||
@dataclass | ||
class User: | ||
user_id: str | ||
email: str | ||
active: bool | ||
|
||
|
||
def get_user_by_id(user_id: str) -> Union[User, None]: | ||
for user_data in users: | ||
if user_data["user_id"] == user_id: | ||
return User( | ||
user_id=str(user_data["user_id"]), | ||
email=str(user_data["email"]), | ||
active=bool(user_data["active"]), | ||
) | ||
|
||
return None | ||
|
||
|
||
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.NOT_FOUND) | ||
|
||
|
||
@logger.inject_lambda_context() | ||
def lambda_handler(event: dict, context: LambdaContext) -> dict: | ||
return app.resolve(event, context) |