Optimise AWS Lambda Performance using CloudWatch and Xray
My specialities in previous years includes optimisation of page load during the launch of websites-this is reduce energy consumption and improve user experience. XRay from AWS has got tremendous features to help us when used at right time to spot the delays in page load.
Let us see some interesting use cases by creating a simple website using Lambda.
Lambda Blueprint based Website
Navigate to AWS Lambda to create a function:
Choose the option of 'Use a blueprint':
Make sure to create a role with basic Lambda functions to execute this code:
Use a simple handler to launch the page with status code as 200:
Code:
import * as fs from 'node:fs';
const html = fs.readFileSync('index.html', { encoding: 'utf8' });
/**
* Returns an HTML page containing an interactive Web-based tutorial.
* Visit the function URL to see it and learn how to build with lambda.
*/
export const handler = async (event) => {
console.log('Received event:', JSON.stringify(event, null, 2));
const response = {
statusCode: 200,
headers: {
'Content-Type': 'text/html',
},
body: html,
};
return response;
};
Now deploy the code using Deploy button. Once deployed, navigate to configuration section to navigate to monitoring and operational tools to enable Xray:
Click enable button for Xray at Lambda:
Now launch the website by copying the function URL from function overview. Once page is loaded, navigate to cloud watch to see logs (via monitoring of Lambda):
Similarly click on Monitoring->Open XRay logs to navigate to see each components load time and delays within XRay Logs:
This trace will further help on identifying root cause for some of the failures and delays during the page load time-this is similar to lighthouse extension of Google Chrome:
Overview
AWS X-Ray is a service that helps developers analyze and debug distributed applications, particularly those built using microservices. When it comes to website page load performance, AWS X-Ray can be extremely useful for identifying and resolving bottlenecks or performance issues. Here's how it can be applied to optimize page load:
Recommended by LinkedIn
1. Trace Requests Across Services
2. Identify Performance Bottlenecks
3. Monitor and Debug Errors
4. Visualize the Request Flow
5. Track External Service Calls
6. Improve User Experience
7. Integrating with AWS Services
8. Root Cause of Slow Loading Web Pages
Example Use Case:
Imagine a scenario where a webpage takes too long to load, and you need to debug it. With AWS X-Ray enabled, you can:
Conclusion:
In short, AWS X-Ray helps you track and optimize the performance of a web application by providing deep insights into the time it takes for each component of the app to process requests during a page load. It gives you the ability to diagnose and fix performance bottlenecks, reduce latency, improve reliability, and ensure a smooth user experience on your website.
👉Please feel free to share your views in the comments section on some AWS XRay usecases.
⚡Follow me on LinkedIn: Link
Like this article? Subscribe to Engineering Leadership , Digital Accessibility, Digital Payments Hub and Motivation newsletters to enjoy reading useful articles. Press SHARE and REPOST button to help sharing the content with your network.