Skip to content

Commit 9561173

Browse files
author
Michael Brewer
authored
Merge branch 'develop' into docs/1067
2 parents 07f4534 + 999cdff commit 9561173

File tree

5 files changed

+94
-37
lines changed

5 files changed

+94
-37
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44

55
This project follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) format for changes and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## 1.25.6 - 2022-04-01
8+
9+
* **logger:** clear_state regression on absent standard keys like level, location, timestamp ([#1088](https://github.com/awslabs/aws-lambda-powertools-python/issues/1088))
10+
711
## 1.25.5 - 2022-03-18
812

913
### Bug Fixes

aws_lambda_powertools/logging/formatter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def remove_keys(self, keys: Iterable[str]):
187187

188188
def clear_state(self):
189189
self.log_format = dict.fromkeys(self.log_record_order)
190+
self.log_format.update(**self._build_default_keys())
190191

191192
@staticmethod
192193
def _build_default_keys():

docs/index.md

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,26 @@ title: Homepage
33
description: AWS Lambda Powertools Python
44
---
55

6-
A suite of utilities for AWS Lambda functions to ease adopting best practices such as tracing, structured logging, custom metrics, and more.
6+
A suite of utilities for AWS Lambda functions to ease adopting best practices such as tracing, structured logging, custom metrics, idempotency, batching, and more.
77

88
???+ tip "Tip: Looking for a quick read through how the core features are used?"
99

1010
Check out [this detailed blog post](https://aws.amazon.com/blogs/opensource/simplifying-serverless-best-practices-with-lambda-powertools/) with a practical example.
1111

12-
## Tenets
13-
14-
Core utilities such as Tracing, Logging, Metrics, and Event Handler will be available across all Lambda Powertools runtimes. Additional utilities are subjective to each language ecosystem and customer demand.
15-
16-
* **AWS Lambda only**. We optimise for AWS Lambda function environments and supported runtimes only. Utilities might work with web frameworks and non-Lambda environments, though they are not officially supported.
17-
* **Eases the adoption of best practices**. The main priority of the utilities is to facilitate best practices adoption, as defined in the AWS Well-Architected Serverless Lens; all other functionality is optional.
18-
* **Keep it lean**. Additional dependencies are carefully considered for security and ease of maintenance, and prevent negatively impacting startup time.
19-
* **We strive for backwards compatibility**. New features and changes should keep backwards compatibility. If a breaking change cannot be avoided, the deprecation and migration process should be clearly defined.
20-
* **We work backwards from the community**. We aim to strike a balance of what would work best for 80% of customers. Emerging practices are considered and discussed via Requests for Comment (RFCs)
21-
* **Progressive**. Utilities are designed to be incrementally adoptable for customers at any stage of their Serverless journey. They follow language idioms and their community’s common practices.
2212

2313
## Install
2414

2515
Powertools is available in the following formats:
2616

27-
* **Lambda Layer**: [**arn:aws:lambda:{region}:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:**](#){: .copyMe}
17+
* **Lambda Layer**: [**arn:aws:lambda:{region}:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:**](#){: .copyMe}
2818
* **PyPi**: **`pip install aws-lambda-powertools`**
2919

20+
???+ hint "Support this project by using Lambda Layers :heart:"
21+
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.
22+
23+
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.
24+
25+
3026
### Lambda Layer
3127

3228
[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.
@@ -37,23 +33,28 @@ You can include Lambda Powertools Lambda Layer using [AWS Lambda Console](https:
3733

3834
| Region | Layer ARN
3935
|--------------------------- | ---------------------------
40-
| `us-east-1` | [arn:aws:lambda:us-east-1:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
41-
| `us-east-2` | [arn:aws:lambda:us-east-2:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
42-
| `us-west-1` | [arn:aws:lambda:us-west-1:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
43-
| `us-west-2` | [arn:aws:lambda:us-west-2:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
44-
| `ap-south-1` | [arn:aws:lambda:ap-south-1:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
45-
| `ap-northeast-1` | [arn:aws:lambda:ap-northeast-1:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
46-
| `ap-northeast-2` | [arn:aws:lambda:ap-northeast-2:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
47-
| `ap-northeast-3` | [arn:aws:lambda:ap-northeast-3:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
48-
| `ap-southeast-1` | [arn:aws:lambda:ap-southeast-1:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
49-
| `ap-southeast-2` | [arn:aws:lambda:ap-southeast-2:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
50-
| `eu-central-1` | [arn:aws:lambda:eu-central-1:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
51-
| `eu-west-1` | [arn:aws:lambda:eu-west-1:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
52-
| `eu-west-2` | [arn:aws:lambda:eu-west-2:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
53-
| `eu-west-3` | [arn:aws:lambda:eu-west-3:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
54-
| `eu-north-1` | [arn:aws:lambda:eu-north-1:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
55-
| `ca-central-1` | [arn:aws:lambda:ca-central-1:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
56-
| `sa-east-1` | [arn:aws:lambda:sa-east-1:017000801446:layer:AWSLambdaPowertoolsPython:15 :clipboard:](#){: .copyMe}
36+
| `us-east-1` | [arn:aws:lambda:us-east-1:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
37+
| `us-east-2` | [arn:aws:lambda:us-east-2:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
38+
| `us-west-1` | [arn:aws:lambda:us-west-1:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
39+
| `us-west-2` | [arn:aws:lambda:us-west-2:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
40+
| `ap-south-1` | [arn:aws:lambda:ap-south-1:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
41+
| `ap-northeast-1` | [arn:aws:lambda:ap-northeast-1:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
42+
| `ap-northeast-2` | [arn:aws:lambda:ap-northeast-2:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
43+
| `ap-northeast-3` | [arn:aws:lambda:ap-northeast-3:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
44+
| `ap-southeast-1` | [arn:aws:lambda:ap-southeast-1:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
45+
| `ap-southeast-2` | [arn:aws:lambda:ap-southeast-2:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
46+
| `eu-central-1` | [arn:aws:lambda:eu-central-1:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
47+
| `eu-west-1` | [arn:aws:lambda:eu-west-1:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
48+
| `eu-west-2` | [arn:aws:lambda:eu-west-2:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
49+
| `eu-west-3` | [arn:aws:lambda:eu-west-3:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
50+
| `eu-north-1` | [arn:aws:lambda:eu-north-1:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
51+
| `ca-central-1` | [arn:aws:lambda:ca-central-1:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
52+
| `sa-east-1` | [arn:aws:lambda:sa-east-1:017000801446:layer:AWSLambdaPowertoolsPython:16 :clipboard:](#){: .copyMe}
53+
54+
??? question "Can't find our Lambda Layer for your preferred AWS region?"
55+
You can use [Serverless Application Repository (SAR)](#sar) method, our [CDK Layer Construct](https://github.com/aws-samples/cdk-lambda-powertools-python-layer){target="_blank"}, or PyPi like you normally would for any other library.
56+
57+
Please do file a feature request with the region you'd want us to prioritize making our Lambda Layer available.
5758

5859
=== "SAM"
5960

@@ -62,7 +63,7 @@ You can include Lambda Powertools Lambda Layer using [AWS Lambda Console](https:
6263
Type: AWS::Serverless::Function
6364
Properties:
6465
Layers:
65-
- !Sub arn:aws:lambda:${AWS::Region}:017000801446:layer:AWSLambdaPowertoolsPython:15
66+
- !Sub arn:aws:lambda:${AWS::Region}:017000801446:layer:AWSLambdaPowertoolsPython:16
6667
```
6768

6869
=== "Serverless framework"
@@ -72,7 +73,7 @@ You can include Lambda Powertools Lambda Layer using [AWS Lambda Console](https:
7273
hello:
7374
handler: lambda_function.lambda_handler
7475
layers:
75-
- arn:aws:lambda:${aws:region}:017000801446:layer:AWSLambdaPowertoolsPython:15
76+
- arn:aws:lambda:${aws:region}:017000801446:layer:AWSLambdaPowertoolsPython:16
7677
```
7778

7879
=== "CDK"
@@ -88,7 +89,7 @@ You can include Lambda Powertools Lambda Layer using [AWS Lambda Console](https:
8889
powertools_layer = aws_lambda.LayerVersion.from_layer_version_arn(
8990
self,
9091
id="lambda-powertools",
91-
layer_version_arn=f"arn:aws:lambda:{env.region}:017000801446:layer:AWSLambdaPowertoolsPython:15"
92+
layer_version_arn=f"arn:aws:lambda:{env.region}:017000801446:layer:AWSLambdaPowertoolsPython:16"
9293
)
9394
aws_lambda.Function(self,
9495
'sample-app-lambda',
@@ -135,7 +136,7 @@ You can include Lambda Powertools Lambda Layer using [AWS Lambda Console](https:
135136
role = aws_iam_role.iam_for_lambda.arn
136137
handler = "index.test"
137138
runtime = "python3.9"
138-
layers = ["arn:aws:lambda:{region}:017000801446:layer:AWSLambdaPowertoolsPython:15"]
139+
layers = ["arn:aws:lambda:{region}:017000801446:layer:AWSLambdaPowertoolsPython:16"]
139140

140141
source_code_hash = filebase64sha256("lambda_function_payload.zip")
141142
}
@@ -152,7 +153,7 @@ You can include Lambda Powertools Lambda Layer using [AWS Lambda Console](https:
152153
? Do you want to configure advanced settings? Yes
153154
...
154155
? Do you want to enable Lambda layers for this function? Yes
155-
? Enter up to 5 existing Lambda layer ARNs (comma-separated): arn:aws:lambda:eu-central-1:017000801446:layer:AWSLambdaPowertoolsPython:15
156+
? Enter up to 5 existing Lambda layer ARNs (comma-separated): arn:aws:lambda:eu-central-1:017000801446:layer:AWSLambdaPowertoolsPython:16
156157
❯ amplify push -y
157158

158159

@@ -163,15 +164,15 @@ You can include Lambda Powertools Lambda Layer using [AWS Lambda Console](https:
163164
- Name: <NAME-OF-FUNCTION>
164165
? Which setting do you want to update? Lambda layers configuration
165166
? Do you want to enable Lambda layers for this function? Yes
166-
? Enter up to 5 existing Lambda layer ARNs (comma-separated): arn:aws:lambda:eu-central-1:017000801446:layer:AWSLambdaPowertoolsPython:15
167+
? Enter up to 5 existing Lambda layer ARNs (comma-separated): arn:aws:lambda:eu-central-1:017000801446:layer:AWSLambdaPowertoolsPython:16
167168
? Do you want to edit the local lambda function now? No
168169
```
169170

170171
=== "Get the Layer .zip contents"
171172
Change {region} to your AWS region, e.g. `eu-west-1`
172173

173174
```bash title="AWS CLI"
174-
aws lambda get-layer-version-by-arn --arn arn:aws:lambda:{region}:017000801446:layer:AWSLambdaPowertoolsPython:15 --region {region}
175+
aws lambda get-layer-version-by-arn --arn arn:aws:lambda:{region}:017000801446:layer:AWSLambdaPowertoolsPython:16 --region {region}
175176
```
176177

177178
The pre-signed URL to download this Lambda Layer will be within `Location` key.
@@ -407,6 +408,8 @@ sam init --location https://github.com/aws-samples/cookiecutter-aws-sam-python
407408

408409
## Features
409410

411+
Core utilities such as Tracing, Logging, Metrics, and Event Handler will be available across all Lambda Powertools languages. Additional utilities are subjective to each language ecosystem and customer demand.
412+
410413
| Utility | Description
411414
| ------------------------------------------------- | ---------------------------------------------------------------------------------
412415
[Tracing](./core/tracer.md) | Decorators and utilities to trace Lambda function handlers, and both synchronous and asynchronous functions
@@ -454,3 +457,14 @@ set_package_logger() # (1)
454457
```
455458

456459
1. :information_source: this will configure our `aws_lambda_powertools` logger with debug.
460+
461+
## Tenets
462+
463+
These are our core principles to guide our decision making.
464+
465+
* **AWS Lambda only**. We optimise for AWS Lambda function environments and supported runtimes only. Utilities might work with web frameworks and non-Lambda environments, though they are not officially supported.
466+
* **Eases the adoption of best practices**. The main priority of the utilities is to facilitate best practices adoption, as defined in the AWS Well-Architected Serverless Lens; all other functionality is optional.
467+
* **Keep it lean**. Additional dependencies are carefully considered for security and ease of maintenance, and prevent negatively impacting startup time.
468+
* **We strive for backwards compatibility**. New features and changes should keep backwards compatibility. If a breaking change cannot be avoided, the deprecation and migration process should be clearly defined.
469+
* **We work backwards from the community**. We aim to strike a balance of what would work best for 80% of customers. Emerging practices are considered and discussed via Requests for Comment (RFCs)
470+
* **Progressive**. Utilities are designed to be incrementally adoptable for customers at any stage of their Serverless journey. They follow language idioms and their community’s common practices.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "aws_lambda_powertools"
3-
version = "1.25.5"
3+
version = "1.25.6"
44
description = "A suite of utilities for AWS Lambda functions to ease adopting best practices such as tracing, structured logging, custom metrics, batching, idempotency, feature flags, and more."
55
authors = ["Amazon Web Services"]
66
include = ["aws_lambda_powertools/py.typed", "THIRD-PARTY-LICENSES"]

tests/functional/test_logger.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,44 @@ def handler(event, context):
663663
assert "my_key" not in second_log
664664

665665

666+
def test_clear_state_keeps_standard_keys(lambda_context, stdout, service_name):
667+
# GIVEN
668+
logger = Logger(service=service_name, stream=stdout)
669+
standard_keys = ["level", "location", "message", "timestamp", "service"]
670+
671+
# WHEN clear_state is set
672+
@logger.inject_lambda_context(clear_state=True)
673+
def handler(event, context):
674+
logger.info("Foo")
675+
676+
# THEN all standard keys should be available as usual
677+
handler({}, lambda_context)
678+
handler({}, lambda_context)
679+
680+
first_log, second_log = capture_multiple_logging_statements_output(stdout)
681+
for key in standard_keys:
682+
assert key in first_log
683+
assert key in second_log
684+
685+
686+
def test_clear_state_keeps_exception_keys(lambda_context, stdout, service_name):
687+
# GIVEN
688+
logger = Logger(service=service_name, stream=stdout)
689+
690+
# WHEN clear_state is set and an exception was logged
691+
@logger.inject_lambda_context(clear_state=True)
692+
def handler(event, context):
693+
try:
694+
raise ValueError("something went wrong")
695+
except Exception:
696+
logger.exception("Received an exception")
697+
698+
# THEN we expect a "exception_name" to be "ValueError"
699+
handler({}, lambda_context)
700+
log = capture_logging_output(stdout)
701+
assert "ValueError" == log["exception_name"]
702+
703+
666704
def test_inject_lambda_context_allows_handler_with_kwargs(lambda_context, stdout, service_name):
667705
# GIVEN
668706
logger = Logger(service=service_name, stream=stdout)

0 commit comments

Comments
 (0)