Choose the Delivery Chain Before Creating CDN Resources
A hostname has one edge, and the edge is whichever layer terminates TLS and answers from cache. Everything before it is a lookup and everything after it is a fetch. A chain where two layers terminate TLS and two layers cache has two owners for one decision, and the second owner is always discovered during an incident.
Four decisions travel with every request, and each one needs a single owner. DNS chooses an address, the edge proves a certificate and serves a cached body, the origin decides whether the request is allowed, and the application decides what the body says. Write the owner of each into the design before the first resource block exists.
| Decision | AWS-native chain | Cloudflare-fronted chain | If two layers claim it |
|---|---|---|---|
| DNS answer | Route 53 alias or CNAME | Cloudflare or external zone | two records, two ages, one stale |
| Viewer TLS | ACM certificate in us-east-1 | Cloudflare edge certificate | two certificates, one hostname |
| Cache key | CloudFront cache policy | Cloudflare cache rules plus CloudFront | a purge that fixes half the page |
| Origin admission | OAC, VPC origin or required header | the inner edge still decides | a header both layers rewrite |
| Client address | the visitor | the proxy address | logs and rate limits that name the wrong party |
Choose the chain from the requirement, not from the products already on the invoice. Three shapes cover most estates: Route 53 with ACM and CloudFront over a private origin; an external DNS zone with a DNS-only CNAME to CloudFront; and the Cloudflare proxy in front of CloudFront or an ALB. Each is defensible on its own terms, and mixing their roles is what produces the incident.
The AWS-native chain is the shortest and the easiest to explain. Route 53 answers with an alias to the distribution, ACM issues the viewer certificate, CloudFront terminates TLS and caches, and the origin admits only the distribution. One provider, one state, one place to look when a body is wrong.
Cloudflare as DNS only keeps one edge and adds a second API. The record is a DNS-only CNAME to the distribution, so CloudFront still terminates TLS and still owns the cache, while Cloudflare supplies zone management that does not depend on the registrar. That is a real split of responsibility rather than a duplicated one, and it is the pattern used later when an external owner holds the zone.
The Cloudflare proxy in front of CloudFront is a second edge, not an extra security layer. Both products terminate TLS for the same hostname, both cache, both rewrite headers, and both need purging. A stale body becomes an argument about which layer served it, and the pair of cache headers answers that only after you have learned to read both.
A second proxy also changes what CloudFront can see. Visitor addresses become proxy addresses, so logs, geographic restrictions and address-based rules describe the intermediate rather than the person. A trusted header convention or a rule at the outer edge restores the information, and either one is a policy surface that needs an owner.
A redundant layer is justified by a capability you cannot get below it. Keep the outer proxy for bot management, its ruleset language, a load balancer that spans clouds, or an identity layer in front of an internal application. If the requirement is a certificate and a cache for static objects, CloudFront already provides both.
Two caches mean two purge procedures that fail independently. Invalidating at CloudFront leaves the outer cache holding the old body until its TTL expires, and purging the outer edge leaves the inner cache untouched. A release runbook that names one of them ships stale content behind a green deployment.
Latency is the other bill. Each extra edge adds a handshake and a hop, and the outer layer helps only when it is materially closer to the visitor. Measure the chain you intend to run instead of assuming that more layers mean more speed.
The decision is a claim about ownership, so draw it before you build it. The first diagram separates the question "which products" from the question "who owns TLS, cache and DNS", and the table above is the version you can paste into a review.
%%{init: {'theme':'base','themeCSS':'.edgeLabel .label text, .edgeLabel text, .edgeLabel span { fill:#132440 !important; color:#132440 !important; }','themeVariables': {'primaryColor':'#16476A','primaryTextColor':'#ffffff','primaryBorderColor':'#132440','secondaryColor':'#3B9797','secondaryTextColor':'#ffffff','secondaryBorderColor':'#132440','tertiaryColor':'#c4873b','tertiaryTextColor':'#132440','tertiaryBorderColor':'#132440','lineColor':'#5a6e82','textColor':'#132440','titleColor':'#132440','mainBkg':'#16476A','nodeBorder':'#132440','clusterBkg':'#eef3f7','clusterBorder':'#132440','edgeLabelBackground':'#ffffff','actorBkg':'#16476A','actorTextColor':'#ffffff','actorBorder':'#132440','activationBkgColor':'#3B9797','signalColor':'#5a6e82','noteBkgColor':'#c4873b','noteTextColor':'#132440','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
H["Hostname under design"] --> Q1{"Needs a Cloudflare-only
capability?"}
Q1 -- "yes" --> P["Cloudflare proxy
owns TLS and cache"]
Q1 -- "no" --> Q2{"Who is authoritative
for the zone?"}
Q2 -- "Route 53" --> A["Alias record
works at the apex"]
Q2 -- "External provider" --> X["DNS-only CNAME
no proxy"]
A --> C["CloudFront
owns TLS and cache"]
X --> C
P --> Q3{"CloudFront also
in the path?"}
Q3 -- "yes" --> D["Two TLS terminators
two caches"]
Q3 -- "no" --> O["Origin behind
the proxy"]
C --> S["Private origin
OAC, VPC origin or header"]
S --> OK["One edge
one cache owner"]
D --> BAD["Purge and log ambiguity"]
BAD --> FIX["Remove a layer, or record
the owner of each decision"]
O --> OK
classDef primary fill:#132440,color:#ffffff,stroke:#0b172b;
classDef secondary fill:#16476A,color:#ffffff,stroke:#0f314a;
classDef ok fill:#3B9797,color:#ffffff,stroke:#2a6f6f;
classDef danger fill:#BF092F,color:#ffffff,stroke:#7a0420;
classDef warn fill:#c4873b,color:#132440,stroke:#8a5a1f;
class H,Q1,Q2,Q3 primary;
class A,X,C,S secondary;
class OK,FIX ok;
class P,O warn;
class D,BAD danger;
Read the diagram from the hostname down. A capability that only Cloudflare offers forces the outer proxy, and CloudFront then becomes an origin that must still present a certificate the proxy trusts. Without that requirement the zone owner decides the record shape, and CloudFront remains the only edge in the path.
Both chains end at a private origin, and that is not an accident. An edge is only useful if the origin cannot be reached around it. The next two sections take the two halves of that claim in order: certificate scope first, then origin access.
Map ACM Certificates and Validation to the Correct Scope
ACM is regional, and CloudFront reads one region only. A viewer certificate for a distribution must exist in us-east-1, whatever region the bucket, the load balancer or the team sits in. A regional consumer such as an ALB or a regional API Gateway endpoint wants the certificate in its own region instead.
The scope rule belongs to the consumer, not to the domain. The same hostname can have a certificate in several regions, each a separate object with its own ARN, validation record and renewal. Only the one the consumer reads matters; the others are inventory that will drift.
A provider alias makes the scope visible in review. Pin one provider configuration to us-east-1 and use it for the certificate and the distribution, while the origin keeps the default provider. The alias is not decoration: it is the only line in the configuration that states which region the viewer certificate lives in.
A regional mismatch fails at the consumer, not at the certificate. The request succeeds, validation succeeds, and the ARN looks healthy, then the distribution apply rejects it because the global service cannot see a certificate from another region. The plan does not warn you; the provider attached to the resource tells you.
A certificate also has an account. CloudFront uses the certificate's account as part of its domain-fronting defence, so a distribution will not accept a certificate borrowed from a neighbouring account. A cross-account edge needs the distribution in the certificate's account rather than a shared ARN.
Validation is a DNS proof, so the zone decides who can finish the request. With a Route 53 public zone, Terraform writes the validation record and the validation resource waits for issuance. With an external zone the same CNAME has to be written through that provider's API, and the apply now depends on a second credential.
The validation record must be served, not proxied. On Cloudflare the record is a CNAME with proxying off, because a proxied record answers with proxy addresses and the validation token is never seen. The record name also arrives with a trailing dot from the ACM API, which some providers reject or normalise into a different name unless it is trimmed.
There are two waits, and only one of them belongs to Terraform. The validation resource waits for the certificate to be issued, but the distribution still needs its own deployment time, and an edge can serve the previous certificate while a change propagates. A green apply is not the same as a served certificate.
Wildcards cover one label, and the apex is a separate name. A wildcard certificate authorises names one label below its parent but not the parent itself, and CloudFront requires the exact name or a wildcard at the same level in the SAN list. Listing the apex explicitly costs one entry and avoids a cutover surprise.
One certificate attaches to a distribution at a time. Adding a hostname later means reissuing the certificate and waiting for the distribution to pick it up, not attaching a second certificate. Keep the hostname list in a variable that the certificate and the aliases both read.
Renewal is managed; the validation record is still yours. ACM renews a DNS-validated certificate only while the validation CNAME remains resolvable, so pruning it as an unused record schedules a silent expiry months later. Keep validation records in the same state as the certificate and never clean them up separately.
| Consumer | Certificate region | Validation record lives | Wrong-scope symptom |
|---|---|---|---|
| CloudFront viewer certificate | us-east-1, always | the zone authoritative for the name | distribution apply rejects the ARN |
| ALB or NLB listener | the load balancer's region | the zone authoritative for the name | listener apply rejects the ARN |
| API Gateway, regional endpoint | the API's region | the zone authoritative for the name | domain name stays pending |
| API Gateway, edge-optimised | us-east-1 | the zone authoritative for the name | domain name stays pending |
| CloudFront to a custom origin | the origin's certificate store | no ACM validation record | 502 at the viewer |
Read the table by the consumer column, not by the domain. A team that owns a hostname often assumes it owns the certificate, and the two are separate objects with separate regions and separate validation records.
The failure branches are worth drawing because they happen in a fixed order. The flow below asks for the consumer, then the region, then the validation method, then the zone, and only then the SAN list.
%%{init: {'theme':'base','themeCSS':'.edgeLabel .label text, .edgeLabel text, .edgeLabel span { fill:#132440 !important; color:#132440 !important; }','themeVariables': {'primaryColor':'#16476A','primaryTextColor':'#ffffff','primaryBorderColor':'#132440','secondaryColor':'#3B9797','secondaryTextColor':'#ffffff','secondaryBorderColor':'#132440','tertiaryColor':'#c4873b','tertiaryTextColor':'#132440','tertiaryBorderColor':'#132440','lineColor':'#5a6e82','textColor':'#132440','titleColor':'#132440','mainBkg':'#16476A','nodeBorder':'#132440','clusterBkg':'#eef3f7','clusterBorder':'#132440','edgeLabelBackground':'#ffffff','actorBkg':'#16476A','actorTextColor':'#ffffff','actorBorder':'#132440','activationBkgColor':'#3B9797','signalColor':'#5a6e82','noteBkgColor':'#c4873b','noteTextColor':'#132440','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
R["Certificate request
domain plus SANs"] --> Q1{"Which consumer?"}
Q1 -- "CloudFront" --> R1{"Requested in
us-east-1?"}
Q1 -- "ALB or regional API" --> R2{"Same region as
the endpoint?"}
R1 -- "no" --> F1["Consumer apply rejects
the certificate"]
R2 -- "no" --> F2["Listener apply rejects
the certificate"]
R1 -- "yes" --> V{"Validation method"}
R2 -- "yes" --> V
V -- "email" --> E["Manual approval
no Terraform wait"]
V -- "DNS" --> Z{"Zone authoritative
for the name?"}
Z -- "Route 53" --> T["Terraform writes
the CNAME"]
Z -- "Cloudflare" --> X["External provider
writes the CNAME"]
X --> PX{"Record proxied?"}
PX -- "yes" --> F3["Token never resolves
certificate stays pending"]
PX -- "no" --> I["Certificate issued"]
T --> I
E --> I
I --> S{"Apex in the
SAN list?"}
S -- "no" --> F4["Apex fails at the edge
wildcard covers one label"]
S -- "yes" --> OK["Attach to the consumer"]
classDef primary fill:#132440,color:#ffffff,stroke:#0b172b;
classDef secondary fill:#16476A,color:#ffffff,stroke:#0f314a;
classDef ok fill:#3B9797,color:#ffffff,stroke:#2a6f6f;
classDef danger fill:#BF092F,color:#ffffff,stroke:#7a0420;
classDef warn fill:#c4873b,color:#132440,stroke:#8a5a1f;
class R,Q1,R1,R2,V,Z,PX,S primary;
class T,X,I secondary;
class OK ok;
class E warn;
class F1,F2,F3,F4 danger;
The two red branches at the top are the cheap ones to fix. A wrong region is a provider alias, and a wrong consumer is a design decision; both are visible in a plan before anything is created. The expensive branches are lower down, where a pending certificate blocks an apply and the record looks correct in the console.
Scope is also a state boundary. Two roots that both request a certificate for the same hostname produce two objects, two renewals and two chances to attach the wrong ARN. Name one owner per hostname and let the other root read the value as an input.
Version baseline. Behaviour was checked against Terraform CLI 1.17.1, hashicorp/aws 6.72.0 and cloudflare/cloudflare 5.28.1 on 6 November 2026. Where a product document outruns the provider schema, the schema decides what Terraform can express and the API reference decides what the service does.
Configure CloudFront and Origin Access for Private Content
The distribution is the only public door, and the origin must have no other. A private bucket with a correct bucket policy is still public if a second policy, an ACL or a website endpoint admits the world. Origin access is a property of the pair, not of the distribution alone.
Current origin access is OAC, not the legacy identity. An origin access control is created for one origin type with signing behaviour always and signature version four, and the distribution references it by origin_access_control_id. The older identity cannot serve KMS-encrypted objects, opt-in regions or write requests.
OAC signs the request; the bucket policy authorises the distribution. The statement allows the CloudFront service principal to read objects and conditions on the distribution ARN, so another distribution in the same account cannot reuse the bucket. Public access block stays on all four switches, including the two that ignore existing ACLs.
The order of the references is the dependency. The bucket policy needs the distribution ARN and the distribution needs the origin access control, so both are expression edges and Terraform orders them without help. Adding a manual dependency between the policy and the distribution creates a cycle rather than safety.
Encrypted objects need a third grant. When the bucket uses SSE-KMS, the CloudFront principal also needs decrypt permission in the key policy, conditioned on the same distribution ARN. Without it the distribution receives a 403 from S3 and passes it on, which reads like a path or cache problem at the viewer.
An S3 website endpoint cannot use origin access control. Website endpoints are HTTP-only and public by design, so CloudFront has to treat one as a custom origin and the bucket stays reachable directly. For private content the website endpoint is the wrong origin, not a configuration to patch.
A load balancer origin needs a different proof. A custom origin can require a secret header that the listener verifies while the security group allows only the CloudFront origin-facing prefix list, or it can sit behind a VPC origin with no public listener at all. The header is a shared secret; the VPC origin removes the network path.
Cache policy is the primary surface, and forwarding is the exception. A behaviour takes a cache policy and an origin request policy, while the older forwarded-values block is deprecated and easy to copy from old examples. Resolve managed policies by name through a data source instead of pasting identifiers into configuration.
The cache key is the security boundary for personalised content. If cookies, the Authorization header or a user identifier enter the cache key, every distinct value becomes an object, and one policy mistake serves a response to the wrong person. Personalised paths belong in a behaviour with caching disabled and forwarding enabled.
| Path | Cache policy | Forwarding | What must never happen |
|---|---|---|---|
| static assets | caching optimised | none | HTML served under an asset key |
| HTML entry point | caching optimised, short TTL | none | a stale shell after a release |
| API routes | caching disabled | all viewer except host | a cached personalised body |
| account pages | caching disabled | all viewer | a shared cache storing a session |
Protocol settings belong in the same change. Redirect viewers from HTTP to HTTPS, set the minimum protocol version to TLS 1.2 or later, enable HTTP/2 and HTTP/3, and compress text. A response headers policy adds HSTS and the security headers the origin should not have to repeat.
A distribution is slow to change and slower to delete. Deployment takes minutes, deletion is blocked until the distribution is disabled, and the retain flag trades cleanup for safety. Plan a teardown as two operations rather than one destroy at the end of a lab.
Invalidation is a repair, not a release step. A wildcard invalidation clears the edge for a path that should have been versioned, and it does not fix a cache key that contains the wrong header. Versioned asset names plus a short HTML TTL remove most of the need for it.
The origin is the part that survives a bad edge change. A bucket with public access blocked and a policy that names one distribution can be re-fronted by a new distribution without touching the data. That is the property worth designing for, because it makes the edge replaceable.
Manage Route 53 Records or External DNS Deliberately
DNS has four jobs, and they are usually held by three different systems. The registrar holds the delegation, the authoritative zone holds the records, the certificate authority holds a validation record, and the application holds the name that visitors use. Naming the owner of each job is the whole discipline.
A hosted zone is not a delegation. A Route 53 zone resource creates the zone and returns four name servers; the registrar still has to point at them. When the domain is registered through Route 53 the delegation follows, and when it is registered anywhere else that step is manual, out of band and easy to miss during a rebuild.
A replaced zone is a new delegation. Destroying and recreating a hosted zone produces different name servers, so every record is correct and the name still does not resolve. Protect the zone with a lifecycle guard, or accept that zone replacement is a registrar change with a human in the loop.
The apex cannot be a CNAME in ordinary DNS. Route 53 solves it with an alias record that names the distribution's domain and zone, and CloudFront supplies its own zone identifier through the distribution rather than through a constant. Cloudflare solves the same problem with CNAME flattening, which is why a CNAME at the apex is legal there and not in Route 53.
Alias and CNAME records fail differently at cutover. An alias follows the target with no TTL of its own, so a change appears as soon as the distribution deploys. A CNAME is bounded by its TTL and by resolvers that ignore it, so lower the TTL a day before the change and raise it again afterwards.
One record, one owner. A name declared in both Route 53 and Cloudflare produces two answers with different ages and a plan that keeps proposing a change the other tool reverts. Record the owning root in a comment and delete the duplicate rather than adding an overwrite flag.
A proxied record in an external zone bypasses the distribution. When Cloudflare is authoritative and the record is proxied, the answer is a proxy address and CloudFront is never contacted, even though the alias in AWS still exists. The certificate at CloudFront then serves nothing and the cache you tuned is unused.
A subdomain can have a different zone owner from its parent. Delegating a subdomain to Cloudflare while the apex stays in Route 53 is legitimate, and it changes which zone must hold the validation record. That record belongs to the zone authoritative for the name being validated, not to the zone holding the application record.
Records outlive the resources they point at. A destroyed distribution leaves a CNAME aimed at a hostname that no longer exists, and a deleted bucket behind a website endpoint leaves a name another account can claim. Delete the record in the same change as its target, or publish a deliberate placeholder answer.
TTL is a rollback control, not a performance knob. A five-minute TTL gives a five-minute rollback window on an external record and no window at all on a client that cached the old answer. Set it before the change, not during the incident.
| Record | Authoritative owner | Terraform surface | Failure signature |
|---|---|---|---|
| apex A and AAAA | Route 53 | alias to the distribution | nothing resolves after a zone rebuild |
| www CNAME | external provider | DNS-only record, proxy off | one answer, but the old edge serves it |
| validation CNAME | zone owning the name | provider-specific record | certificate stays pending |
| NS delegation | registrar | outside Terraform, or the domain resource | zone exists and receives no queries |
The failure paths share one shape: a correct record in the wrong zone. The diagram below keeps the intended chain on top and the three ways it breaks underneath, because each one produces a different symptom and the same feeling of having done everything right.
A rebuild is the test that exposes all three. Destroy the lab zone in a branch, apply it again, and resolve the name before touching the registrar. If the answer changes, you have found the delegation dependency; if it does not, the zone was never authoritative in the first place.
Prove TLS, Cache Bypass and Origin Protection
Four checks answer four different questions, and none substitutes for another. The viewer certificate says the name is trusted at the edge, DNS says which edge answered, the response headers say who produced the body, and a direct origin request says whether the private path is actually private.
Start with the certificate the edge really serves. Connect with the public hostname and SNI, then read issuer, SAN and dates. An ACM issuer is what a distribution with an alias should present; a default CloudFront certificate means the hostname is missing from the alias list or the DNS name differs from the one you tested.
Then confirm which object answered. A lookup should return the distribution's name through the alias, and the response should carry a CloudFront request identifier. If the answer is a proxy address, the record is proxied and every later measurement describes a different chain.
Cache behaviour is proven by repetition, not by reading configuration. Two identical requests should show a miss followed by a hit, and a changed path should produce a new object. A second miss on a static asset means the key is too wide; one visitor receiving another visitor's page means it is too narrow.
Personalised responses are the negative case worth designing for. A page carrying a session cookie, a user name or a balance must never be cached, and a behaviour that forwards cookies while caching remains enabled will do exactly that under load. Test it with two sessions and compare the bodies rather than the headers.
Origin protection is proven from outside the edge. Request the bucket's regional endpoint or the load balancer address with the public hostname as SNI, and expect a refusal. An unsigned S3 request returns 403, a listener that requires a header returns 403 or 404, and a VPC origin has no public address to reach.
The result that matters most is a 200. A direct request returning the object means the bucket policy, the security group or the listener still admits the world, and no edge setting compensates for it. Record the result with the source network, because a rule that blocks your laptop is not a rule that blocks the internet.
Read the edge's own status codes as evidence rather than as noise. The code names the layer that refused, which is faster than inspecting three configurations in parallel.
| Response | Layer that decided | First check |
|---|---|---|
| 403 from the edge | viewer authorisation | signed URL or WAF rule |
| 403 from the origin | bucket policy or OAC | SourceArn condition and public access block |
| 502 from a custom origin | origin TLS or hostname | certificate SAN and origin domain |
| 200 on the direct path | origin exposure | bucket policy, security group, listener |
Verify the negative cases before the release, not after it. The lab below walks the chain in the order a request travels and records one piece of evidence per layer, so a failure points at a section rather than at the whole design.
Prove TLS, Cache Bypass and Origin Protection
Status: design-only. Use a lab account, one lab hostname and placeholder values for every address and identifier. No production distribution, customer hostname or private key is needed.
Prerequisites. Terraform CLI 1.17.1, hashicorp/aws 6.72.0, and cloudflare/cloudflare 5.28.1 if the zone is external. You also need curl, openssl, dig and a private bucket or a lab load balancer with a health endpoint.
Steps.
- Resolve the hostname and record which name servers answer the zone.
- Read the served certificate with SNI and note issuer, SAN and dates.
- Request one asset twice and record both cache headers.
- Request the same asset with an extra query parameter and compare the body.
- Request the origin directly with the public hostname as SNI.
- Open the personalised path from two sessions and compare the bodies.
| Check | Evidence to collect | Pass condition | Failure signal |
|---|---|---|---|
| Delegation | dig +short NS <zone> | the zone Terraform created | a different provider answers |
| Viewer TLS | openssl s_client with SNI | ACM issuer, matching SAN | default CloudFront certificate |
| Cache | curl -sSI twice on one asset | miss then hit on the second | hit on the first, or two misses |
| Bypass | curl -sSI with an added query | same or new object as designed | a personalised body under a shared key |
| Origin | curl -sS to the bucket endpoint | 403 or no route | 200 with the object body |
| Session | curl -sS with two cookie jars | different bodies, no shared cache | one user's page for both |
Expected observation. The first asset request reports a miss and the second a hit, while the account path reports no cache at all. The certificate names the hostname and is issued by ACM. The direct origin request fails, and the failure is a refusal rather than a timeout. The two sessions return different bodies even when the requests are made within the same second.
Failure check. The lab fails if the direct origin returns 200, if the recorded issuer is the CloudFront default, if both sessions receive identical HTML, or if a query parameter changes the object on an endpoint that should ignore it. It also fails if the only evidence for the certificate is a padlock in a browser.
Cost and cleanup. One bucket or one load balancer, one distribution and one hostname. Destroy the distribution before the bucket, remove the DNS record in the same change, and delete the validation record only when the certificate is genuinely retired. Keep the distribution disabled for a short window if you need a rollback.
Evidence to keep. The DNS answer, the certificate fields, both cache header pairs, the direct-origin result with its source network, and the two session bodies with identifiers removed. Store the evidence with the change record, not with the credentials.
Close with the questions the configuration has to answer. Which layer terminates TLS, which zone is authoritative, which cache key decides the body, and who can reach the origin without the edge? A single answer for each is the difference between an edge you can debug and a chain you can only restart.
Next in the Series
In AWS Governance: IAM Boundaries, Encryption, Logs and Cost Ownership, we'll explore Combine supported security and telemetry resources with least privilege and accountable operational evidence.