Skip to content

Commit f24e265

Browse files
committed
docs(quickstart): tidy requirements up
1 parent 7371c68 commit f24e265

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

docs/quickstart.md

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ description: Powertools introduction
55
Quickstart introducing core Powertools functionalities.
66

77
## Requirements
8-
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) installed.
9-
* [SAM](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) installed.
10-
* [AWS Credentials to your AWS Account](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started-set-up-credentials.html) configured.
118

12-
## Getting started
9+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html){target="_blank"} and [configured with your credentials](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started-set-up-credentials.html){target="_blank"}.
10+
* [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html){target="_blank"} installed.
11+
12+
## Getting started
1313
Let's clone our sample project before we add one feature at a time.
1414

1515
???+ tip "Tip: Want to skip to the final project?"
@@ -62,7 +62,7 @@ Let's configure our base application to look like the following code snippet.
6262
- x86_64
6363
Events:
6464
HelloWorld:
65-
Type: Api
65+
Type: Api
6666
Properties:
6767
Path: /hello
6868
Method: get
@@ -71,11 +71,11 @@ Let's configure our base application to look like the following code snippet.
7171
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
7272
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
7373
```
74-
Our Lambda code consists of an entry point function named `lambda_handler`, and a `hello` function.
74+
Our Lambda code consists of an entry point function named `lambda_handler`, and a `hello` function.
7575

7676
When API Gateway receives a request, Lambda will call our `lambda_handler` function, subsequently calling the `hello` function. API Gateway will use this response to return the correct HTTP Status Code and payload back to the caller.
7777
The SAM model configures API Gateway, which redirects traffic to Lambda for one path only: `hello`.
78-
!!! Warning
78+
!!! Warning
7979
For simplicity, we do not set up authentication and authorization in the example! Feel free to [implement](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-controlling-access-to-apis.html) it on your own.
8080
### Run your code
8181
At each point, you have two ways to run your code. Locally and within your AWS account. Given that we use SAM, the two methods are just as simple.
@@ -108,23 +108,23 @@ You may also deploy your application into AWS Account by issuing the following c
108108
...
109109
CloudFormation outputs from deployed stack
110110
------------------------------------------------------------------------------------------------------------------------------------------
111-
Outputs
111+
Outputs
112112
------------------------------------------------------------------------------------------------------------------------------------------
113-
Key HelloWorldFunctionIamRole
114-
Description Implicit IAM Role created for Hello World function
115-
Value arn:aws:iam::123456789012:role/sam-app-HelloWorldFunctionRole-1T2W3H9LZHGGV
113+
Key HelloWorldFunctionIamRole
114+
Description Implicit IAM Role created for Hello World function
115+
Value arn:aws:iam::123456789012:role/sam-app-HelloWorldFunctionRole-1T2W3H9LZHGGV
116116

117-
Key HelloWorldApi
118-
Description API Gateway endpoint URL for Prod stage for Hello World function
119-
Value https://1234567890.execute-api.eu-central-1.amazonaws.com/Prod/hello/
117+
Key HelloWorldApi
118+
Description API Gateway endpoint URL for Prod stage for Hello World function
119+
Value https://1234567890.execute-api.eu-central-1.amazonaws.com/Prod/hello/
120120

121-
Key HelloWorldFunction
122-
Description Hello World Lambda Function ARN
123-
Value arn:aws:lambda:eu-central-1:123456789012:function:sam-app-HelloWorldFunction-dOcfAtYoEiGo
121+
Key HelloWorldFunction
122+
Description Hello World Lambda Function ARN
123+
Value arn:aws:lambda:eu-central-1:123456789012:function:sam-app-HelloWorldFunction-dOcfAtYoEiGo
124124
------------------------------------------------------------------------------------------------------------------------------------------
125125
Successfully created/updated stack - sam-app in eu-central-1
126126
```
127-
This command builds a package and deploy it to your AWS Account. You find the API Gateway URL path against which you can launch requests in the output section.
127+
This command builds a package and deploy it to your AWS Account. You find the API Gateway URL path against which you can launch requests in the output section.
128128
Now, you can trigger your endpoints.
129129
```bash
130130
> curl https://1234567890.execute-api.eu-central-1.amazonaws.com/Prod/hello
@@ -170,7 +170,7 @@ We decided to write another Lambda including required method. Next, we configure
170170
Runtime: python3.9
171171
Events:
172172
HelloWorld:
173-
Type: Api
173+
Type: Api
174174
Properties:
175175
Path: /hello
176176
Method: get
@@ -197,11 +197,11 @@ This way certainly works for simple use case. But what happens if your applicati
197197
* Add a new Lambda handler with business logic for each new URL path and HTTP method used.
198198
* Add a new Lambda configuration to a SAM template file to map the Lambda function to the required path and HTTP URL method.
199199

200-
This could result in a number of alike Lambda files and large SAM configuration file with similar configuration sections.
200+
This could result in a number of alike Lambda files and large SAM configuration file with similar configuration sections.
201201
if we see that the addition of new URL paths lead to the boilerplate code, we should lean towards the routing approach.
202202
!!! Info
203203
If you want a more detailed explanation of these two approaches, we have explained the considerations [here](.. /core/event_handler/api_gateway/#considerations)
204-
204+
205205
The simple code might look similar to the following code snippet.
206206
=== "app.py"
207207

@@ -269,7 +269,7 @@ The simple code might look similar to the following code snippet.
269269
Path: /hello
270270
Method: get
271271
HelloWorldName:
272-
Type: Api
272+
Type: Api
273273
Properties:
274274
Path: /hello/{name}
275275
Method: get
@@ -279,10 +279,10 @@ The simple code might look similar to the following code snippet.
279279
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
280280
```
281281

282-
* We add two methods: `hello_name` and `hello` (line 4,9).
283-
* We add the `Router` class which allows us to record the method that should be called when the specific request arrives (line 13).
284-
* We create the instance and added the configuration with the mapping of the processing methods and the http query method (line 29-31).
285-
* In the Lambda handler, we call router instance `get` method to retrieve a reference to the processing method (`hello` or `hello_name`).(line 37).
282+
* We add two methods: `hello_name` and `hello` (line 4,9).
283+
* We add the `Router` class which allows us to record the method that should be called when the specific request arrives (line 13).
284+
* We create the instance and added the configuration with the mapping of the processing methods and the http query method (line 29-31).
285+
* In the Lambda handler, we call router instance `get` method to retrieve a reference to the processing method (`hello` or `hello_name`).(line 37).
286286
* Finally, we run this method and send the results back to API Gateway (line 38).
287287

288288
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.
@@ -321,7 +321,7 @@ Let's see how we can improve it with Powertools.
321321
Powertools provides an `ApiGatewayResolver` class, which helps understand the structure, no need to look it up.
322322

323323
We have added the route annotation as the decorator for our methods. It enables us to use the parameters passed in the request directly.
324-
We have also specified Lambda Powertools package in our `requirement.txt` file to ensure SAM is able to build our Lambda.
324+
We have also specified Lambda Powertools package in our `requirement.txt` file to ensure SAM is able to build our Lambda.
325325

326326
!!! tip
327327
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.
@@ -389,8 +389,8 @@ instead of
389389
[INFO] 2021-11-22T15:32:02.145Z ba3bea3d-fe3a-45db-a2ce-72e813d55b91 Request from unknown received
390390
```
391391

392-
So far, so good! To make things easier, we want to add extra context to the logs.
393-
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.
392+
So far, so good! To make things easier, we want to add extra context to the logs.
393+
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.
394394

395395
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 :-)
396396
=== "app.py"
@@ -424,7 +424,7 @@ Can we ensure that the required attributes are added automatically on our behalf
424424
return app.resolve(event, context)
425425
```
426426

427-
We add powertools logger (line 8) and all the configuration is done.
427+
We add powertools logger (line 8) and all the configuration is done.
428428
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.
429429

430430
In result, we should see logs with following attributes.
@@ -449,7 +449,7 @@ By having structured logs like this, we can easily search and analyse them in [C
449449
=== "CloudWatch Logs Insight Example"
450450
![CloudWatch Logs Insight Example](./media/cloudwatch_logs_insight_example.png)
451451
## Tracing
452-
The next improvement is to add an appropriate tracking mechanism to your stack. Developers want to analyze traces of queries that pass via the API gateway to your Lambda.
452+
The next improvement is to add an appropriate tracking mechanism to your stack. Developers want to analyze traces of queries that pass via the API gateway to your Lambda.
453453
With structured logs, it is an important step to provide the observability of your application!
454454
The AWS service that has these capabilities is [AWS X-RAY](https://aws.amazon.com/xray/). How do we send application trace to the AWS X-RAY service then?
455455

@@ -478,7 +478,7 @@ Let's first explore how we can achieve this with [x-ray SDK](https://docs.aws.am
478478
def hello_name(name):
479479
with xray_recorder.in_subsegment("hello_name") as subsegment:
480480
subsegment.put_annotation("User", name)
481-
logger.info(f"Request from {name} received")
481+
logger.info(f"Request from {name} received")
482482
return {"statusCode": 200, "body": json.dumps({"message": f"hello {name}!"})}
483483

484484

@@ -526,7 +526,7 @@ Let's first explore how we can achieve this with [x-ray SDK](https://docs.aws.am
526526
Path: /hello
527527
Method: get
528528
HelloWorldName:
529-
Type: Api
529+
Type: Api
530530
Properties:
531531
Path: /hello/{name}
532532
Method: get
@@ -537,11 +537,11 @@ Let's first explore how we can achieve this with [x-ray SDK](https://docs.aws.am
537537
```
538538

539539
* First, we import required X-ray SDK classes. `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.
540-
* To build new sub-segments, we use `xray_recorder.in_subsegment` method as a context manager.
540+
* To build new sub-segments, we use `xray_recorder.in_subsegment` method as a context manager.
541541
* 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.
542542

543543
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.
544-
!!! Info
544+
!!! Info
545545
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.
546546
!!! Info
547547
If you want to understand how the Lambda execution environment works and why cold starts can occur, follow [blog series](https://aws.amazon.com/blogs/compute/operating-lambda-performance-optimization-part-1/).
@@ -583,31 +583,31 @@ Now, let's try to simplify it with Lambda Powertools:
583583
def lambda_handler(event, context):
584584
return app.resolve(event, context)
585585
```
586-
587-
With powertools tracer we have much cleaner code right now.
588586

589-
To make our methods visible in the traces, we add `@tracer.capture_method` decorator to the processing methods.
590-
We add annotations directly in the code without adding it with the context handler using the `tracer.put_annotation` method.
591-
Since we add the `@tracer.capture_lambda_handler` decorator for our `lambda_handler`, powertools automatically adds cold start information as an annotation.
587+
With powertools tracer we have much cleaner code right now.
588+
589+
To make our methods visible in the traces, we add `@tracer.capture_method` decorator to the processing methods.
590+
We add annotations directly in the code without adding it with the context handler using the `tracer.put_annotation` method.
591+
Since we add the `@tracer.capture_lambda_handler` decorator for our `lambda_handler`, powertools automatically adds cold start information as an annotation.
592592
It also automatically append Lambda response as a metadata into trace, so we don't need to worry about it.
593-
!!! tip
594-
For differences between annotations and metadata in traces, please follow [link](https://awslabs.github.io/aws-lambda-powertools-python/latest/core/tracer/#annotations-metadata).
593+
!!! tip
594+
For differences between annotations and metadata in traces, please follow [link](https://awslabs.github.io/aws-lambda-powertools-python/latest/core/tracer/#annotations-metadata).
595595

596596
Therefore, you should see traces of your Lambda in the X-ray console.
597597
=== "Example X-RAY Console View"
598598
![Tracer utility](./media/tracer_utility_showcase_2.png)
599599

600600
You may consider using **CloudWatch ServiceLens** which links the CloudWatch metrics and logs, in addition to traces from the AWS X-Ray.
601601

602-
It gives you a complete view of your apps and their dependencies, making your services more observable.
602+
It gives you a complete view of your apps and their dependencies, making your services more observable.
603603
From here, you can browse to specific logs in CloudWatch Logs Insight, Metrics Dashboard or Traces in CloudWatch X-Ray traces.
604604
=== "Example CloudWatch ServiceLens View"
605605
![CloudWatch ServiceLens View](./media/tracer_utility_showcase_3.png)
606606
!!! Info
607607
For more information on CloudWatch ServiceLens, please visit [link](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/ServiceLens.html).
608608
## Custom Metrics
609-
The final step to provide complete observability is to add business metrics (such as number of sales or reservations).
610-
Lambda adds technical metrics (such as Invocations, Duration, Error Count & Success Rate) to the CloudWatch metrics out of the box.
609+
The final step to provide complete observability is to add business metrics (such as number of sales or reservations).
610+
Lambda adds technical metrics (such as Invocations, Duration, Error Count & Success Rate) to the CloudWatch metrics out of the box.
611611

612612
Let's expand our application with custom metrics without Powertools to see how it works, then let's upgrade it with Powertools:-)
613613

0 commit comments

Comments
 (0)