Lesson 5 of 6 Edge Protection and Application Security

Protecting Origins Behind Amazon CloudFront

This lesson explains how origin protection ensures every request reaches the origin through your CloudFront distribution, where caching, WAF rules, Shield and edge logic all apply.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how S3 and ALB origins are locked down.
  • Explain why origin access control and VPC origins matter.
  • Describe how headers and policies harden the edge.

Locking down the origin

An origin that accepts traffic from anywhere lets attackers bypass CloudFront, and with it WAF rules, geographic restrictions and signed URLs. For an S3 bucket, origin access control (OAC) signs CloudFront's requests. The bucket policy allows only the CloudFront service principal for that distribution, and Block Public Access stays on. For an ALB, NLB or EC2 instance in private subnets, CloudFront VPC origins reach it directly, so it needs no public IP or internet-facing load balancer at all.

For an internet-facing ALB or another custom origin, the security group allows only the CloudFront origin-facing managed prefix list. CloudFront also adds a secret custom header, which an ALB rule or WAF checks, so requests from other distributions are rejected too. A Lambda function URL uses OAC with IAM authentication on the URL. The following table summarises the protections.

OriginHow to accept only CloudFront traffic
S3 bucketOrigin access control (OAC) with a bucket policy allowing the CloudFront service principal for that distribution; keep Block Public Access on
ALB, NLB or EC2 in private subnetsCloudFront VPC origins: the origin needs no public IP or internet-facing load balancer
Internet-facing ALB or custom originSecurity group allowing only the CloudFront origin-facing managed prefix list, plus a secret custom header that the ALB rule or WAF checks
Lambda function URLOAC with IAM authentication on the function URL

Why each detail matters

Origin access control replaces the legacy origin access identity. It signs CloudFront's requests with SigV4, works in all Regions, and supports objects encrypted with SSE-KMS, provided the KMS key policy allows CloudFront, as well as PUT requests. The bucket policy's condition on AWS:SourceArn limits access to your distribution; without it, any CloudFront distribution, including one an attacker creates, could read the bucket.

For custom origins, the managed prefix list alone is not enough for the same reason: it admits traffic from every CloudFront distribution. The secret header ties the origin to your distribution, and it should be rotated like any secret. VPC origins remove the public origin entirely, which makes them the strongest option wherever they are supported.

Hardening the edge

Once all traffic passes through the distribution, the edge is the place to apply further protection. Response headers policies add security headers, such as HTTP Strict Transport Security and Content-Security-Policy, without changing the application. CloudFront Functions run lightweight logic, such as validating a token or rewriting a URL, on every request with sub-millisecond start-up, while Lambda@Edge handles heavier logic that needs network calls or runs on origin events.

HTTPS is required both between viewers and CloudFront and between CloudFront and the origin, with a minimum TLS version, and AWS WAF is attached to the distribution so that malicious requests are filtered at the edge, before they consume any origin capacity.

Worked example

A company serves its web application through CloudFront with WAF, from an internet-facing ALB. A penetration test shows that the ALB's DNS name can be called directly, bypassing WAF. The company wants all traffic to pass through CloudFront with the least ongoing effort.

The strongest answer is to move the ALB into private subnets and use a CloudFront VPC origin, so that no public path to the ALB exists. If the ALB must stay internet-facing, restrict its security group to the CloudFront origin-facing prefix list and require a secret header, rotated with Secrets Manager, in an ALB listener rule.

Check yourself

How can an internet-facing ALB accept only requests that come through CloudFront?

Show answer

Allow only the CloudFront managed prefix list in its security group and require a secret custom header that CloudFront adds, checked by a listener rule or AWS WAF. CloudFront VPC origins can also keep the ALB private.

Exam Tips

Remember for the exam

  • Use OAC (not OAI) for S3 origins; it supports SSE-KMS and all Regions.
  • VPC origins let CloudFront reach private ALBs, NLBs and instances.
  • For public custom origins, combine the CloudFront prefix list with a secret header check.
  • Limit S3 origin access to your distribution with an AWS:SourceArn condition.
  • CloudFront Functions for lightweight edge logic; Lambda@Edge for heavier processing.