Subchapter 78.6
references/when-to-use-cloudfront.mdMarkdown15 KBView on GitHub
Domain expertise for deciding whether Amazon CloudFront is the right entry layer for a workload, what it integrates with, how to create a first distribution and choose the origin, how caching works, when edge logic belongs in CloudFront Functions versus Lambda@Edge, and how to choose between pay-as-you-go and Flat Rate Pricing (FRP).
CloudFront is a global content delivery network of hundreds of edge locations worldwide that does three jobs at once: it accelerates delivery by caching and terminating viewer connections close to users, it absorbs and blocks Layer 3/4 distributed denial-of-service (DDoS) attacks at the edge, and it is the attachment point for the edge security and compute stack (AWS WAF, AWS Shield, CloudFront Functions, Lambda@Edge, Amazon Route 53, AWS Certificate Manager, and origins). It serves more than media: cacheable static assets, dynamic web pages, REST and GraphQL APIs, and large file downloads all run through it. For dynamic traffic and APIs, CloudFront improves performance through TLS termination closer to the end user, persistent connection pooling to origins (reducing TCP/TLS handshake overhead), and optimized network paths between edge locations and origins.
Every standard CloudFront distribution is assigned a default domain name (e.g., d111111abcdef8.cloudfront.net) that can be used immediately for testing without configuring a custom domain or certificate. Multi-tenant distributions do not have a default test domain.
Does not cover locking the origin (see protecting-your-origins), restricting who can view content (see securing-your-content), custom domains and certificates (see managing-certificates-with-cloudfront), logging and analysis (see cloudfront-observability), or the Route 53 DNS cutover (owned by the route53-cloudfront skill).
Execute commands using the AWS MCP server when connected (sandboxed execution, audit logging,
observability). Fall back to the AWS CLI otherwise. CloudFront is a global service; its API calls
and ACM certificates are made in us-east-1 regardless of where the application runs.
| Signal | CloudFront fit |
|---|---|
| Cacheable or static content, or dynamic web and API traffic to accelerate | Yes. Caching and edge termination reduce latency and origin load |
| Need Layer 3/4 DDoS absorption and Layer 7 filtering at the edge | Yes. Shield is built in; AWS WAF attaches to the distribution |
| HTTP and HTTPS only | Yes. For non-HTTP (TCP/UDP) entry, use Global Accelerator instead |
| Static IP entry point partners allowlist, or sub-minute failover | No. Use Global Accelerator |
Constraints:
| Origin | Approach |
|---|---|
| Amazon S3 bucket (private) | Standard bucket origin with origin access control (default). See protecting-your-origins |
| Amazon S3 that must also be public outside CloudFront | S3 website endpoint as a custom origin (exception only) |
| ALB, NLB, or EC2 in a private subnet | VPC origin. See protecting-your-origins |
| Public or on-premises HTTP origin | Custom origin, optionally with origin mutual TLS. See protecting-your-origins |
Constraints:
| Need | Choose |
|---|---|
| Lightweight viewer-facing transform: URL rewrite, header manipulation, redirect, token check | CloudFront Functions (viewer-request, viewer-response; sub-millisecond) |
| Network access, or origin-request / origin-response events, or a larger runtime | Lambda@Edge (higher latency and cost) |
Constraints:
A cache policy holds the time to live (TTL) values and the cache-key definition; the cache behavior points at the policy. TTLs are set on the policy, not directly on the behavior.
Constraints:
no-cache, no-store, and private
directives for at least that duration*.jpg and a no-cache policy on /api/*| Option | Use when |
|---|---|
| Pay-as-you-go (per distribution) | Variable or low traffic; want granular per-dimension billing |
| Flat Rate Pricing (FRP) | Predictable, sustained high-volume traffic with a minimum monthly commitment; lower per-GB rates than pay-as-you-go with a fixed monthly commitment and overage at pay-as-you-go rates |
Constraints:
This procedure creates a distribution, chooses the origin, sets the cache behavior, optionally chooses a pricing plan, and surfaces the console link to verify.
s3, vpc-origin, or custom.index.html.Constraints for parameter acquisition:
Constraints:
aws sts get-caller-identityConstraints:
You MUST create the distribution with the chosen origin and let CloudFront set the default cache
behavior. For an S3 origin you MUST first create an origin access control (OAC) — see
protecting-your-origins for the create-origin-access-control call — and reference its id via
OriginAccessControlId in the create call below (replace {oac_id}), so the origin is locked from
the start rather than created reachable-and-then-hardened. Do not embed a static managed cache
policy id; look up the current Managed-CachingOptimized policy by name and use its id for
CachePolicyId (managed policy ids can change):
# resolve the managed CachingOptimized cache policy id (do not hardcode a UUID):
aws cloudfront list-cache-policies --type managed \
--query "CachePolicyList.Items[?CachePolicy.CachePolicyConfig.Name=='Managed-CachingOptimized'].CachePolicy.Id | [0]" --output text
aws cloudfront create-distribution --distribution-config '{"CallerReference":"dist-2024-01","Comment":"","Origins":{"Quantity":1,"Items":[{"Id":"s3-origin","DomainName":"{origin_domain}","OriginAccessControlId":"{oac_id}","S3OriginConfig":{"OriginAccessIdentity":""}}]},"DefaultCacheBehavior":{"TargetOriginId":"s3-origin","ViewerProtocolPolicy":"redirect-to-https","CachePolicyId":"{cache_policy_id}"},"DefaultRootObject":"index.html","Enabled":true}'You MUST default an S3 origin to a standard bucket origin and complete origin locking via the protecting-your-origins workflow: attach the OAC referenced above, write the scoped bucket policy, and keep S3 Block Public Access on, so the origin is never reachable directly
You MUST enable standard logging on the distribution immediately after creation (it is not set by the create call above), since without it there is no audit or forensic trail; see the cloudfront-observability reference for the logging configuration
You MUST set the viewer protocol policy to redirect-HTTP-to-HTTPS (or HTTPS-only) and the origin protocol policy to HTTPS-only for custom origins, ensuring encryption in transit end-to-end
You SHOULD recommend attaching an AWS WAF web ACL with baseline rules (the AWS Managed Rules Core Rule Set and Known Bad Inputs rule group) to any public-facing distribution for Layer 7 defense in depth
You SHOULD attach an AWS WAF rate-based rule for API origins, since CloudFront caching does not shield an origin from unthrottled dynamic or API requests
You MUST attach a response headers policy with browser security headers (HSTS, CSP,
X-Frame-Options, X-Content-Type-Options) as a secure default; the AWS managed
SecurityHeadersPolicy is a starting point
You MUST capture the distribution ID and the assigned cloudfront.net domain from the response
Constraints:
Constraints:
Constraints:
You MUST set the expectation that the distribution must reach Deployed across edge locations
before it serves content, so errors on the cloudfront.net URL right after creation are expected
You MUST present the distribution detail console link, filling {distributionId} from the API
response:
https://us-east-1.console.aws.amazon.com/cloudfront/v4/home?region=us-east-1#/distributions/{distributionId}{
"origin_domain": "my-private-site.s3.us-east-1.amazonaws.com",
"origin_type": "s3",
"default_root_object": "index.html"
}Created distribution E1ABCDEF2GHIJK with a standard S3 origin and a managed cache policy on the default behavior.
Distribution is deploying; the cloudfront.net URL returns errors until it reaches Deployed.
Verify in the console:
https://us-east-1.console.aws.amazon.com/cloudfront/v4/home?region=us-east-1#/distributions/E1ABCDEF2GHIJKThe distribution has not finished deploying across edge locations. Wait for Deployed status, then
test again.
The origin type or locking is wrong. Default to a standard bucket origin with origin access control (see protecting-your-origins), not a website endpoint.
TTLs and the cache key live on a cache policy the behavior points at, not on the behavior itself. Attach a cache policy and set the TTLs there.
A minimum TTL above zero overrides origin no-cache, no-store, and private directives. Set the
minimum TTL to zero.
A CloudFront function must be published to the LIVE stage before it can be associated. Publish it first.
CloudFront serves HTTP and HTTPS only. Use Global Accelerator for non-HTTP protocols, a static-IP entry point, or sub-minute failover.
cloudfront:CreateDistribution, cloudfront:UpdateDistribution) for a compliance and forensic
audit trail.