Skip to content

Commit 8ee6259

Browse files
heitorlessarubenfonseca
authored andcommitted
docs(v2): document optional dependencies and local dev (#1574)
1 parent bbc1c14 commit 8ee6259

File tree

5 files changed

+61
-74
lines changed

5 files changed

+61
-74
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<!-- markdownlint-disable MD043 MD041 -->
1+
<!-- markdownlint-disable MD013 MD041 MD043 -->
22
# AWS Lambda Powertools for Python
33

44
[![Build](https://github.com/awslabs/aws-lambda-powertools-python/actions/workflows/python_build.yml/badge.svg)](https://github.com/awslabs/aws-lambda-powertools-python/actions/workflows/python_build.yml)
55
[![codecov.io](https://codecov.io/github/awslabs/aws-lambda-powertools-python/branch/develop/graphs/badge.svg)](https://app.codecov.io/gh/awslabs/aws-lambda-powertools-python)
66
![PythonSupport](https://img.shields.io/static/v1?label=python&message=3.6%20|%203.7|%203.8|%203.9&color=blue?style=flat-square&logo=python) ![PyPI version](https://badge.fury.io/py/aws-lambda-powertools.svg) ![PyPi monthly downloads](https://img.shields.io/pypi/dm/aws-lambda-powertools) ![Lambda Layer](https://api.globadge.com/v1/badgen/aws/lambda/layer/latest-version/eu-central-1/017000801446/AWSLambdaPowertoolsPython)
77
[![Join our Discord](https://dcbadge.vercel.app/api/server/B8zZKbbyET)](https://discord.gg/B8zZKbbyET)
88

9-
A suite of Python utilities for AWS Lambda functions to ease adopting best practices such as tracing, structured logging, custom metrics, and more. (AWS Lambda Powertools [Java](https://github.com/awslabs/aws-lambda-powertools-java) and [Typescript](https://github.com/awslabs/aws-lambda-powertools-typescript) is also available).
9+
A suite of Python utilities for AWS Lambda functions to ease adopting best practices such as tracing, structured logging, custom metrics, and more. (AWS Lambda Powertools for [Java](https://github.com/awslabs/aws-lambda-powertools-java), [Typescript](https://github.com/awslabs/aws-lambda-powertools-typescript), and [.NET](https://awslabs.github.io/aws-lambda-powertools-dotnet/){target="_blank"} are also available).
1010

1111
**[📜Documentation](https://awslabs.github.io/aws-lambda-powertools-python/)** | **[🐍PyPi](https://pypi.org/project/aws-lambda-powertools/)** | **[Roadmap](https://awslabs.github.io/aws-lambda-powertools-python/latest/roadmap/)** | **[Detailed blog post](https://aws.amazon.com/blogs/opensource/simplifying-serverless-best-practices-with-lambda-powertools/)**
1212

docs/core/tracer.md

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ Tracer is an opinionated thin wrapper for [AWS X-Ray Python SDK](https://github.
1919
???+ tip
2020
All examples shared in this documentation are available within the [project repository](https://github.com/awslabs/aws-lambda-powertools-python/tree/develop/examples){target="_blank"}.
2121

22+
### Install
23+
24+
!!! info "This is not necessary if you're installing Powertools via [Lambda Layer](../index.md#lambda-layer){target="_blank"}"
25+
26+
Add `aws-lambda-powertools[tracer]` as a dependency in your preferred tool: _e.g._, _requirements.txt_, _pyproject.toml_.
27+
28+
This will ensure you have the required dependencies before using Tracer.
29+
2230
### Permissions
2331

2432
Before your use this utility, your AWS Lambda function [must have permissions](https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html#services-xray-permissions) to send traces to AWS X-Ray.

docs/index.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ description: AWS Lambda Powertools for Python
1111
A suite of utilities for AWS Lambda functions to ease adopting best practices such as tracing, structured logging, custom metrics, idempotency, batching, and more.
1212

1313
???+ note
14-
Lambda Powertools is also available for [Java](https://awslabs.github.io/aws-lambda-powertools-java/){target="_blank"} and [TypeScript](https://awslabs.github.io/aws-lambda-powertools-typescript/latest/){target="_blank"}.
14+
Powertools is also available for [Java](https://awslabs.github.io/aws-lambda-powertools-java/){target="_blank"}, [TypeScript](https://awslabs.github.io/aws-lambda-powertools-typescript/latest/){target="_blank"}, and [.NET](https://awslabs.github.io/aws-lambda-powertools-dotnet/){target="_blank"}
1515

1616
## Install
1717

@@ -20,11 +20,29 @@ Powertools is available in the following formats:
2020
* **Lambda Layer**: [**arn:aws:lambda:{region}:017000801446:layer:AWSLambdaPowertoolsPython:38**](#){: .copyMe}:clipboard:
2121
* **PyPi**: **`pip install aws-lambda-powertools`**
2222

23+
???+ info "Some utilities require additional dependencies"
24+
You can stop reading if you're using Lambda Layer.
25+
26+
[Tracer](./core/tracer.md){target="_blank"}, [Validation](./utilities/validation.md){target="_blank"} and [Parser](./utilities/parser.md){target="_blank"} require additional dependencies. If you prefer to install all of them, use `pip install aws-lambda-powertools[all]`.
27+
2328
???+ hint "Support this project by using Lambda Layers :heart:"
2429
Lambda Layers allow us to understand who uses this library in a non-intrusive way. This helps us justify and gain future investments for other Lambda Powertools languages.
2530

2631
When using Layers, you can add Lambda Powertools as a dev dependency (or as part of your virtual env) to not impact the development process.
2732

33+
### Local development
34+
35+
Powertools relies on the AWS SDK bundled in the Lambda runtime. This helps us achieve an optimal package size and initialization.
36+
37+
This means you need to add AWS SDK as a development dependency (not as a production dependency).
38+
39+
* **Pip**: `pip install aws-lambda-powertools[aws-sdk]`
40+
* **Poetry**: `poetry add aws-lambda-powertools[aws-sdk] --dev`
41+
* **Pipenv**: `pipenv install --dev "aws-lambda-powertools[aws-sdk]"`
42+
43+
???+ note "Local emulation"
44+
If you're running your code locally with [AWS SAM CLI](https://github.com/aws/aws-sam-cli){target="_blank"}, and not with your Python/IDE interpreter directly, this is not necessary. SAM CLI already brings the AWS SDK in its emulation image.
45+
2846
### Lambda Layer
2947

3048
[Lambda Layer](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html){target="_blank"} is a .zip file archive that can contain additional code, pre-packaged dependencies, data, or configuration files. Layers promote code sharing and separation of responsibilities so that you can iterate faster on writing business logic.

docs/utilities/parser.md

+24-71
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Parser
2+
title: Parser (Pydantic)
33
description: Utility
44
---
55
<!-- markdownlint-disable MD043 -->
@@ -12,20 +12,25 @@ This utility provides data parsing and deep validation using [Pydantic](https://
1212
* Built-in envelopes to unwrap, extend, and validate popular event sources payloads
1313
* Enforces type hints at runtime with user-friendly errors
1414

15-
**Extra dependency**
15+
## Getting started
1616

17-
???+ warning
17+
### Install
18+
19+
!!! info "This is not necessary if you're installing Powertools via [Lambda Layer](../index.md#lambda-layer){target="_blank"}"
20+
21+
Add `aws-lambda-powertools[parser]` as a dependency in your preferred tool: _e.g._, _requirements.txt_, _pyproject.toml_.
1822

23+
This will ensure you have the required dependencies before using Parser.
24+
25+
???+ warning
1926
This will increase the compressed package size by >10MB due to the Pydantic dependency.
2027

2128
To reduce the impact on the package size at the expense of 30%-50% of its performance [Pydantic can also be
2229
installed without binary files](https://pydantic-docs.helpmanual.io/install/#performance-vs-package-size-trade-off):
2330

24-
`SKIP_CYTHON=1 pip install --no-binary pydantic aws-lambda-powertools[pydantic]`
31+
Pip example: `SKIP_CYTHON=1 pip install --no-binary pydantic aws-lambda-powertools[parser]`
2532

26-
Install parser's extra dependencies using **`pip install aws-lambda-powertools[pydantic]`**.
27-
28-
## Defining models
33+
### Defining models
2934

3035
You can define models to parse incoming events by inheriting from `BaseModel`.
3136

@@ -47,11 +52,11 @@ class Order(BaseModel):
4752

4853
These are simply Python classes that inherit from BaseModel. **Parser** enforces type hints declared in your model at runtime.
4954

50-
## Parsing events
55+
### Parsing events
5156

5257
You can parse inbound events using **event_parser** decorator, or the standalone `parse` function. Both are also able to parse either dictionary or JSON string as an input.
5358

54-
### event_parser decorator
59+
#### event_parser decorator
5560

5661
Use the decorator for fail fast scenarios where you want your Lambda function to raise an exception in the event of a malformed payload.
5762

@@ -104,7 +109,7 @@ handler(event=payload, context=LambdaContext())
104109
handler(event=json.dumps(payload), context=LambdaContext()) # also works if event is a JSON string
105110
```
106111

107-
### parse function
112+
#### parse function
108113

109114
Use this standalone function when you want more control over the data validation process, for example returning a 400 error for malformed payloads.
110115

@@ -149,7 +154,7 @@ def my_function():
149154
}
150155
```
151156

152-
## Built-in models
157+
### Built-in models
153158

154159
Parser comes with the following built-in models:
155160

@@ -172,7 +177,7 @@ Parser comes with the following built-in models:
172177
| **KafkaSelfManagedEventModel** | Lambda Event Source payload for self managed Kafka payload |
173178
| **KafkaMskEventModel** | Lambda Event Source payload for AWS MSK payload |
174179

175-
### Extending built-in models
180+
#### Extending built-in models
176181

177182
You can extend them to include your own models, and yet have all other known fields parsed along the way.
178183

@@ -251,7 +256,7 @@ for order_item in ret.detail.items:
251256
--8<-- "examples/parser/src/extending_built_in_models_with_json_validator.py"
252257
```
253258

254-
## Envelopes
259+
### Envelopes
255260

256261
When trying to parse your payloads wrapped in a known structure, you might encounter the following situations:
257262

@@ -309,7 +314,7 @@ def handler(event: UserModel, context: LambdaContext):
309314
3. Parser parsed the original event against the EventBridge model
310315
4. Parser then parsed the `detail` key using `UserModel`
311316

312-
### Built-in envelopes
317+
#### Built-in envelopes
313318

314319
Parser comes with the following built-in envelopes, where `Model` in the return section is your given model.
315320

@@ -328,7 +333,7 @@ Parser comes with the following built-in envelopes, where `Model` in the return
328333
| **LambdaFunctionUrlEnvelope** | 1. Parses data using `LambdaFunctionUrlModel`. <br/> 2. Parses `body` key using your model and returns it. | `Model` |
329334
| **KafkaEnvelope** | 1. Parses data using `KafkaRecordModel`. <br/> 2. Parses `value` key using your model and returns it. | `Model` |
330335

331-
### Bringing your own envelope
336+
#### Bringing your own envelope
332337

333338
You can create your own Envelope model and logic by inheriting from `BaseEnvelope`, and implementing the `parse` method.
334339

@@ -393,7 +398,7 @@ Here's a snippet of how the EventBridge envelope we demonstrated previously is i
393398
3. Then, we parsed the incoming data with our envelope to confirm it matches EventBridge's structure defined in `EventBridgeModel`
394399
4. Lastly, we call `_parse` from `BaseEnvelope` to parse the data in our envelope (.detail) using the customer model
395400

396-
## Data model validation
401+
### Data model validation
397402

398403
???+ warning
399404
This is radically different from the **Validator utility** which validates events against JSON Schema.
@@ -410,7 +415,7 @@ Keep the following in mind regardless of which decorator you end up using it:
410415
* You must raise either `ValueError`, `TypeError`, or `AssertionError` when value is not compliant
411416
* You must return the value(s) itself if compliant
412417

413-
### validating fields
418+
#### validating fields
414419

415420
Quick validation to verify whether the field `message` has the value of `hello world`.
416421

@@ -455,7 +460,7 @@ class HelloWorldModel(BaseModel):
455460
parse(model=HelloWorldModel, event={"message": "hello universe", "sender": "universe"})
456461
```
457462

458-
### validating entire model
463+
#### validating entire model
459464

460465
`root_validator` can help when you have a complex validation mechanism. For example finding whether data has been omitted, comparing field values, etc.
461466

@@ -486,7 +491,7 @@ parse(model=UserModel, event=payload)
486491
???+ info
487492
You can read more about validating list items, reusing validators, validating raw inputs, and a lot more in <a href="https://pydantic-docs.helpmanual.io/usage/validators/">Pydantic's documentation</a>.
488493

489-
## Advanced use cases
494+
### Advanced use cases
490495

491496
???+ tip "Tip: Looking to auto-generate models from JSON, YAML, JSON Schemas, OpenApi, etc?"
492497
Use Koudai Aono's [data model code generation tool for Pydantic](https://github.com/koxudaxi/datamodel-code-generator)
@@ -551,55 +556,3 @@ If what you're trying to use isn't available as part of the high level import sy
551556
```python title="Pydantic import escape hatch"
552557
from aws_lambda_powertools.utilities.parser.pydantic import <what you'd like to import'>
553558
```
554-
555-
**What is the cold start impact in bringing this additional dependency?**
556-
557-
No significant cold start impact. It does increase the final uncompressed package by **71M**, when you bring the additional dependency that parser requires.
558-
559-
Artillery load test sample against a [hello world sample](https://github.com/aws-samples/cookiecutter-aws-sam-python) using Tracer, Metrics, and Logger with and without parser.
560-
561-
**No parser**
562-
563-
???+ info
564-
**Uncompressed package size**: 55M, **p99**: 180.3ms
565-
566-
```javascript
567-
Summary report @ 14:36:07(+0200) 2020-10-23
568-
Scenarios launched: 10
569-
Scenarios completed: 10
570-
Requests completed: 2000
571-
Mean response/sec: 114.81
572-
Response time (msec):
573-
min: 54.9
574-
max: 1684.9
575-
median: 68
576-
p95: 109.1
577-
p99: 180.3
578-
Scenario counts:
579-
0: 10 (100%)
580-
Codes:
581-
200: 2000
582-
```
583-
584-
**With parser**
585-
586-
???+ info
587-
**Uncompressed package size**: 128M, **p99**: 193.1ms
588-
589-
```javascript
590-
Summary report @ 14:29:23(+0200) 2020-10-23
591-
Scenarios launched: 10
592-
Scenarios completed: 10
593-
Requests completed: 2000
594-
Mean response/sec: 111.67
595-
Response time (msec):
596-
min: 54.3
597-
max: 1887.2
598-
median: 66.1
599-
p95: 113.3
600-
p99: 193.1
601-
Scenario counts:
602-
0: 10 (100%)
603-
Codes:
604-
200: 2000
605-
```

docs/utilities/validation.md

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ This utility provides JSON Schema validation for events and responses, including
1818
???+ tip
1919
All examples shared in this documentation are available within the [project repository](https://github.com/awslabs/aws-lambda-powertools-python/tree/develop/examples){target="_blank"}.
2020

21+
### Install
22+
23+
!!! info "This is not necessary if you're installing Powertools via [Lambda Layer](../index.md#lambda-layer){target="_blank"}"
24+
25+
Add `aws-lambda-powertools[validation]` as a dependency in your preferred tool: _e.g._, _requirements.txt_, _pyproject.toml_.
26+
27+
This will ensure you have the required dependencies before using Validation.
28+
2129
You can validate inbound and outbound events using [`validator` decorator](#validator-decorator).
2230

2331
You can also use the standalone `validate` function, if you want more control over the validation process such as handling a validation error.

0 commit comments

Comments
 (0)