-
Notifications
You must be signed in to change notification settings - Fork 434
docs(middleware-factory): snippets split, improved, and lint #1451
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
Changes from all commits
597f2cf
ec90c5d
5cdf6c6
42f03c9
4c2a1e7
b2e1945
54a0600
fbe67c7
79b7b63
78e7bd2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
Transform: AWS::Serverless-2016-10-31 | ||
Description: Middleware-powertools-utilities example | ||
|
||
Globals: | ||
Function: | ||
Timeout: 5 | ||
Runtime: python3.9 | ||
Tracing: Active | ||
Architectures: | ||
- x86_64 | ||
Environment: | ||
Variables: | ||
LOG_LEVEL: DEBUG | ||
POWERTOOLS_LOGGER_SAMPLE_RATE: 0.1 | ||
POWERTOOLS_LOGGER_LOG_EVENT: true | ||
POWERTOOLS_SERVICE_NAME: middleware | ||
|
||
Resources: | ||
MiddlewareFunction: | ||
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction | ||
Properties: | ||
CodeUri: middleware/ | ||
Handler: app.lambda_handler | ||
Description: Middleware function | ||
Policies: | ||
- AWSLambdaBasicExecutionRole # Managed Policy | ||
- Version: '2012-10-17' # Policy Document | ||
Statement: | ||
- Effect: Allow | ||
Action: | ||
- dynamodb:PutItem | ||
Resource: !GetAtt HistoryTable.Arn | ||
- Effect: Allow | ||
Action: # https://docs.aws.amazon.com/appconfig/latest/userguide/getting-started-with-appconfig-permissions.html | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if I'm comfortable with this, but from what I can see it's a real example in an official AWS documentation. |
||
- ssm:GetDocument | ||
- ssm:ListDocuments | ||
- appconfig:GetLatestConfiguration | ||
- appconfig:StartConfigurationSession | ||
- appconfig:ListApplications | ||
- appconfig:GetApplication | ||
- appconfig:ListEnvironments | ||
- appconfig:GetEnvironment | ||
- appconfig:ListConfigurationProfiles | ||
- appconfig:GetConfigurationProfile | ||
- appconfig:ListDeploymentStrategies | ||
- appconfig:GetDeploymentStrategy | ||
- appconfig:GetConfiguration | ||
- appconfig:ListDeployments | ||
- appconfig:GetDeployment | ||
Resource: "*" | ||
Events: | ||
GetComments: | ||
Type: Api | ||
Properties: | ||
Path: /comments | ||
Method: GET | ||
GetCommentsById: | ||
Type: Api | ||
Properties: | ||
Path: /comments/{comment_id} | ||
Method: GET | ||
|
||
# DynamoDB table to store historical data | ||
HistoryTable: | ||
Type: AWS::DynamoDB::Table | ||
Properties: | ||
TableName: "HistoryTable" | ||
AttributeDefinitions: | ||
- AttributeName: customer_id | ||
AttributeType: S | ||
- AttributeName: request_id | ||
AttributeType: S | ||
KeySchema: | ||
- AttributeName: customer_id | ||
KeyType: HASH | ||
- AttributeName: request_id | ||
KeyType: "RANGE" | ||
BillingMode: PAY_PER_REQUEST | ||
|
||
# Feature flags using AppConfig | ||
FeatureCommentApp: | ||
Type: AWS::AppConfig::Application | ||
Properties: | ||
Description: "Comments Application for feature toggles" | ||
Name: comments | ||
|
||
FeatureCommentDevEnv: | ||
Type: AWS::AppConfig::Environment | ||
Properties: | ||
ApplicationId: !Ref FeatureCommentApp | ||
Description: "Development Environment for the App Config Comments" | ||
Name: dev | ||
|
||
FeatureCommentConfigProfile: | ||
Type: AWS::AppConfig::ConfigurationProfile | ||
Properties: | ||
ApplicationId: !Ref FeatureCommentApp | ||
Name: features | ||
LocationUri: "hosted" | ||
|
||
HostedConfigVersion: | ||
Type: AWS::AppConfig::HostedConfigurationVersion | ||
Properties: | ||
ApplicationId: !Ref FeatureCommentApp | ||
ConfigurationProfileId: !Ref FeatureCommentConfigProfile | ||
Description: 'A sample hosted configuration version' | ||
Content: | | ||
{ | ||
"save_history": { | ||
"default": true | ||
} | ||
} | ||
ContentType: 'application/json' | ||
|
||
# this is just an example | ||
# change this values according your deployment strategy | ||
BasicDeploymentStrategy: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added the deployment strategy to avoid the default baking time.. The default time (10 minutes) is too long to someone that wants to test this template. |
||
Type: AWS::AppConfig::DeploymentStrategy | ||
Properties: | ||
Name: "Deployment" | ||
Description: "Deployment strategy for comments app." | ||
DeploymentDurationInMinutes: 1 | ||
FinalBakeTimeInMinutes: 1 | ||
GrowthFactor: 100 | ||
GrowthType: LINEAR | ||
ReplicateTo: NONE | ||
|
||
ConfigDeployment: | ||
Type: AWS::AppConfig::Deployment | ||
Properties: | ||
ApplicationId: !Ref FeatureCommentApp | ||
ConfigurationProfileId: !Ref FeatureCommentConfigProfile | ||
ConfigurationVersion: !Ref HostedConfigVersion | ||
DeploymentStrategyId: !Ref BasicDeploymentStrategy | ||
EnvironmentId: !Ref FeatureCommentDevEnv |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import time | ||
from typing import Callable | ||
|
||
import requests | ||
from requests import Response | ||
|
||
from aws_lambda_powertools import Tracer | ||
from aws_lambda_powertools.event_handler import APIGatewayRestResolver | ||
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
tracer = Tracer() | ||
app = APIGatewayRestResolver() | ||
|
||
|
||
@lambda_handler_decorator(trace_execution=True) | ||
def middleware_with_advanced_tracing(handler, event, context) -> Callable: | ||
|
||
tracer.put_metadata(key="resource", value=event.get("resource")) | ||
|
||
start_time = time.time() | ||
response = handler(event, context) | ||
execution_time = time.time() - start_time | ||
|
||
tracer.put_annotation(key="TotalExecutionTime", value=str(execution_time)) | ||
|
||
# adding custom headers in response object after lambda executing | ||
response["headers"]["execution_time"] = execution_time | ||
response["headers"]["aws_request_id"] = context.aws_request_id | ||
|
||
return response | ||
|
||
|
||
@app.get("/products") | ||
def create_product() -> dict: | ||
product: Response = requests.get("https://dummyjson.com/products/1") | ||
product.raise_for_status() | ||
|
||
return {"product": product.json()} | ||
|
||
|
||
@middleware_with_advanced_tracing | ||
def lambda_handler(event: dict, context: LambdaContext) -> dict: | ||
return app.resolve(event, context) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"resource": "/products", | ||
"path": "/products", | ||
"httpMethod": "GET" | ||
} |
Uh oh!
There was an error while loading. Please reload this page.