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

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.

What you'll learn 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?

Why do we need this?

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?

What is it?

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
Viewer → Edge → (cache hit | origin) → WAF checksGlobal users + attackers
Fast edges + filtered HTTP

4. How to set up (checklist)

How to do it (step by step)

  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

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

How to do it (step by step)

  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.
Price class All when India-onlyPick nearer classes to save.
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.

Practice Tasks

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

Why: See CDNs.

What to do: List CloudFront distributions.

How:
  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

Why: Scope CLOUDFRONT.

What to do: List web ACLs with scope CLOUDFRONT.

How:
  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

Why: Cost allocate CDN.

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

How:
  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

Why: Check origin objects.

What to do: List objects in bucket milanfurniture-origin.

How:
  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 ACM region for CF — enter 1 if us-east-1

Why: Global cert store.

What to do: Enter 1 if CloudFront ACM certs are usually from us-east-1.

How:
  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 WAF is L7 — enter 7

Why: Layer 7.

What to do: Which OSI layer does WAF mainly filter? Enter 7.

How:
  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 Force HTTPS — enter 1

Why: Redirect viewers.

What to do: Enter 1 if viewers should be redirected to HTTPS.

How:
  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.

Medium Get distribution

Why: Inspect origins.

What to do: Get distribution E123EXAMPLE.

How:
  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

Why: See rules.

What to do: Get web ACL Name lab Id abcd Scope CLOUDFRONT.

How:
  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

Why: CF certs region.

What to do: List certificates.

How:
  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

Why: Forensics.

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

How:
  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

Why: Path to cache policy.

What to do: JSON PathPattern /static/* TTL 86400.

How:
  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

Why: Cost control.

What to do: About how many CloudFront price class choices are commonly discussed (enter 3)?

How:
  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

Why: Clear stale HTML carefully.

What to do: Create invalidation on E123EXAMPLE for path /index.html.

How:
  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

Why: Bucket not public.

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

How:
  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

Why: Login abuse.

What to do: JSON Limit 100 Path /login.

How:
  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.