@@ -42,45 +42,27 @@ This is the sample infrastructure for API Gateway we are using for the examples
4242 Timeout: 5
4343 Runtime: python3.8
4444 Tracing: Active
45- Environment:
45+ Environment:
4646 Variables:
4747 LOG_LEVEL: INFO
4848 POWERTOOLS_LOGGER_SAMPLE_RATE: 0.1
4949 POWERTOOLS_LOGGER_LOG_EVENT: true
5050 POWERTOOLS_METRICS_NAMESPACE: MyServerlessApplication
51- POWERTOOLS_SERVICE_NAME: hello
51+ POWERTOOLS_SERVICE_NAME: my_api-service
5252
5353 Resources:
54- HelloWorldFunction :
54+ ApiFunction :
5555 Type: AWS::Serverless::Function
5656 Properties:
5757 Handler: app.lambda_handler
58- CodeUri: hello_world
59- Description: Hello World function
58+ CodeUri: api_handler/
59+ Description: API handler function
6060 Events:
61- HelloUniverse:
62- Type: Api
63- Properties:
64- Path: /hello
65- Method: GET
66- HelloYou:
67- Type: Api
68- Properties:
69- Path: /hello/{name} # see Dynamic routes section
70- Method: GET
71- CustomMessage:
72- Type: Api
73- Properties:
74- Path: /{message}/{name} # see Dynamic routes section
75- Method: GET
76-
77- Outputs:
78- HelloWorldApigwURL:
79- Description: "API Gateway endpoint URL for Prod environment for Hello World Function"
80- Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello"
81- HelloWorldFunction:
82- Description: "Hello World Lambda Function ARN"
83- Value: !GetAtt HelloWorldFunction.Arn
61+ ApiEvent:
62+ Type: Api
63+ Properties:
64+ Path: /{proxy+} # Send requests on any path to the lambda function
65+ Method: ANY # Send requests using any http method to the lambda function
8466 ```
8567
8668### API Gateway decorator
@@ -360,6 +342,87 @@ You can also combine nested paths with greedy regex to catch in between routes.
360342 ...
361343 }
362344 ```
345+ ### HTTP Methods
346+ You can use named decorators to specify the HTTP method that should be handled in your functions. As well as the
347+ ` get ` method already shown above, you can use ` post ` , ` put ` , ` patch ` , ` delete ` , and ` patch ` .
348+
349+ === "app.py"
350+
351+ ```python hl_lines="9-10"
352+ from aws_lambda_powertools import Logger, Tracer
353+ from aws_lambda_powertools.logging import correlation_paths
354+ from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver
355+
356+ tracer = Tracer()
357+ logger = Logger()
358+ app = ApiGatewayResolver()
359+
360+ # Only POST HTTP requests to the path /hello will route to this function
361+ @app.post("/hello")
362+ @tracer.capture_method
363+ def get_hello_you():
364+ name = app.current_event.json_body.get("name")
365+ return {"message": f"hello {name}"}
366+
367+ # You can continue to use other utilities just as before
368+ @logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
369+ @tracer.capture_lambda_handler
370+ def lambda_handler(event, context):
371+ return app.resolve(event, context)
372+ ```
373+
374+ === "sample_request.json"
375+
376+ ```json
377+ {
378+ "resource": "/hello/{name}",
379+ "path": "/hello/lessa",
380+ "httpMethod": "GET",
381+ ...
382+ }
383+ ```
384+
385+ If you need to accept multiple HTTP methods in a single function, you can use the ` route ` method and pass a list of
386+ HTTP methods.
387+
388+ === "app.py"
389+
390+ ```python hl_lines="9-10"
391+ from aws_lambda_powertools import Logger, Tracer
392+ from aws_lambda_powertools.logging import correlation_paths
393+ from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver
394+
395+ tracer = Tracer()
396+ logger = Logger()
397+ app = ApiGatewayResolver()
398+
399+ # PUT and POST HTTP requests to the path /hello will route to this function
400+ @app.route("/hello", method=["PUT", "POST"])
401+ @tracer.capture_method
402+ def get_hello_you():
403+ name = app.current_event.json_body.get("name")
404+ return {"message": f"hello {name}"}
405+
406+ # You can continue to use other utilities just as before
407+ @logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
408+ @tracer.capture_lambda_handler
409+ def lambda_handler(event, context):
410+ return app.resolve(event, context)
411+ ```
412+
413+ === "sample_request.json"
414+
415+ ```json
416+ {
417+ "resource": "/hello/{name}",
418+ "path": "/hello/lessa",
419+ "httpMethod": "GET",
420+ ...
421+ }
422+ ```
423+
424+ !!! note "It is usually better to have separate functions for each HTTP method, as the functionality tends to differ
425+ depending on which method is used."
363426
364427### Accessing request details
365428
0 commit comments