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
+38-26Lines changed: 38 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,27 +8,31 @@ This quickstart progressively introduces Lambda Powertools core utilities by usi
8
8
## Requirements
9
9
10
10
*[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"}.
*[AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html){target="_blank"} installed.
12
12
13
13
## Getting started
14
+
14
15
Let's clone our sample project before we add one feature at a time.
15
16
16
17
???+ tip "Tip: Want to skip to the final project?"
17
18
Bootstrap directly via SAM CLI: `sam init --location https://github.com/aws-samples/cookiecutter-aws-sam-python`
18
-
=== "shell"
19
-
```bash
19
+
20
+
```bash title="Use SAM CLI to initialize the sample project"
20
21
sam init --runtime python3.9 --dependency-manager pip --app-template hello-world --name powertools-quickstart
21
22
```
22
23
23
24
### Project structure
25
+
24
26
As we move forward, we will modify the following files within the `powertools-quickstart` folder:
25
27
26
28
***app.py** - Application code.
27
29
***template.yaml** - AWS infrastructure configuration using SAM.
28
30
***requirements.txt** - List of extra Python packages needed.
29
31
30
32
### Code example
33
+
31
34
Let's configure our base application to look like the following code snippet.
35
+
32
36
=== "app.py"
33
37
34
38
```python
@@ -74,37 +78,40 @@ Let's configure our base application to look like the following code snippet.
74
78
```
75
79
Our Lambda code consists of an entry point function named `lambda_handler`, and a `hello` function.
76
80
77
-
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.
78
-
The SAM model configures API Gateway, which redirects traffic to Lambda for one path only: `hello`.
81
+
When API Gateway receives a HTTP GET request on `/hello` route, 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.
82
+
79
83
!!! Warning
80
-
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.
84
+
For simplicity, we do not set up authentication and authorization! You can find more information on how to implement it on [AWS SAM documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-controlling-access-to-apis.html){target="_blank"}.
81
85
### Run your code
82
-
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.
86
+
87
+
At each point, you have two ways to run your code: locally and within your AWS account.
88
+
83
89
#### Local test
84
-
SAM allows you to execute a serverless application locally. Perform the next command in a shell.
85
-
```bash
90
+
91
+
AWS SAM allows you to execute a serverless application locally by running `sam build && sam local start-api` in your preferred shell.
92
+
93
+
```bash title="Build and run API Gateway locally"
86
94
> sam build && sam local start-api
87
95
...
88
96
2021-11-26 17:43:08 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
89
97
```
90
-
As a result API endpoint will be exposed for you. You can trigger it with the `curl` command like in the following example.
91
-
```bash
98
+
99
+
As a result, a local API endpoint will be exposed and you can invoke it using your browser, or your preferred HTTP API client e.g., [Postman](https://www.postman.com/downloads/){target="_blank"}, [httpie](https://httpie.io/){target="_blank"}, etc.
100
+
101
+
```bash title="Invoking our function locally via curl"
92
102
> curl http://127.0.0.1:3000/hello
93
103
{"message": "hello unknown!"}
94
104
```
105
+
95
106
!!! info
96
-
To learn more about local testing, please visit [SAM local testing](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-local-start-api.html) page
97
-
???+ tip "Tip: Want to use different tool for API testing?"
98
-
Instead of `curl` you have many other options to choose from.
107
+
To learn more about local testing, please visit the [AWS SAM CLI local testing](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-local-start-api.html) documentation.
99
108
100
-
* Choose different command line tool like `HTTPie`.
101
-
* Type the url in the browser directly
102
-
* Use REST API client like `Postman` or `SoupUI`.
103
-
!!! warning
104
-
**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.
105
-
#### Remote test
106
-
You may also deploy your application into AWS Account by issuing the following command.
107
-
```bash
109
+
110
+
#### Live test
111
+
112
+
First, you need to deploy your application into your AWS Account by issuing `sam build && sam deploy --guided` command. This command builds a ZIP package of your source code, and deploy it to your AWS Account.
113
+
114
+
```bash title="Build and deploy your serverless application"
108
115
> sam build && sam deploy --guided
109
116
...
110
117
CloudFormation outputs from deployed stack
@@ -125,14 +132,15 @@ Value arn:aws:lambda:eu-central-1:123456789012:function:sam-app-He
Successfully created/updated stack - sam-app in eu-central-1
127
134
```
128
-
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.
129
-
Now, you can trigger your endpoints.
130
-
```bash
135
+
136
+
At the end of the deployment, you will find the API endpoint URL within `Outputs` section. You can use this URL to test your serverless application.
137
+
138
+
```bash title="Invoking our application via API endpoint"
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).
143
+
For more details on AWS SAM deployment mechanism, see [SAM Deploy reference docs](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-deploy.html).
136
144
137
145
## API Gateway router
138
146
Let's expand our application with a new method. It takes an username as a input and return it in the response.
@@ -450,6 +458,10 @@ By having structured logs like this, we can easily search and analyse them in [C
**Tracer** uses AWS X-Ray and you will not see any traces when executing your function locally.
464
+
453
465
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.
454
466
With structured logs, it is an important step to provide the observability of your application!
455
467
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?
0 commit comments