Test the application

Test the application

A new CloudFront distribution takes a few minutes to propagate to all edge locations. When propagation finishes, the distribution status changes to Deployed. Before that, you can check whether the domain already resolves by running nslookup against the CloudFront domain name. CloudFront returns multiple IP addresses for each domain to increase availability.

  1. Download MobaXterm. You will use it to SSH into your EC2 instance. CloudFront Lab

  2. Create an SSH session in MobaXterm:

    • Click Session, then select SSH.
    • In Remote host, enter the public IP address of your EC2 instance.
    • In Username, enter ec2-user.
    • Under Advanced SSH settings, select Use private key and choose your .pem key file.
    • Click OK. CloudFront Lab
  3. Open the CloudFront console and copy the Distribution domain name CloudFront Lab

  4. In the EC2 terminal, run: nslookup YOUR-DOMAIN-NAME. The output lists several IPv4 and IPv6 addresses for the same domain name. This confirms that the distribution resolves. CloudFront Lab

  5. Open https://YOUR-DOMAIN-NAME in your browser. The page shows the request headers that CloudFront forwarded to the API origin. Note these:

    • x-amz-cf-id: a unique ID that CloudFront assigns to each request. It changes every time you refresh the page. CloudFront also returns it to the viewer and writes it to the access logs, so log it on your web server. If you open an AWS Support case, include this ID.
    • via: shows that the request passed through CloudFront.

    CloudFront redirects HTTP requests to HTTPS, so the page loads over HTTPS. CloudFront Lab

  6. Open the browser developer tools (F12), go to the Network tab, refresh the page, and select the document request. Check these response headers:

    • X-Amz-Cf-Id: the request ID assigned by CloudFront.
    • X-Amz-Cf-Pop: the edge location that served the request. It is a three-letter code plus a number, for example HAN51-P2. The three-letter code usually matches the IATA code of an airport near the edge location.
    • X-Cache: whether CloudFront served the response from cache. For the HTML file, subsequent requests return Hit from cloudfront. Requests to /api always return Miss from cloudfront because caching is disabled for that behavior. CloudFront Lab

A cache hit means CloudFront returned the object from the edge without contacting the origin, which reduces page load time. If you update content on the origin, invalidate the cache so that users get the latest version.