CloudFront Edge Security
Protecting S3 and custom origins behind CloudFront with OAC, signed URLs and cookies, response headers policies, geo restriction and field-level encryption.
Exam tasks: 3.1 (edge protection with CloudFront headers)
The decision: how do you keep viewers and attackers off the origin and add security headers without touching the application?
CloudFront is the edge layer for public web content. Security comes from keeping the origin reachable only through CloudFront, restricting who may fetch content, and adding WAF and Shield at the edge.
Pick the control
| Goal | Feature |
|---|---|
| Users must not bypass CloudFront to read S3 | Origin access control (OAC) |
| Only paying or logged-in users get files | Signed URLs or signed cookies |
| Block or allow whole countries | CloudFront geographic restrictions |
| Block part of a site by country or by city | WAF geo match, or a third-party geolocation service plus signed URLs |
| Add HSTS, CSP, X-Frame-Options and similar headers | Response headers policy |
| Encrypt chosen form fields so only one backend can read them | Field-level encryption |
| Keep ALB or EC2 origins off the internet | VPC origins |
| Layer 7 filtering and rate limits | AWS WAF on the distribution |
| Network and transport DDoS, extra response help | Shield |
Origin access control
OAC makes CloudFront sign its origin requests with SigV4. The bucket policy allows the service principal cloudfront.amazonaws.com with an AWS:SourceArn condition naming the distribution. Direct S3 URLs then fail, so signed URLs and cookies cannot be bypassed.
| Item | What to remember |
|---|---|
| Versus OAI | OAC supports SSE-KMS, all Regions including opt-in Regions, and PUT and DELETE. The legacy origin access identity does not. AWS recommends OAC |
| SSE-KMS | Add the distribution to the KMS key policy for kms:Decrypt, kms:Encrypt and kms:GenerateDataKey*, again using AWS:SourceArn |
| Signing behavior | Always sign (recommended), never sign, or do not override the viewer Authorization header |
| Object Ownership | Must be bucket owner enforced (default), or bucket owner preferred if you need ACLs |
| Not supported | S3 buckets configured as a website endpoint. Use a custom origin instead |
Steps. Create the OAC, attach it to the S3 origin of the distribution, then update the bucket policy.
Exam trap
A bucket configured as a static website endpoint cannot use OAC or OAI. To restrict it, use a custom origin header secret or move to the REST endpoint with OAC.
Keeping viewers off the origin
| Origin | How to force traffic through CloudFront |
|---|---|
| S3 bucket | Origin access control (OAC) plus a bucket policy that allows cloudfront.amazonaws.com with AWS:SourceArn set to the distribution. Keep Block Public Access on. Works with SSE-KMS if the key policy allows the CloudFront service principal |
| Lambda function URL | OAC with the function URL's auth type set to AWS_IAM |
| Private ALB, NLB or EC2 | VPC origins: the origin stays in private subnets with no internet-facing endpoint |
| Public ALB | Security group allows only the CloudFront managed prefix list, and a WAF rule (or listener rule) requires a secret custom header that CloudFront adds. Rotate the secret with Secrets Manager |
Legacy: use origin access control (OAC) instead
Origin access identity (OAI) doesn't support SSE-KMS objects, S3 in all Regions, or requests other than GET. Migrate OAI distributions to OAC and update the bucket policy to use the service principal.
- Signed URLs (one object) and signed cookies (many objects) restrict who can fetch content. Use trusted key groups, not the root-user CloudFront key pair.
- Geographic restrictions on the distribution allow or block whole countries. Use WAF geo match instead when you need exceptions, labels or combination with other conditions.
- Set a modern TLS security policy for viewers, redirect HTTP to HTTPS, and use HTTPS to the origin.
Signed URLs versus signed cookies
| Use | When |
|---|---|
| Signed URL | One file at a time, or a client that cannot use cookies |
| Signed cookie | Many files, such as every segment of an HLS video or a subscriber area, or you do not want to change URLs |
- Signers are trusted key groups, which AWS recommends.
- A request with a signed URL is judged by the URL alone, even when cookies are also present.
- Add a restriction to the origin too, otherwise viewers use the origin URL and skip the signature.
Response headers policy
A response headers policy adds or removes HTTP response headers with no code and no origin change. It supports security headers such as Strict-Transport-Security, Content-Security-Policy and X-Frame-Options, plus CORS, Cache-Control and Server-Timing, and can remove headers such as X-Powered-By. AWS provides managed policies. It applies to cached and origin responses.
S3 only returns custom metadata headers prefixed x-amz-meta-, which is why headers used to be injected by Lambda@Edge.
Header options
- Managed policies such as SecurityHeadersPolicy cover the common set. Create a custom policy for your own CSP.
- CloudFront Functions are the lightweight option for custom header logic. Use Lambda@Edge only when you need the network, the body or longer run time.
Legacy: use response headers policies instead
Adding security headers with a Lambda@Edge origin-response function was the standard pattern before response headers policies existed. It still works, but it costs more and adds code you must maintain.
Field-level encryption
- Encrypts up to 10 named fields of a POST body at the edge with your RSA public key (2048-bit key).
- The fields stay ciphertext through the whole backend. Only a component holding the private key can decrypt.
- Needs HTTPS viewer policy, POST and PUT allowed, and an origin that supports chunked encoding.
VPC origins
CloudFront reaches an ALB, NLB or EC2 instance in a private subnet through a service-managed ENI. The origin security group allows the CloudFront managed prefix list or the service-managed group. The VPC needs an internet gateway attached. Gateway Load Balancers are not supported, and Lambda@Edge origin triggers do not work with VPC origins.
Geographic restriction
An allow list or block list applies per distribution and by country. Blocked viewers get HTTP 403. If CloudFront cannot place the viewer, it serves the content.
Exam signal
"Users bypass signed URLs by hitting the S3 URL": OAC with a bucket policy. "Add security headers without touching the app": response headers policy. "Protect the credit card field end to end": field-level encryption.
Viewer TLS
Set a modern TLS security policy for viewers, redirect HTTP to HTTPS, and use HTTPS to the origin.
Scenarios
OAC signs CloudFront's requests to S3, and the bucket policy then allows only that distribution. Because the objects use SSE-KMS, the key policy must also let CloudFront decrypt, which OAC supports. OAI doesn't support SSE-KMS and is legacy. CORS is a browser mechanism and doesn't stop direct requests. Block Public Access should stay on, since OAC access isn't public access.
Related
Further reading
AWS Shield and DDoS Resilience
Shield Standard versus Shield Advanced, which resources each protects, and the architecture patterns that make an application resilient to DDoS.
S3 Cross-Origin Resource Sharing
How S3 CORS rules tell browsers which origins may call a bucket, and what CORS does not protect.