8000 chore: refactor documentation · ericbn/powertools-lambda-python@28958b1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 28958b1

Browse files
committed
chore: refactor documentation
1 parent f54a585 commit 28958b1

File tree

6 files changed

+61
-68
lines changed

6 files changed

+61
-68
lines changed

docs/core/event_handler/_openapi_customization_swagger.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

docs/core/event_handler/_swagger_ui.md

Lines changed: 0 additions & 10 deletions
This file was deleted.

docs/core/event_handler/api_gateway.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,15 @@ We provide pre-defined errors for the most popular ones such as HTTP 400, 401, 4
476476

477477
!!! note "This feature requires [data validation](#data-validation) feature to be enabled."
478478

479-
--8<-- "docs/core/event_handler/_swagger_ui.md"
479+
Behind the scenes, the [data validation](api_gateway.md#data-validation) feature auto-generates an OpenAPI specification from your routes and type annotations. You can use [Swagger UI](https://swagger.io/tools/swagger-ui/){target="_blank" rel="nofollow"} to visualize and interact with your newly auto-documented API.
480+
481+
There are some important **caveats** that you should know before enabling it:
482+
483+
| Caveat | Description |
484+
| ------------------------------------------------ |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
485+
| Swagger UI is **publicly accessible by default** | When using `enable_swagger` method, you can [protect sensitive API endpoints by implementing a custom middleware](api_gateway.md#customizing-swagger-ui) using your preferred authorization mechanism. |
486+
| **No micro-functions support** yet | Swagger UI is enabled on a per resolver instance which will limit its accuracy here. |
487+
| You need to expose **new routes** | You'll need to expose the following paths to Lambda: `/swagger`, `/swagger.css`, `/swagger.js`; ignore if you're routing all paths already. |
480488

481489
```python hl_lines="12-13" title="enabling_swagger.py"
482490
--8<-- "examples/event_handler_rest/src/enabling_swagger.py"
@@ -927,7 +935,10 @@ To implement these customizations, include extra parameters when defining your r
927935

928936
#### Customizing Swagger UI
929937

930-
--8<-- "docs/core/event_handler/_openapi_customization_swagger.md"
938+
???+note "Customizing the Swagger metadata"
939+
The `enable_swagger` method accepts the same metadata as described at [Customizing OpenAPI metadata](api_gateway.md#customizing-openapi-metadata).
940+
941+
The Swagger UI appears by default at the `/swagger` path, but you can customize this to serve the documentation from another path and specify the source for Swagger UI assets.
931942

932943
Below is an example configuration for serving Swagger UI from a custom path or CDN, with assets like CSS and JavaScript loading from a chosen CDN base URL.
933944

docs/core/event_handler/bedrock_agents.md

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,41 @@ Event handler for Amazon Bedrock Agents, including auto generation of OpenAPI sc
99

1010
* Same declarative syntax as the [other Powertools event handlers](api_gateway.md)
1111
* Drastically reduce the boilerplate to build Agents for Amazon Bedrock
12-
* Automatic generation of OpenAPI schemas from the API
12+
* Automatic generation of OpenAPI schemas from your business logic code
1313
* Built-in data validation for requests/responses
1414

1515
## Getting started
1616

17-
In order to build Bedrock Agents, you need:
17+
```mermaid
18+
flowchart LR
19+
Bedrock <--> Agent
20+
Agent -- invokes --> Lambda[Lambda function]
21+
Agent -- consults --> OpenAPI{{OpenAPI schema}}
22+
OpenAPI -. generated from .-> Lambda
23+
```
24+
25+
To build Bedrock Agents, you need:
1826

19-
* Create the Lambda function that defines the business logic for the action that your agent will carry out
27+
* Create the Lambda function that defines the business logic for the action that your agent carries out.
2028
* Create an OpenAPI schema with the API description, structure, and parameters for the action group.
2129
* Ensure that Bedrock and your Lambda functions have the [necessary permissions](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-permissions.html).
2230

23-
AWS Lambda Powertools facilitates the process by providing support for the development of the Lambda function and the creation of the OpenAPI specification.
31+
Powertools makes it easier to author the Lambda function and the creation of the OpenAPI schema.
2432

25-
!!! info "This is not necessary if you're installing Powertools for AWS Lambda (Python) via [Lambda Layer/SAR](../../index.md#lambda-layer){target="_blank"}."
33+
!!! info "This is unnecessary if you're installing Powertools for AWS Lambda (Python) via [Lambda Layer/SAR](../../index.md#lambda-layer){target="_blank"}."
2634

2735
You need to add `pydantic` as a dependency in your preferred tool _e.g., requirements.txt, pyproject.toml_.
2836

29-
As of now, both Pydantic V1 and V2 are supported. For a future major version, we will only support Pydantic V2.
37+
At this time, we support both Pydantic V1 and V2. For a future major version, we will only support Pydantic V2.
3038

3139
### Your first Agent
3240

33-
To create a Bedrock Agent, use the `BedrockAgentResolver` to annotate your actions. This is
34-
similar to the way [all the other Powertools](api_gateway.md) resolvers work.
41+
To create a Bedrock Agent, use the `BedrockAgentResolver` to annotate your actions.
42+
This is similar to the way [all the other Powertools](api_gateway.md) resolvers work.
3543

36-
Be aware that it's important to include a description for each API endpoint. The description is essential because it provides Bedrock Agents with an understanding of the primary function of your API action.
44+
???+ note
45+
It's important to include a description for each API endpoint.
46+
This provides Bedrock Agents with an understanding of your API action.
3747

3848
=== "Lambda handler"
3949

@@ -42,6 +52,7 @@ Be aware that it's important to include a description for each API endpoint. The
4252
```
4353

4454
1. `description` is a required field in order for Bedrock Agents to work.
55+
2. Powertools take care of parsing, validate, routing and responding to the request.
4556

4657
=== "Input payload"
4758

@@ -55,47 +66,51 @@ Be aware that it's important to include a description for each API endpoint. The
5566
--8<-- "examples/event_handler_bedrock_agents/src/getting_started_output.json"
5667
```
5768

58-
The resolvers utilized by Bedrock Agents are also compatible with the full suite of Powertools utilities. This ensures seamless integration and functionality across the different tools provided by Powertools when working with Bedrock Agents.
69+
The resolvers used by Bedrock Agents are compatible with the full suite of Powertools utilities.
70+
This includes [Logger](../logger.md), [Metrics](../metrics.md) and [Tracer](../tracer.md).
5971

6072
### Generating OpenAPI schemas
6173

62-
To create a schema for your API, use the `get_openapi_json_schema` function provided by the Bedrock Agent resolver. This function will produce a JSON-serialized string that represents your schema. You have the option to either display this string output or save it to a file for future reference.
74+
Use the `get_openapi_json_schema` function provided by the Bedrock Agent resolver.
75+
This function will produce a JSON-serialized string that represents your OpenAPI schema.
76+
You can print this string or save it to a file for future reference.
6377

64-
=== "Generating the OpenAPI schem"
78+
=== "Generating the OpenAPI schema"
6579

6680
```python hl_lines="24 25"
6781
--8<-- "examples/event_handler_bedrock_agents/src/generating_openapi_schema.py"
6882
```
6983

84+
1. This ensures that it's only executed when running the file directly, and not when running on the Lambda runtime.
85+
2. You can use [additional options](#customizing-openapi-metadata) to customize the OpenAPI schema.
86+
7087
=== "OpenAPI schema"
7188

7289
```json hl_lines="13 16 24"
7390
--8<-- "examples/event_handler_bedrock_agents/src/generating_openapi_schema.json"
7491
```
7592

76-
To obtain the OpenAPI schema, execute the Python script from the command line interface (CLI). Upon execution, the script will generate the schema, which will be output directly to the console.
77-
78-
### Creating your Agent in the AWS Console
93+
To get the OpenAPI schema, run the Python script from your terminal.
94+
The script will generate the schema directly to standard output, which you can redirect to a file.
7995

80-
To create a Bedrock Agent, you should refer to the [official documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-create.html) provided by AWS.
81-
82-
During the creation process, when the user interface (UI) prompts you for an OpenAPI specification, you should input the specification that was generated in the previous step. This is the spec that you obtained by running your Python script, which produced the OpenAPI schema as output.
96+
```sh
97+
python app.py > schema.json
98+
```
8399

84-
### Enabling the Swagger UI
100+
### Creating your Agent on the AWS Console
85101

86-
--8<-- "docs/core/event_handler/_swagger_ui.md"
102+
To create an Agent for Bedrock, refer to the [official documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-create.html) provided by AWS.
87103

88-
```python hl_lines="10" title="enabling_swagger.py"
89-
--8<-- "examples/event_handler_bedrock_agents/src/enabling_swagger.py"
90-
```
104+
The following video demonstrates the end-to-end process:
91105

92-
1. `enable_swagger` creates a route to serve Swagger UI and allows quick customizations. <br><br> You can also include middlewares to protect or enhance the overall experience.
106+
During the creation process, you can use the schema generated in the previous step when prompted for an OpenAPI specification.
93107

94108
## Advanced
95109

96110
### Additional metadata
97111

98-
To enrich the view that Bedrock Agents has of your Lambda functions, we use a combination of Pydantic Models and [OpenAPI](https://www.openapis.org/){target="_blank"} type annotations to add constraints to your API's parameters.
112+
To enrich the view that Agents for Bedrock has of your Lambda functions,
113+
use a combination of [Pydantic Models](https://docs.pydantic.dev/latest/concepts/models/){target="_blank"} and [OpenAPI](https://www.openapis.org/){target="_blank"} type annotations to add constraints to your APIs parameters.
99114

100115
--8<-- "docs/core/event_handler/_openapi_customization_intro.md"
101116

@@ -113,26 +128,6 @@ To implement these customizations, include extra parameters when defining your r
113128
--8<-- "examples/event_handler_bedrock_agents/src/customizing_api_operations.py"
114129
```
115130

116-
#### Customizing Swagger UI
117-
118-
--8<-- "docs/core/event_handler/_openapi_customization_swagger.md"
119-
120-
Below is an example configuration for serving Swagger UI from a custom path or CDN, with assets like CSS and JavaScript loading from a chosen CDN base URL.
121-
122-
=== "customizing_swagger.py"
123-
124-
```python hl_lines="10"
125-
--8<-- "examples/event_handler_bedrock_agents/src/customizing_swagger.py"
126-
```
127-
128-
=== "customizing_swagger_middlewares.py"
129-
130-
A Middleware can handle tasks such as adding security headers, user authentication, or other request processing for serving the Swagger UI.
131-
132-
```python hl_lines="7 13-18 21"
133-
--8<-- "examples/event_handler_bedrock_agents/src/customizing_swagger_middlewares.py"
134-
```
135-
136131
#### Customizing OpenAPI metadata
137132

138133
--8<-- "docs/core/event_handler/_openapi_customization_metadata.md"
@@ -147,16 +142,18 @@ Include extra parameters when exporting your OpenAPI specification to apply thes
147142

148143
### Data validation
149144

150-
The Bedrock Agents Resolver allows for the clear definition of the expected format for incoming data and responses. By delegating data validation tasks to the Event Handler resolvers, you can significantly reduce the amount of repetitive code in your project.
145+
The Bedrock Agents Resolver allows for the clear definition of the expected format for incoming data and responses.
146+
By delegating data validation tasks to Powertools, you can significantly reduce the amount of repetitive code in your project.
151147

152-
For detailed guidance on implementing this feature, please consult the [REST API validation documentationi](api_gateway.md#data-validation). There, you'll find step-by-step instructions on how to apply data validation when using the resolver.
148+
For detailed guidance on implementing this feature, see the [REST API validation documentation](api_gateway.md#data-validation).
149+
There, you'll find step-by-step instructions on how to apply data validation when using the resolver.
153150

154151
???+ note
155152
When using the Bedrock Agent resolver, there's no need to add the `enable_validation` parameter, as it's enabled by default.
156153

157154
## Testing your code
158155

159-
You can test your routes by passing a proxy event request with required params.
156+
Test your routes by passing a Bedrock Agents proxy event request:
160157

161158
=== "assert_bedrock_agent_response.py"
162159

examples/event_handler_bedrock_agents/src/generating_openapi_schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ def lambda_handler(event: dict, context: LambdaContext):
2121
return app.resolve(event, context)
2222

2323

24-
if __name__ == "__main__":
25-
print(app.get_openapi_json_schema())
24+
if __name__ == "__main__": # (1)!
25+
print(app.get_openapi_json_schema()) # (2)!

examples/event_handler_bedrock_agents/src/getting_started.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ def current_time() -> int:
1818
@logger.inject_lambda_context
1919
@tracer.capture_lambda_handler
2020
def lambda_handler(event: dict, context: LambdaContext):
21-
return app.resolve(event, context)
21+
return app.resolve(event, context) # (2)!

0 commit comments

Comments
 (0)
0