Tutorial
This tutorial progressively introduces Powertools for AWS Lambda (Python) core utilities by using one feature at a time.
Requirements¶
- AWS CLI and configured with your credentials.
- AWS SAM CLI installed.
Getting started¶
Let's clone our sample project before we add one feature at a time.
Tip: Want to skip to the final project?
Bootstrap directly via SAM CLI:
1 | |
| Use SAM CLI to initialize the sample project | |
|---|---|
1 | |
Project structure¶
As we move forward, we will modify the following files within the powertools-quickstart folder:
- app.py - Application code.
- template.yaml - AWS infrastructure configuration using SAM.
- requirements.txt - List of extra Python packages needed.
Code example¶
Let's configure our base application to look like the following code snippet.
1 2 3 4 5 6 7 8 9 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
Our Lambda code consists of an entry point function named lambda_handler, and a hello function.
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.
Warning
For simplicity, we do not set up authentication and authorization! You can find more information on how to implement it on AWS SAM documentation.
Run your code¶
At each point, you have two ways to run your code: locally and within your AWS account.
Local test¶
AWS SAM allows you to execute a serverless application locally by running sam build && sam local start-api in your preferred shell.
| Build and run API Gateway locally | |
|---|---|
1 2 3 | |
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, httpie, etc.
| Invoking our function locally via curl | |
|---|---|
1 2 | |
Info
To learn more about local testing, please visit the AWS SAM CLI local testing documentation.
Live test¶
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.
| Build and deploy your serverless application | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
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.
| Invoking our application via API endpoint | |
|---|---|
1 2 | |
Info
For more details on AWS SAM deployment mechanism, see SAM Deploy reference docs.
Routing¶
Adding a new route¶
Let's expand our application with a new route - /hello/{name}. It will accept an username as a path input and return it in the response.
For this to work, we could create a new Lambda function to handle incoming requests for /hello/{name} - It'd look like this:
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
Question
But what happens if your application gets bigger and we need to cover numerous URL paths and HTTP methods for them?
This would quickly become non-trivial to maintain. Adding new Lambda function for each path, or multiple if/else to handle several routes & HTTP Methods can be error prone.
Creating our own router¶
Question
What if we create a simple router to reduce boilerplate?
We could group similar routes and intents, separate read and write operations resulting in fewer functions. It doesn't address the boilerplate routing code, but maybe it will be easier to add additional URLs.
Info: You might be already asking yourself about mono vs micro-functions
If you want a more detailed explanation of these two approaches, head over to the trade-offs on each approach later.
A first attempt at the routing logic might look similar to the following code snippet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |