A
<p>Relying solely on server metrics isn't enough to ensure your application is working correctly. You need to simulate user experience to catch potential issues before your customers do. This is exactly what <strong>AWS CloudWatch Synthetics</strong> provides.</p>
<div class="toc">
<h3>Table of Contents</h3>
<ul>
<li><a href="#what-is-it">What is CloudWatch Synthetics?</a></li>
<li><a href="#canary-types">Canary Types</a></li>
<li><a href="#how-it-works">How It Works</a></li>
<li><a href="#step-by-step-setup">Step-by-Step Setup</a></li>
<li><a href="#code-example">Example Canary Code</a></li>
<li><a href="#monitoring-and-alerts">Monitoring and Alerts</a></li>
<li><a href="#faq">Frequently Asked Questions (FAQ)</a></li>
</ul>
</div>
<h2 id="what-is-it">What is CloudWatch Synthetics?</h2>
<p>AWS CloudWatch Synthetics allows you to monitor your application endpoints and APIs. It uses configurable scripts called <strong>Canaries</strong> to run tests on your application at scheduled intervals. Canaries are scripts (Node.js or Python) that mimic the behavior of a user navigating your application.</p>
<h2 id="canary-types">Canary Types</h2>
<p>CloudWatch Synthetics offers several types of Canaries (blueprints) for different needs:</p>
<ul>
<li><strong>Heartbeat Monitor:</strong> Checks if a specific URL is reachable, captures latency and screenshots. This is the most basic monitoring type.</li>
<li><strong>API Canary:</strong> Used to test your REST APIs (GET, POST, PUT, etc.). It checks response times and validates the returned data.</li>
<li><strong>Broken Link Checker:</strong> Crawls your website to find broken links.</li>
<li><strong>Visual Monitoring:</strong> Checks for visual changes on a page (Visual regression testing).</li>
<li><strong>GUI Workflow Builder:</strong> Allows you to create click and navigation scenarios without writing code.</li>
</ul>
<h2 id="how-it-works">How It Works</h2>
<p>Canaries run on AWS Lambda functions in the background. They are triggered at the frequency you define (e.g., every 5 minutes). Using headless Chrome (Puppeteer) or Selenium, they navigate to your web page, click buttons, fill forms, or make API requests. The results, including success/failure status, screenshots, HAR files (network requests), and logs, are saved to CloudWatch and S3.</p>
<h2 id="step-by-step-setup">Step-by-Step Setup</h2>
<p>Follow these steps to create a simple Heartbeat Canary:</p>
<ol>
<li><strong>Go to CloudWatch Console:</strong> Open the CloudWatch service in the AWS Management Console and click on "Synthetics Canaries" in the left menu.</li>
<li><strong>Create Canary:</strong> Click the "Create Canary" button.</li>
<li><strong>Select Blueprint:</strong> Choose "Heartbeat Monitoring".</li>
<li><strong>Configuration:</strong>
<ul>
<li><strong>Name:</strong> Give your Canary a name (e.g., <code>my-app-monitor</code>).</li>
<li><strong>Application URL:</strong> Enter the URL you want to monitor.</li>
</ul>
</li>
<li><strong>Schedule:</strong> Determine how often the test will run (e.g., 5 minutes).</li>
<li><strong>Data Storage:</strong> Select the S3 bucket where test results (screenshots, logs) will be stored.</li>
<li><strong>Create:</strong> Click "Create Canary" to finish the process.</li>
</ol>
<h2 id="code-example">Example Canary Code</h2>
<p>CloudWatch Synthetics uses the Node.js Puppeteer library. Below is a simple example that checks the page title:</p>
<pre><code class="language-javascript">
var synthetics = require('Synthetics');
const log = require('SyntheticsLogger');
const pageLoadBlueprint = async function () {
// URL to monitor
const URL = "https://example.com";
let page = await synthetics.getPage();
const response = await page.goto(URL, {waitUntil: 'domcontentloaded', timeout: 30000});
// Check HTTP status code
if (!response || response.status() < 200 || response.status() > 299) {
throw "Failed to load page!";
}
// Get page title and log it
let title = await page.title();
log.info("Page title: " + title);
// Take a screenshot
await synthetics.takeScreenshot('loaded', 'loaded');
};
exports.handler = async () => {
return await pageLoadBlueprint();
};
</code></pre>
<h2 id="monitoring-and-alerts">Monitoring and Alerts</h2>
<p>Once a Canary is created, "Availability" and "Latency" metrics are automatically sent to CloudWatch Metrics. You can create CloudWatch Alarms based on these metrics to receive notifications (via SNS, email) when your site becomes unreachable or latency increases.</p>
<h3 id="troubleshooting">Troubleshooting</h3>
<p>When a test fails, you can investigate using:</p>
<ul>
<li><strong>Screenshots:</strong> View the screenshot taken at the moment of failure.</li>
<li><strong>Logs:</strong> Examine Lambda logs to understand if the error is code-related or network-related.</li>
<li><strong>HAR File:</strong> Analyze the dump of network requests to identify which resource (image, script, etc.) failed to load.</li>
</ul>
<h2 id="faq">Frequently Asked Questions (FAQ)</h2>
<h3>1. Is CloudWatch Synthetics free?</h3>
<p>No, CloudWatch Synthetics is billed per Canary run. Additionally, standard charges apply for the logs (CloudWatch Logs) and stored data (S3) generated by the tests.</p>
<h3>2. Which languages do Canary scripts support?</h3>
<p>Currently, it supports Node.js (Puppeteer) and Python (Selenium) runtimes.</p>
<h3>3. Can I monitor applications inside a private VPC?</h3>
<p>Yes, by attaching your Canary to a VPC, you can monitor internal applications that are not exposed to the internet.</p>
<p>For more information and technical support, check out our <a href="/en/tech/aws-consultancy">AWS Consultancy</a> or <a href="/en/tech/kubernetes-consultancy">Kubernetes Consultancy</a> services.</p>
<p>Kaynak / Source: <a href="https://awsfundamentals.com/blog/aws-cloudwatch-synthetics" target="_blank" rel="noopener noreferrer">https://awsfundamentals.com/blog/aws-cloudwatch-synthetics</a></p>