You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/quickstart.md
+20-17Lines changed: 20 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,6 +99,8 @@ As a result API endpoint will be exposed for you. You can trigger it with the 'c
99
99
* Choose different command line tool like `HTTPie`.
100
100
* Type the url in the browser directly
101
101
* Use REST API client like `Postman` or `SoupUI`.
102
+
!!! warning
103
+
**Powertools Tracer** requires X-RAY service to work. This means that you will not see the traces locally. Roll it out on your AWS account instead.
102
104
#### Remote test
103
105
You may also deploy your application into AWS Account by issuing the following command.
104
106
```bash
@@ -131,8 +133,9 @@ This command builds a package and deploy it to your AWS Account. You find the AP
131
133
For more details on the SAM deployment mechanism, see [link](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-deploy.html).
132
134
133
135
## API Gateway router
134
-
Let's say we want to have different method that acts like an echo server. It takes user input (username) and output it to the caller. We need to create an API with an URL path `/hello/<name>`, where the `name` string is the input from the user.
135
-
One approach is to create another lambda with required method and set up the API gateway to call it.
136
+
Let's expand our application with a new method. It takes an username as a input and return it in the response.
137
+
138
+
We decided to write another Lambda including required method. Next, we configure our API Gateway to expose this Lambda under a new unique path `/hello/{name}`.
136
139
=== "app_name.py"
137
140
138
141
```python
@@ -190,10 +193,10 @@ One approach is to create another lambda with required method and set up the API
190
193
191
194
This way certainly works for simple use case. But what happens if your application gets bigger and we need to cover numerous URL paths and HTTP methods for them? If that is the case, we should:
192
195
193
-
* Add a new lambda handler with business logic for each new URL path and HTTP method used.
194
-
* Add a new Lambda configuration to a SAM template file to map the lambda function to the required path and HTTP URL method.
196
+
* Add a new Lambda handler with business logic for each new URL path and HTTP method used.
197
+
* Add a new Lambda configuration to a SAM template file to map the Lambda function to the required path and HTTP URL method.
195
198
196
-
This could result in a number of alike lambda files and large SAM configuration file with similar configuration sections.
199
+
This could result in a number of alike Lambda files and large SAM configuration file with similar configuration sections.
197
200
In this case if we see that the addition of new URL paths lead to the boilerplate code, we should lean towards the routing approach.
198
201
!!! Info
199
202
If you want a more detailed explanation of these two approaches, we have explained the considerations [here](.. /core/event_handler/api_gateway/#considerations)
@@ -277,10 +280,10 @@ The simple code might look similar to the following code snippet.
277
280
* We add two methods: `hello_name` and `hello` (line 4,9).
278
281
* We add the `Router` class which allows us to record the method that should be called when the specific request arrives (line 13).
279
282
* We create the instance and added the configuration with the mapping of the processing methods and the http query method (line 29-31).
280
-
* In the lambda handler, we call router instance `get` method to retrieve a reference to the processing method (`hello` or `hello_name`) that will process the query (line 37).
283
+
* In the Lambda handler, we call router instance `get` method to retrieve a reference to the processing method (`hello` or `hello_name`) that will process the query (line 37).
281
284
* Finally, we run this method and send the results back to API Gateway (line 38).
282
285
283
-
This approach simplifies the configuration of our infrastructure since we have added all API Gateway paths in the `HelloWorldFunction` event section. We need to understand the internal structure of the API Gateway request events, to deduce the requested path, http method and path parameters. This puts additional engineering effort to provide proper error handling. Also, if we decide to use another event source for our lambda, since we are highly coupled it requires rewriting of our lambda handler to get the information we need.
286
+
This approach simplifies the configuration of our infrastructure since we have added all API Gateway paths in the `HelloWorldFunction` event section. We need to understand the internal structure of the API Gateway request events, to deduce the requested path, http method and path parameters. This puts additional engineering effort to provide proper error handling. Also, if we decide to use another event source for our Lambda, since we are highly coupled it requires rewriting of our Lambda handler to get the information we need.
284
287
Let's see how we can improve it with Powertools.
285
288
286
289
=== "app.py"
@@ -314,12 +317,12 @@ Let's see how we can improve it with Powertools.
314
317
315
318
Powertools provides an `ApiGatewayResolver` class, which helps understand the structure, no need to look it up.
316
319
We can also directly use the parameters passed in the request now, because we have added the route annotation as the decorator for our methods.
317
-
For SAM to build our package correctly, we have specified lambda powertools package in our `requirement.txt` file.
320
+
For SAM to build our package correctly, we have specified Lambda powertools package in our `requirement.txt` file.
318
321
319
322
!!! tip
320
323
If you'd like to learn how python decorators work under the hood, you can follow [Real Python](https://realpython.com/primer-on-python-decorators/)'s article.
321
324
## Structured Logging
322
-
In the next step, you decided to propose production quality logging capabilities to your lambda code.
325
+
In the next step, you decided to propose production quality logging capabilities to your Lambda code.
323
326
We want our log event to be in a JSON format. Also, You follow [structured logging approach](https://docs.aws.amazon.com/lambda/latest/operatorguide/parse-logs.html). In a result, we expect easy to search, consistent logs containing enough context and data to analyse the status of our system. We can take advantage of CloudWatch Logs and Cloudwatch Insight for this purpose.
324
327
325
328
The first option could be to use a python logger in combination with the `pythonjsonlogger` library for simple structured logging.
@@ -383,7 +386,7 @@ instead of
383
386
```
384
387
385
388
So far, so good! To make things easier, we want to add extra context to the logs.
386
-
We can extract it from a lambda context or an event passed to lambda handler at the time of invocation. We add those specific attributes wherever a logger is used. Can we ensure that the required attributes are added automatically on our behalf without having to move them around? Yes! Powertools Logger to the rescue :-)
389
+
We can extract it from a Lambda context or an event passed to Lambda handler at the time of invocation. We add those specific attributes wherever a logger is used. Can we ensure that the required attributes are added automatically on our behalf without having to move them around? Yes! Powertools Logger to the rescue :-)
387
390
=== "app.py"
388
391
389
392
```python hl_lines="3 7 14 20 24"
@@ -416,7 +419,7 @@ We can extract it from a lambda context or an event passed to lambda handler at
416
419
```
417
420
418
421
We add powertools logger (line 8) and all the configuration is done.
419
-
We also use `logger.inject_lambda_context` decorator to inject lambda context into every log. We instruct logger to log correlation id taken from API Gateway and event automatically. Because powertools library adds a correlation identifier to each log, we can easily correlate all the logs generated for a specific request.
422
+
We also use `logger.inject_lambda_context` decorator to inject Lambda context into every log. We instruct logger to log correlation id taken from API Gateway and event automatically. Because powertools library adds a correlation identifier to each log, we can easily correlate all the logs generated for a specific request.
420
423
421
424
In result, we should see logs with following attributes.
422
425
=== "Example Application Structured Log"
@@ -530,9 +533,9 @@ A lot happens here. First, we import required X-ray SDK classes.
530
533
`xray_recorder` is a global AWS X-ray recorder class instance that starts/ends segments/sub-segments and sends them to the X-ray daemon.
531
534
To build new sub-segments, we use `xray_recorder.in_subsegment` method as a context manager.
532
535
We can add visible granular sub-traces into our X-ray by using customized sub-segments.
533
-
Also, we track lambda cold start by setting global variable outside of a handler. The variable is defined only upon lambda initialization. This information provides an overview of how often the runtime is reused by lambda invoked, which directly impacts lambda performance and latency.
536
+
Also, we track Lambda cold start by setting global variable outside of a handler. The variable is defined only upon Lambda initialization. This information provides an overview of how often the runtime is reused by Lambda invoked, which directly impacts Lambda performance and latency.
534
537
535
-
To allow the tracking of our Lambda, we need to set it up in our SAM template and add `Tracing: Active` under lambda`Properties` section.
538
+
To allow the tracking of our Lambda, we need to set it up in our SAM template and add `Tracing: Active` under Lambda`Properties` section.
536
539
!!! Info
537
540
Want to know more about context managers and understand the benefits of using them? Follow [article](https://realpython.com/python-with-statement/) from Real Python.
538
541
!!! Info
@@ -580,10 +583,10 @@ With powertools tracer we have much cleaner code right now.
580
583
To make our methods visible in the traces, we add `@tracer.capture_method` decorator to the processing methods.
581
584
We add annotations directly in the code without adding it with the context handler using the `tracer.put_annotation` method.
582
585
Since we add the `@tracer.capture_lambda_handler` decorator for our `lambda_handler`, powertools automatically adds cold start information as an annotation.
583
-
It also automatically append lambda response as a metadata into trace, so we don't need to worry about it as well.
586
+
It also automatically append Lambda response as a metadata into trace, so we don't need to worry about it as well.
584
587
!!! tip
585
588
For differences between annotations and metadata in traces, please follow [link](https://awslabs.github.io/aws-lambda-powertools-python/latest/core/tracer/#annotations-metadata).
586
-
Therefore, you should see traces of your lambda in the X-ray console.
589
+
Therefore, you should see traces of your Lambda in the X-ray console.
@@ -699,9 +702,9 @@ Let's expand our application with custom metrics without Powertools to see how i
699
702
700
703
```
701
704
702
-
To add custom metric in **CloudWatch** we add the `boto3` cloudwatch client. Next, we create the new `put_metric_data` method that uses this client to put the metric in CloudWatch synchronously. We call it in our method `hello` and `hello_name`. We divide our metrics by type of application and method. Thus, we can follow the frequency at which specific methods are called. We also need to add additional inline policy allowing our lambda to write metrics in the CloudWatch. In `template.yaml` we add `CloudWatchPutMetricPolicy` policy.
705
+
To add custom metric in **CloudWatch** we add the `boto3` cloudwatch client. Next, we create the new `put_metric_data` method that uses this client to put the metric in CloudWatch synchronously. We call it in our method `hello` and `hello_name`. We divide our metrics by type of application and method. Thus, we can follow the frequency at which specific methods are called. We also need to add additional inline policy allowing our Lambda to write metrics in the CloudWatch. In `template.yaml` we add `CloudWatchPutMetricPolicy` policy.
703
706
!!! Info
704
-
We use direct synchronous call to CloudWatch Metrics API. It will be visible in your AWS X-RAY traces as additional external call. Given your architecture scale, this approach might lead to disadvantages such as increased cost of measuring data collection and increased lambda latency.
707
+
We use direct synchronous call to CloudWatch Metrics API. It will be visible in your AWS X-RAY traces as additional external call. Given your architecture scale, this approach might lead to disadvantages such as increased cost of measuring data collection and increased Lambda latency.
0 commit comments