A Deep Dive into Serverless Tracing with AWS X Ray & Lambda

A Deep Dive into Serverless Tracing with AWS X Ray & Lambda

A Deep Dive into Serverless Tracing with AWS X Ray & Lambda. Over the past few weeks I’ve been experimenting with building a Serverless API on AWS with the goal of having everything needed to run a production system. One necessary piece was distributed tracing. While I’d seen a bit of what some non-AWS options had to offer, the extra cost of the services themselves, along with actually getting the data to them, was a bit prohibitive for what I imagined should be possible (and cheaper) with only AWS, which brought me to X Ray.

Image for post

Over the past few weeks I’ve been experimenting with building a Serverless API on AWS with the goal of having everything needed to run a production system. One necessary piece was distributed tracing. While I’d seen a bit of what some non-AWS options had to offer, the extra cost of the services themselves, along with actually getting the data to them, was a bit prohibitive for what I imagined should be possible (and cheaper) with only AWS, which brought me to X Ray. So, I wanted to see if I could use AWS X Ray to get everything I wanted out of a tracing solution. First, I’ll set some expectations of what I need in a production tracing solution as a goal to orient the research process.

Requirements

  1. Distributed tracing. On a high level, I want to see the path of a request from service to service, along with location of errors and details on latency.
  2. Internal tracing, or a “flame graph”. I want to be able to see the breakdown of a request inside of a lambda, along with a stack trace when an error occurs. This will also help me spot any parts of my lambda that are running particularly inefficiently.

Setup

First, a standard Serverless project setup and code walk-through. By the way, all the code I used in this post is available here.

npm i -g serverless
sls create --template aws-nodejs --path lambda-demo-api
cd lambda-demo-api && npm init && npm i --save-dev serverless-offline

The file structure of the lambda we’re going to deploy looks like this:

├── anotherFile.js
├── index.js
├── logger.js
├── node_modules
├── package-lock.json
├── package.json
├── serverless.yml
└── utils.js

I’ll be explaining each of these files later on. For now, let’s check out the initial serverless config.

nodejs aws-lambda aws-x-ray aws serverless

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Invoke AWS Lambda with Another Lambda Function with Nodejs

In this article, I will show you how to invoke a lambda function from another lambda function.

Serverless Express – Easy APIs On AWS Lambda & AWS HTTP API

Serverless Express enables you to easily host Express.js APIs on AWS Lambda and AWS HTTP API. Here is how to get started and deliver a Serverless Express.js based API with a custom domain, free SSL certificate and much more!

Reporting Errors via ChatOps using AWS Lambda & NodeJS

This article explains how to add real time notifications using Slack, ChatOps, Lambda and NodeJS. This article explains how to inject an error reporting module using ChatOps and how it helps organizations react faster to issues arising from applications that were running inside AWS Lambda Functions. In this demo, I will be using NodeJS and will utilize Slack to deliver alarms to important stakeholders. Reporting Errors via ChatOps using AWS Lambda & NodeJS

Go Serverless with NodeJS and AWS Lambda

Learn how to deploy a simple serverless API with Node.js, AWS Lambda, and the Serverless framework. Make your NodeJS Express API run serverless and deploy it to AWS

CI/CD Pipeline for NodeJS Lambdas on AWS using Jest, Serverless Framework, Github

I wanted to start a serverless project, and one of the primary headaches aside from architecting the workflow and deciding which lambda functions need to be created is a simple and yet effective CICD workflow.