AWS Developer Tools

Instrumenting Your Application with AWS X-Ray

5 min read
Updated June 23, 2025
5,655 characters

--

Architecture and Goal

The goal is to trace a web request as it travels through our application stack. The setup will be:

  • An Amazon EC2 instance running a simple Node.js (Express) application.

  • The AWS X-Ray Daemon running on the same EC2 instance to collect and forward trace data.

  • The AWS X-Ray SDK integrated into the Node.js application code to generate trace data.

  • An IAM Role attached to the EC2 instance granting permissions to send data to X-Ray.

Finally, we will view the trace data, including the service map, in the AWS X-Ray console.


Step 1: Create an IAM Role for EC2

Your EC2 instance needs permission to communicate with the AWS X-Ray service.

  1. Navigate to the IAM console.

  2. Go to Roles and click Create role.

  3. For Trusted entity type, select AWS service.

  4. For Use case, select EC2 and click Next.

  5. In the Add permissions search box, type AWSXRayDaemonWriteAccess.

  6. Check the box next to the policy and click Next.

  7. Give the role a descriptive Role name (e.g., EC2-XRay-Access-Role).

  8. Review the details and click Create role.


Step 2: Launch and Configure an EC2 Instance

Now, launch an EC2 instance that will run the X-Ray daemon and your Node.js application.

  1. Navigate to the EC2 console and click Launch instances.

  2. Give your instance a name (e.g., X-Ray-Demo-Server).

  3. Select an Amazon Machine Image (AMI), such as Amazon Linux 2.

  4. Choose an Instance type, like t2.micro (which is Free Tier eligible).

  5. Create or select a Key pair to allow SSH access.

  6. In Network settings, ensure that you have a security group that allows inbound traffic on:

    • SSH (port 22) from your IP address for access.

    • Custom TCP (port 8080) from anywhere, as this is where our sample app will run.

  7. Expand the Advanced details section. For IAM instance profile, select the EC2-XRay-Access-Role you created in the previous step.

  8. In the User data field, paste the following script. This script will install Node.js, Git, and the AWS X-Ray daemon when the instance launches.

    
    #!/bin/bash
    
    # Install Node.js and npm
    
    yum update -y
    
    yum install -y nodejs
    
    
    
    # Install Git
    
    yum install -y git
    
    
    
    # Download and install the X-Ray Daemon
    
    curl https://s3.us-east-2.amazonaws.com/aws-xray-assets.us-east-2/xray-daemon/aws-xray-daemon-3.x.rpm -o /home/ec2-user/xray.rpm
    
    yum install -y /home/ec2-user/xray.rpm
    
  9. Click Launch instance.


Step 3: Deploy and Instrument the Node.js Application

Once the instance is running, connect to it via SSH to deploy and instrument the application.

  1. SSH into your EC2 instance.

  2. Clone a sample Node.js application:

    
    git clone https://github.com/aws-samples/aws-xray-sdk-node-sample.git
    
    cd aws-xray-sdk-node-sample
    
  3. Install Application Dependencies and the X-Ray SDK: The package.json in this sample project already includes the aws-xray-sdk as a dependency.

    
    npm install
    
  4. Examine the Instrumented Code (index.js):

    Open the main application file (index.js) to see how the X-Ray SDK is used. The key parts are:

    • Importing the SDK:

      
      const AWSXRay = require('aws-xray-sdk');
      
    • Capturing AWS SDK calls: This automatically traces any calls made with the AWS SDK.

      
      const AWS = AWSXRay.captureAWS(require('aws-sdk'));
      
    • Using Express middleware: These lines wrap your application's routes to trace incoming HTTP requests.

      
      const app = express();
      
      app.use(AWSXRay.express.openSegment('MyApp')); // Creates the main segment
      
      
      
      // ... Your application routes go here ...
      
      
      
      app.use(AWSXRay.express.closeSegment()); // Closes the segment
      

Step 4: Run the Application and Generate Trace Data

  1. Start the X-Ray Daemon: The daemon listens on UDP port 2000 for data from the SDK.

    
    # Run the daemon in the background
    
    xray -o -b 0.0.0.0:2000 &
    
  2. Start the Node.js Application:

    
    node index.js
    

    You should see the output Example app listening on port 8080!

  3. Generate Traffic:

    Open a new terminal (or use your local machine) and send a request to your application using the EC2 instance's public IP address.

    
    curl http://YOUR_EC2_PUBLIC_IP:8080
    

    Repeat this command several times to generate enough trace data.


Step 5: View the Traces in the AWS X-Ray Console

  1. Navigate to the AWS X-Ray console.

  2. On the left-hand menu, click on Service map. You should see a map with at least two nodes: one for the client and one for your application (MyApp). The nodes will be green if requests are successful.

  3. Click on Traces. You will see a list of recent traces.

  4. Click on a Trace ID to view the trace details. You will see a timeline showing the full duration of the request.

  5. Examine the Segments in the timeline. You will see the main segment created by your Express app (MyApp) and potentially subsegments if your application were making downstream calls (e.g., to S3 or DynamoDB). You can click on each segment to see its metadata, annotations, and timing details.