Detect and Recover from Website Errors

AWSBeginner
Practice Now

Introduction

Configure a CloudWatch alarm, observe a failed website publication and restore healthy traffic to return the alarm to OK.

Complete the request-log lab first. This fresh environment supplies its own website, three private S3 covers, project list, role, logs and request-metric integration. You create the alarm and perform the failure and recovery, without reusing earlier resources or credentials.

Certification Relevance

Certification Exam task Practice
AWS Certified Cloud Practitioner (CLF-C02) Task 3.8 Use CloudWatch request metrics, alarms and logs to observe application failure and recovery.

Lab Overview

Configure a CloudWatch alarm, observe a failed website publication and restore healthy traffic to return the alarm to OK.

Configure a Website Error Alarm

In this step, you will create an alarm for actual errors in portfolio page requests.

Open the portfolio from AWS View and confirm it works. Each /website request supplies an Errors metric value: 1 for failure, 0 for success. Images and other requests have their own log events, but do not contribute to this page metric.

A metric measures numbers over time. A namespace groups metrics, and a dimension identifies the application. Here the metric is Errors in LabEx/Portfolio, with Application=portfolio. An alarm compares evaluated data with a threshold; it detects the condition and does not repair the website.

Load and display this lab’s identifiers. The first value is the instance ID, the second is its address for SCP, and the third is the log group:

cd /home/labex/project
source website.env
echo "$INSTANCE_ID"
echo "$PUBLIC_IP"
echo "$LOG_GROUP"

Inspect the prepared alarm file. The following settings explain what it evaluates:

cat website-alarm.json
Setting Meaning
MetricName: Errors Website request errors
Statistic: Sum Add error values within each period
Period: 10 Evaluate ten-second periods
EvaluationPeriods: 1 One evaluated period is sufficient
Threshold: 0 / GreaterThanThreshold Any error makes the sum greater than zero
TreatMissingData: missing Missing requests are not assumed to be healthy
Unit: Count Count failed requests

Ten-second alarms require high-resolution custom metrics; the supplied integration publishes at one-second resolution. This lab configures no notification destination. As with launch.json, --cli-input-json reads the supplied local file through file://:

aws cloudwatch put-metric-alarm \
  --cli-input-json file://website-alarm.json

A successful update returns without output. Read only the alarm name, state and reason with --query:

aws cloudwatch describe-alarms \
  --alarm-names portfolio-errors \
  --query 'MetricAlarms[0].{AlarmName:AlarmName,StateValue:StateValue,StateReason:StateReason}'

Initial StateValue may be INSUFFICIENT_DATA while evaluated data is missing. AWS View shows the same alarm. The next steps generate real failed and healthy requests; keep the state driven by those requests rather than setting it manually.

Observe a Failed Publication

In this step, you will publish a deliberately invalid profile and connect its real request failures to the alarm.

Inspect profile-broken.json. Its {invalid-json text is an intentional practice fault; the application cannot parse it as JSON. Publish it with the SCP connection and target path you learned earlier:

cat profile-broken.json
scp -F ssh_config profile-broken.json ubuntu@"$PUBLIC_IP":/home/ubuntu/portfolio/profile.json

Refresh the portfolio. Expect Website unavailable while EC2 still shows running in AWS View. The server is running, but the application cannot handle the page request. Find the failed response in the log group:

aws logs filter-log-events \
  --log-group-name "$LOG_GROUP" \
  --filter-pattern '"status=503"' \
  --query 'events[].message'

Confirm path=/website status=503. HTTP 503 records an unsuccessful application response. The recent publication gives you a specific cause to investigate; a log status alone does not explain every possible cause.

Allow about ten seconds for evaluation, then read the alarm. If it has not reached ALARM, refresh the failing page to supply another real sample, allow another evaluation period and run the same read again:

aws cloudwatch describe-alarms \
  --alarm-names portfolio-errors \
  --query 'MetricAlarms[0].{AlarmName:AlarmName,StateValue:StateValue,StateReason:StateReason}'

Confirm StateValue: ALARM in the response and AWS View. A stale 503 log is not a fresh metric sample; keep failing requests current while checking. You have connected a real failed publication to measured errors.

While waiting for the ten-second evaluation, refresh the page about every five seconds. This keeps samples in adjacent periods. After reading the expected state, promptly use the step’s Check button while those requests are still recent.

Example AWS View: actual website 503 requests trigger ALARM while the server remains running.

Restore the Website and Confirm Recovery

In this step, you will restore the known working profile and confirm both successful requests and the alarm recover.

Inspect profile-working.json. It supplies the three valid fields the application expects; publish it to the same target:

cat profile-working.json
scp -F ssh_config profile-working.json ubuntu@"$PUBLIC_IP":/home/ubuntu/portfolio/profile.json

Refresh the portfolio. Alex Morgan, Make it memorable. and the three covers should return. This verifies the application repair. Read successful request messages:

aws logs filter-log-events \
  --log-group-name "$LOG_GROUP" \
  --filter-pattern '"status=200"' \
  --query 'events[].message'

Find a new path=/website status=200 after the failure. Earlier 503 events remain in the group’s history. A repaired page can appear before the alarm changes: an error already counted in the previous period is not erased by a later success.

Let successful traffic reach a new evaluation period. Refresh the healthy page about every five seconds and repeat the state read as needed until StateValue: OK. OK means evaluated request data does not exceed the error threshold. If requests stop, missing-data handling can return INSUFFICIENT_DATA; that is not proof of healthy traffic.

aws cloudwatch describe-alarms \
  --alarm-names portfolio-errors \
  --query 'MetricAlarms[0].{AlarmName:AlarmName,StateValue:StateValue,StateReason:StateReason}'

While waiting for the ten-second evaluation, refresh the page about every five seconds. This keeps samples in adjacent periods. After reading the expected state, promptly use the step’s Check button while those requests are still recent.

Example AWS View: new website and image 200 requests accompany OK; earlier 503 events remain in history.

Summary

You completed a real operational loop: publication caused failures, logs showed responses, an alarm detected measured errors, and a valid publication restored the website and OK state. You also distinguished absent data from successful traffic. The portfolio remains available for you to explore.

Official references: CloudWatch alarms and high-resolution custom metrics.