Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

Amazon CloudFront and WAF: CDN, HTTPS and Web Firewall Explained

Your website is in Mumbai S3, but a user in Europe waits. CloudFront copies popular files to edge locations closer to users. WAF sits in front to block common web attacks and noisy bots. CDN for speed; WAF for punches you do not want.

In this chapter

  • Why CDN + WAF together
  • CloudFront distributions, origins, behaviors
  • HTTPS certificates and price classes
  • AWS WAF rules and Bot Control basics
  • Lab, errors, FAQ, interview

1. Everyday example first

An education site launches a PDF exam guide. Without CloudFront every download hits the origin. With CloudFront, the first reader warms the edge; the next thousand are cheap and fast. WAF rate rules stop a scraper from downloading the whole library.

2. Why CloudFront and WAF?

Latency and DDoS/L7 abuse are different problems. CloudFront shortens distance and absorbs some volumetric noise. WAF inspects HTTP and applies managed or custom rules. Most public apps want both.

3. What is CloudFront?

Imagine a college page with a public logo and a private report. CloudFront can receive a request at an edge location. An origin is the system that supplies a response when needed, such as an S3 bucket or load balancer. A cache key describes which requests may share a cached response. The logo can be shared across users when the behavior permits it. A private report needs a design that keeps one user’s response from being reused for another. Draw edge, cache and origin, then trace the logo’s first load and a later hit.

A distribution points to origins (S3, ALB, custom). Behaviors map path patterns to cache policies and origin request policies. Viewers can be forced to HTTPS with ACM certificates (usually us-east-1 for CloudFront).

PieceRole
DistributionThe CDN config unit
OriginWhere content lives
BehaviorPath → cache/origin rules
WAF Web ACLRules attached to distribution
A protected CloudFront requestViewer HTTPS request: Arrives at the distribution; Distribution web ACL: AWS WAF checks the request before caching; Allowed request: CloudFront checks its cache; Hit or origin request: A miss may require an origin fetch; Response: Returns to the viewerViewer HTTPS requestArrives at the distributionDistribution web ACLAWS WAF checks the request beforecachingAllowed requestCloudFront checks its cacheHit or origin requestA miss may require an origin fetchResponseReturns to the viewer
Fast edges + filtered HTTP

4. How to set up (checklist)

Decide which origin you have before setting access permissions. For a private S3 REST origin, CloudFront can use Origin Access Control to sign origin requests. CloudFront can still serve the intended public URL while direct bucket access stays blocked. WAF rules evaluate the HTTP request at the supported resource you associate with them. A rule that blocks a request changes whether it reaches the app. Cache configuration changes whether an allowed response can be served from the edge.

  1. Create ACM cert in us-east-1 for your domain.
  2. Create distribution; origin = S3 REST or ALB.
  3. Redirect HTTP to HTTPS; TLS 1.2+.
  4. Attach AWS WAF Web ACL with AWS managed core rule set.
  5. Add rate-based rule for login paths.
  6. Enable access logs to S3 for forensics.
# Cache mental model
# /static/*  → long TTL, compress, query strings ignored
# /api/*     → CachingDisabled, forward headers carefully
# /login     → WAF rate limit + no cache

5. Industries

  • Media: video/image edges
  • Fintech portals: WAF on login + CDN for static
  • Gov sites: managed rules + logging
  • SaaS marketing sites: price class to control cost

6. Lab: design behaviors on paper + console tour

On paper, write a path for your example logo and a path for a user-specific report. For each, name the selected origin, whether the response may be cached across users, and what HTTP result you expect for an unauthorized request. If you examine a sandbox distribution in the console, verify that the behavior path matches the origin you drew and that direct origin access behaves as intended.

Do this in ap-south-1 (Mumbai) unless your org forbids it. Set a budget alert first.

  1. List three path patterns for a sample app.
  2. Open CloudFront console (read-only) and note price classes.
  3. Open WAF console; read AWSManagedRulesCommonRuleSet description.
  4. Write which paths should CachingDisabled.
  5. Sketch IAM for log bucket.

Cleanup (stop billing)

  1. If you created a test distribution, disable then delete.
  2. Delete test Web ACL if empty.

7. Common errors and fixes

Each row is a real mistake students and juniors hit. Read the fix before you panic.

Error / symptomFix
ACM cert in Mumbai for CloudFrontCloudFront needs us-east-1 cert.
S3 website endpoint + OAC confusionPrefer S3 REST + OAC.
Cached HTML forever after deployVersion assets; invalidate carefully.
WAF blocks legitimate trafficTune; count mode first.
Choosing price class by name aloneCheck which edge geographies it covers, then compare price and observed latency.
No WAF on API via CloudFrontAttach Web ACL.
Forwarded all cookies → low cache hitForward only needed cookies.
Origin SG closed to CloudFrontUse managed prefix lists / OAC patterns.
Logged PII in query stringsRedact; avoid tokens in query.
Single origin no failoverAdd secondary origin group.

8. Troubleshooting checklist

  1. 403 from S3: OAC/bucket policy.
  2. Cert not listed: wrong ACM region.
  3. WAF 403: check sampled requests.
  4. High origin traffic: cache policy too strict.
  5. DNS not pointing to distribution domain.
  6. HTTP/2 or TLS issues: security policy.

9. FAQ

Q. CloudFront vs ALB only?

ALB is regional; CloudFront is global edge.

Q. Is WAF enough vs Shield?

WAF=L7 rules; Shield=DDoS; often combine.

Q. Invalidations cost?

Yes after free tier — prefer versioned objects.

Q. Bot Control?

Advanced WAF feature for bots — costs extra.

Q. Can WAF protect API Gateway?

Yes — associate Web ACL.

10. Interview Q&A

Q. What does CloudFront do?

CDN that caches at edges and fronts origins.

Q. Origin Access Control?

Lets CloudFront fetch private S3 securely.

Q. Why us-east-1 ACM?

CloudFront global cert store requirement.

Q. WAF rule types?

Managed, custom, rate-based, Bot Control.

Q. Cache hit ratio low?

Review TTLs, query strings, cookies, headers.

Q. Defense in depth?

Shield + WAF + least privilege origin.

11. Tips in English, Marathi and Hindi

Ravindra Bagale's Tip (EN)

Version static filenames (app.a1b2.js). Invalidations are a tax on poor naming.

Ravindra Bagale's Tip (MR)

Static filenames version करा. Invalidation = poor naming tax.

Tip (HI)

Static फाइल नाम version करो — invalidation खराब नामकरण का टैक्स है।

Try at home

  1. curl -I a CDN URL and read cache headers (x-cache).
  2. List five WAF managed rule groups from docs titles.
  3. Draw Viewer → Edge → Origin with WAF box.

In short

  1. CloudFront = CDN edges + origins + behaviors.
  2. WAF = HTTP rules on the distribution.
  3. HTTPS certs for CF usually from us-east-1 ACM.
  4. Tune cache and WAF with logs, not guesses.

Revision, interview and comparison cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Revision card: CloudFront + WAF. Origin in one region is slow worldwide and open to Layer-7 floods. CloudFront is a CDN (edge cache + HTTPS). AWS WAF filters HTTP requests with rules before they hit the origin. Create distribution → attach ACM cert → origin S3/ALB → associate WAF Web ACL with managed rules. Pune and London users hit edge caches; SQLi patterns get blocked by WAF before Mumbai origin.
Revision card: CloudFront + WAF · Download PNG · Open card page
Revision card: Cache behaviors and TTL. Wrong TTL serves stale prices or never caches and burns origin. Behaviors match path patterns; TTL and cache policies control what is stored at the edge. Static /assets/* long TTL; /api/* caching disabled or short; use versioned filenames for deploys. app.js?v=42 caches a year; HTML stays short so new deploys appear quickly.
Revision card: Cache behaviors and TTL · Download PNG · Open card page
Revision card: HTTPS and OAC. Public S3 websites bypass CloudFront and skip WAF/custom domains easily. Use ACM certs on CloudFront; Origin Access Control so only CloudFront reads the S3 origin. Block public S3; bucket policy allow only the distribution’s OAC; redirect HTTP→HTTPS. Direct s3.amazonaws.com URL returns 403; https://www.shop.in works via CloudFront.
Revision card: HTTPS and OAC · Download PNG · Open card page
Revision card: WAF managed rules. Hand-writing every SQLi regex is brittle; bots evolve weekly. AWS Managed Rules (Common Rule Set, Known Bad Inputs, IP reputation) give a strong baseline. Attach Core rule set in count mode first → review → switch to block; add rate limits on /login. Credential-stuffing spike hits rate-based rule; genuine users still browse the catalog.
Revision card: WAF managed rules · Download PNG · Open card page
Revision card: CDN cost basics. Unexpected bill from high data transfer or bad cache hit ratio. You pay for egress from edge and requests; higher cache hit ratio lowers origin and often cost. Compress, tune TTLs, use Price Class if you can skip costly regions, watch Free Tier carefully. Hit ratio rises from 40% to 85% after fixing Cache-Control on images; origin bandwidth drops.
Revision card: CDN cost basics · Download PNG · Open card page
Comparison: CloudFront + origin vs ALB only (no CDN). CloudFront + origin: Edge cache/HTTPS/WAF close to users worldwide. ALB only (no CDN): Users hit one region ALB every time.
Comparison: CloudFront + origin vs ALB only (no CDN) · Download PNG · Open card page
Comparison: AWS WAF vs Security group. AWS WAF: HTTP(S) rules: SQLi, bots, rate, geo. Security group: Allow/deny IP+port at ENI/ALB/instance.
Comparison: AWS WAF vs Security group · Download PNG · Open card page
Interview card: Why OAC for S3?. So only CloudFront can read the bucket; public S3 access stays blocked. Pairs with ACM HTTPS on the distribution. Direct S3 URL 403; www via CloudFront 200.
Interview card: Why OAC for S3? · Download PNG · Open card page
Interview card: WAF managed rules in one line?. AWS-written rule groups that block common exploits and bad inputs at the edge. Start in count mode, then block; add rate rules on login. SQLi in query string blocked before origin.
Interview card: WAF managed rules in one line? · Download PNG · Open card page
Interview card: How do you pick TTL?. Long for versioned static assets; short/none for personalized or API paths. Watch hit ratio and origin bytes after changes. /assets/* 1 year; /api/* no cache.
Interview card: How do you pick TTL? · Download PNG · Open card page
Interview card: S3 vs ALB as origin?. S3 (+OAC) for static; ALB/API for dynamic. Many apps use both with path behaviors. WAF still sits on the distribution for both. /static/* → S3; /api/* → ALB behavior.
Interview card: S3 vs ALB as origin? · Download PNG · Open card page

More cards: Ravi Sir's Cards.

Practise this chapter

Interview questions and answers

How can CloudFront return a public URL backed by a bucket that rejects direct public access?

Use CloudFront with a suitable S3 REST origin and Origin Access Control. Let the origin accept the intended CloudFront requests while keeping direct bucket reads restricted. Test both URLs so you can show which path provides access without sharing the origin.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. In the CloudFront request path, what is an origin?

Answer and explanation

B. The system that serves the content CloudFront needs to fetch

The origin can be a bucket, load balancer or other configured backend.

2. What does the cache key help CloudFront decide?

Answer and explanation

A. Whether different incoming requests may use the same cached response

Cache-key settings determine which request fields distinguish cache entries. Personalised content needs particular care.

3. Which mechanism evaluates web request rules for a supported CloudFront resource?

Answer and explanation

C. AWS WAF

WAF examines the HTTP request and applies its associated web ACL. The origin and cache have separate roles.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

Milan Furniture is a furniture business in Milan. Giulia Rossi looks after its IT and cloud work. They count money in EUR and run their AWS resources in the eu-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 16

Easy List distributions

See CDNs.

List CloudFront distributions.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy List web ACLs CLOUDFRONT

Scope CLOUDFRONT.

List web ACLs with scope CLOUDFRONT.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy List tags for distribution

Cost allocate CDN.

List tags for arn:aws:cloudfront::126154182628:distribution/E123EXAMPLE.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy List origin bucket objects

Check origin objects.

List objects in bucket milanfurniture-origin.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy Pick the certificate Region

A CloudFront distribution needs an ACM certificate for its viewer hostname. Enter the Region code where you request that certificate.

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Easy WAF is L7 — enter 7

Layer 7.

Which OSI layer does WAF mainly filter? Enter 7.

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Upgrade a viewer request

You want a browser requesting an HTTP page to automatically retry at its HTTPS URL. Which CloudFront viewer protocol policy should you select?

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Medium Get distribution

Inspect origins.

Get distribution E123EXAMPLE.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Get web ACL

See rules.

Get web ACL Name lab Id abcd Scope CLOUDFRONT.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium List certs us-east-1

CF certs region.

List certificates.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Allow WAF logging write

Forensics.

Allow logs:CreateLogStream on arn:aws:logs:eu-south-1:126154182628:log-group:/aws/waf/*.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium Behavior JSON

Path to cache policy.

JSON PathPattern /static/* TTL 86400.

  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.

Medium Price classes roughly 3

Cost control.

About how many CloudFront price class choices are commonly discussed (enter 3)?

  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard Create invalidation

Clear stale HTML carefully.

Create invalidation on E123EXAMPLE for path /index.html.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Deny non-CloudFront S3 get sketch

Bucket not public.

Deny s3:GetObject on arn:aws:s3:::milanfurniture-origin/* (Effect Deny).

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Hard Rate rule JSON

Login abuse.

JSON Limit 100 Path /login.

  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.