AWS Developer Tools Blog
Introducing AWS X-Ray support for Python web frameworks used in Serverless applications
This is a guest post by Chan Chiem Jeffery Saeteurn. Jeffery is a Software Development Engineer on the AWS X-Ray SDK Team. He has fond interests in IoT, distributed systems, and crafting software to automate everyday tasks.
Announcing AWS X-Ray SDK for Python support for instrumenting web frameworks deployed in serverless applications! Serverless is an application model that enables you to shift more of your operational responsibilities to AWS. As a result, you can focus only on your applications and services, instead of the infrastructure management tasks such as server provisioning, patching, operating system maintenance, and capacity provisioning. With serverless, you can deploy your web application to AWS Lambda and have customers interact with it through a Lambda-invoking endpoint, such as Amazon API Gateway.
Now you can deploy your Python web application using the serverless model and have AWS X-Ray automatically instrument your web frameworks so that you can track the internal requests. You can see an end-to-end view of all the downstream calls made starting from Amazon API Gateway, to Lambda, and all through the downstream calls that your web application makes.
Currently supported frameworks
The currently supported frameworks for this architecture are Flask and Django.
Prerequisites
In this post, we show an example of creating a serverless application and deploying it to Lambda, where it will be invoked via Amazon API Gateway. We’ll use Zappa to automatically deploy our application to Lambda and configure our API Gateway endpoint.
You need to have the following installed:
- Python (2.7 or 3.6)
- Pip
- Virtualenv
- AWS CLI
Setting up your serverless application
Be sure your AWS CLI is configured with the account and AWS Region that you want your application to be deployed in. Zappa uses this for deployment.
Developing Your Application
First, use your terminal to create a directory to store your application, and then change to the new directory. For this example, we named our project serverless_application.
Next, create a virtual environment inside your new directory, and then use the following command to activate it.
Install X-Ray, Zappa, Flask, and Requests into your environment.
Add some code to your directory. In this example, we’ll write a simple application based on Flask’s Hello World example.
Within the serverless_application directory, create a file named my_app.py. Using your editor of choice, add the following code.
This application instruments the requests module, patches the Flask application’s middleware, and opens the endpoint ‘/’. When a request is received, the application sends an outgoing request to https://aws.amazon.com and returns the text Hello, World: https://aws.amazon.com.
Creating a Zappa Environment and Deploying it
Go back to your terminal and initialize Zappa from within the new virtual environment.
Follow the instructions to configure the Zappa deployment. In this example, use the default settings.
Next, we need to enable X-Ray. To do so, open the file named zappa_settings.json in your favorite text editor. It should look something like this.
Add “xray_tracing”: true as an entry to your configuration (don’t forget to add a comma to the end of the previous line). The result should be similar to the following.
The application is now configured. Next, you need to deploy your application. This automatically configures the API Gateway endpoint and uploads your code to Lambda.
If everything is deployed correctly, the CLI displays the endpoint URL to access your application. You should see the following result.
Next, we need to enable X-Ray tracing for API Gateway.
- Open the API Gateway console.
- Find your newly generated API (it should look something like serverless-exam-dev).
- Choose Stages.
- Choose the name of your deployment stage (the default is dev).
- On the Logs/Tracing tab, select the Enable X-Ray Tracing box.
- Choose Save Changes.
Now use your favorite browser to access your endpoint: https://**.execute-api.us-west-2.amazonaws.com/dev
It should display the text “Hello, World: https://aws.amazon.com/”.
Viewing the Trace
Open the X-Ray console and view your traces. You should see the trace made from the endpoint access, which contains a segment generated by API Gateway, a segment generated by your Lambda function, and a segment generated by the Lambda container. You should also see a subsegment named My First Serverless App under the Lambda function’s segment, followed by another subsegment named https://aws.amazon.com. You might also see another subsegment named initialization. Sometimes Lambda generates this during initialization. The following is an example.
Here is an example of the service graph.
This shows the downstream path starting from API Gateway, through the Lambda function, and to the outgoing request made to https://aws.amazon.com.
The following screenshot is the same call, but made on a local machine running Flask.
In this example, you can see that the Flask middleware generates a node, but the serverless application doesn’t. This is the intended behavior. In Lambda, the container acts as the middleware that instruments the downstream calls.
You can add more features to your application by adding AWS clients using Boto3 to work with services such as AWS DynamoDB and Amazon S3 and instrument them with X-Ray. Web framework support in serverless applications allows you to deploy applications and seamlessly integrate X-Ray into your normal workflow.
Cleaning Up Your Application
To clean up your application and remove it from Lambda, API Gateway, and Amazon S3, run the following command inside your project directory.
You can also delete your project directory.
Give it a try!
Let us know what you think about this feature on GitHub!