Skip to Content
Asterrr's Handbook
3 · Infrastructure security

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.

HTTPSinspectOAC signedheadersViewersWAF web ACLCloudFrontS3 bucket policyEdge function
CloudFront asks WAF to inspect each viewer request. It then signs requests to S3 with origin access control, so the bucket policy admits only that distribution.

Pick the control

GoalFeature
Users must not bypass CloudFront to read S3Origin access control (OAC)
Only paying or logged-in users get filesSigned URLs or signed cookies
Block or allow whole countriesCloudFront geographic restrictions
Block part of a site by country or by cityWAF geo match, or a third-party geolocation service plus signed URLs
Add HSTS, CSP, X-Frame-Options and similar headersResponse headers policy
Encrypt chosen form fields so only one backend can read themField-level encryption
Keep ALB or EC2 origins off the internetVPC origins
Layer 7 filtering and rate limitsAWS WAF on the distribution
Network and transport DDoS, extra response helpShield

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.

ItemWhat to remember
Versus OAIOAC supports SSE-KMS, all Regions including opt-in Regions, and PUT and DELETE. The legacy origin access identity does not. AWS recommends OAC
SSE-KMSAdd the distribution to the KMS key policy for kms:Decrypt, kms:Encrypt and kms:GenerateDataKey*, again using AWS:SourceArn
Signing behaviorAlways sign (recommended), never sign, or do not override the viewer Authorization header
Object OwnershipMust be bucket owner enforced (default), or bucket owner preferred if you need ACLs
Not supportedS3 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

OriginHow to force traffic through CloudFront
S3 bucketOrigin 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 URLOAC with the function URL's auth type set to AWS_IAM
Private ALB, NLB or EC2VPC origins: the origin stays in private subnets with no internet-facing endpoint
Public ALBSecurity 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

UseWhen
Signed URLOne file at a time, or a client that cannot use cookies
Signed cookieMany 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

Scenario · choose 2
A static documentation site is hosted in an S3 bucket encrypted with SSE-KMS using a customer managed key, and served through CloudFront. The auditors found that objects can also be fetched from the bucket's S3 endpoint. The security engineer must make the bucket reachable only through CloudFront. Which TWO steps are required?

Further reading

On this page