AWS CloudWatch Real User Monitoring (RUM) Web Client
The `aws-rum-web` client is a JavaScript library designed for real user monitoring (RUM) of web applications, integrating seamlessly with Amazon CloudWatch. It provides telemetry on critical user experience metrics such as page load timings, JavaScript errors, and HTTP request performance. The current stable version is 2.1.0, and the package maintains a relatively active release cadence with frequent minor and patch updates. Key differentiators include its tight integration into the AWS observability ecosystem, enabling developers to gain deeper insights into their web application's frontend performance and user behavior directly within CloudWatch, and its comprehensive data collection capabilities for various web vitals and errors.
Common errors
-
ReferenceError: AwsRum is not defined
cause The `AwsRum` class was not imported or the script loading the RUM client failed in a module environment, or `AWSRUM` global object is accessed prematurely in a CDN setup.fixFor module usage, ensure `import { AwsRum } from 'aws-rum-web';` is at the top of your file. For CDN, verify the RUM script is loaded before `AWSRUM` is referenced. -
Failed to initialize AWS RUM client: Missing required configuration parameter: applicationId
cause The `applicationId` property was not provided or was empty in the `AwsRumConfig` object passed to the `AwsRum` constructor.fixPopulate the `applicationId` field in your `AwsRumConfig` with the correct value from your AWS CloudWatch RUM application monitor setup. -
Access to XMLHttpRequest at 'https://dataplane.rum.us-east-1.amazonaws.com/...' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
cause The browser is preventing the RUM client from sending data to the RUM endpoint due to a Cross-Origin Resource Sharing (CORS) policy violation. This usually indicates a misconfiguration in the AWS RUM application monitor's domain allow-list.fixIn your AWS RUM application monitor configuration, ensure that your application's domain (e.g., `http://localhost:3000` for development, or your production domain) is correctly added to the 'Allow additional domains' setting.
Warnings
- breaking The `VirtualPageLoadTimer` is now disabled by default. This may affect how page load metrics are collected and reported if your application relied on its previous default enabled state.
- gotcha Incorrect or incomplete `AwsRumConfig` parameters (e.g., `applicationId`, `id`, `region`, `guestRoleArn`, `endpoint`) will prevent the RUM client from sending data to CloudWatch, potentially failing silently or with console errors.
- gotcha Browser Content Security Policy (CSP) rules can block the RUM client from sending data to the CloudWatch RUM data plane or from loading necessary external resources (e.g., for plugins like rrweb).
- gotcha Ad blockers or browser extensions can interfere with the RUM client's ability to collect and send data, leading to incomplete or missing telemetry in CloudWatch.
Install
-
npm install aws-rum-web -
yarn add aws-rum-web -
pnpm add aws-rum-web
Imports
- AwsRum
const AwsRum = require('aws-rum-web');import { AwsRum } from 'aws-rum-web'; - AwsRumConfig
import { AwsRum, AwsRumConfig } from 'aws-rum-web'; - rrwebPlugin
import rrwebPlugin from 'aws-rum-web';
import { rrwebPlugin } from 'aws-rum-web';
Quickstart
import { AwsRum, AwsRumConfig } from 'aws-rum-web';
// Configure your AWS RUM application details. Replace placeholders.
// Ensure your Identity Pool allows unauthenticated access to the RUM endpoint.
const rumConfig: AwsRumConfig = {
applicationId: process.env.AWS_RUM_APP_ID ?? 'YOUR_APP_ID',
applicationVersion: '1.0.0',
id: process.env.AWS_RUM_MONITOR_ID ?? 'YOUR_MONITOR_ID',
region: process.env.AWS_RUM_REGION ?? 'us-east-1',
sessionSampleRate: 0.1, // Sample 10% of sessions for monitoring
guestRoleArn: process.env.AWS_RUM_GUEST_ROLE_ARN ?? 'arn:aws:iam::123456789012:role/RUM_Guest_Role',
endpoint: process.env.AWS_RUM_ENDPOINT ?? 'https://dataplane.rum.us-east-1.amazonaws.com',
// Optional: add a plugin for session replay
// plugins: [rrwebPlugin()],
allowCookies: true,
enableXRay: true
};
try {
const rum = new AwsRum(rumConfig);
// The RUM client automatically collects performance data, errors, and web vitals.
// You can also manually record events or metrics:
// rum.recordPageView({ name: '/my-custom-page' });
// rum.recordEvent('UserLogin', { success: true, method: 'email' });
console.log('AWS RUM client initialized successfully.');
} catch (error) {
console.error('Failed to initialize AWS RUM client:', error);
}