Lesson 3 of 4 AWS WAF, AWS Shield and Amazon CloudFront

Securing Content Delivery with Amazon CloudFront

This lesson explains how Amazon CloudFront, which terminates users' connections at the edge, is the natural place for TLS, access restrictions, security headers and WAF rules, and for keeping origins private.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how S3 and custom origins are kept private behind CloudFront.
  • Choose signed URLs, signed cookies or geographic restrictions to restrict viewers.
  • Describe viewer protocol policies, TLS settings and security headers.
  • Configure origin access control for an origin encrypted with a customer managed key.

Keeping origins private

If an origin can be reached directly, attackers can bypass CloudFront and the WAF rules, geographic restrictions and signed URLs applied there. For an S3 bucket, origin access control (OAC) signs CloudFront's requests, and the bucket policy allows only the CloudFront service principal with a condition on the distribution's ARN, so the bucket stays private. OAC also works with objects encrypted with SSE-KMS. For load balancers or instances in private subnets, VPC origins let CloudFront reach them with no public exposure at all.

For a public ALB, two controls combine. Its security group allows inbound traffic only from the CloudFront managed prefix list, and CloudFront adds a secret custom header that the ALB checks, so requests from other CloudFront distributions are rejected too. The following table summarises the protections.

OriginProtection
S3 bucketOrigin access control (OAC): bucket policy allows the CloudFront service principal with the distribution ARN; works with SSE-KMS
Private ALB, NLB or EC2VPC origins: no public exposure
Public ALBSecurity group allows only the CloudFront managed prefix list, plus a secret custom header

The following diagram shows the two policies that must name a CloudFront distribution when its private S3 origin is encrypted with a KMS key.

A private, KMS-encrypted S3 origin behind CloudFront with origin access control. The bucket policy and the key policy both allow the CloudFront service principal only for distribution E2EXAMPLE1234; Block Public Access stays on. Forgetting the key policy statement gives 403 errors for encrypted objects only.
OAC stands for origin access control.
Match the pairs

Keeping each origin private

Exam skills 3.1.2

Match each origin type with the control that stops direct access to it.

Log in to try this activity Interactive activities are free with an account.

Restricting viewers

CloudFront can restrict which viewers may access content. The following list describes the available mechanisms.

  • Signed URLs: one file, e.g. a download.
  • Signed cookies: many files, e.g. a paid video library. Both use trusted key groups.
  • Geographic restrictions: allow or block whole countries; WAF for finer control.
Scenario questions

Restricting who can view

Exam skills 3.1.2 · 3.1.3

Choose the CloudFront mechanism that restricts viewers as required.

Log in to try this activity Interactive activities are free with an account.

TLS and headers

CloudFront enforces encrypted connections and can add security headers to responses. The following list describes the relevant settings.

  • Viewer protocol policy: redirect HTTP to HTTPS or require HTTPS; security policies set minimum TLS and ciphers.
  • Certificates must be in ACM in us-east-1; origin protocol policies cover CloudFront-to-origin TLS.
  • Response headers policies add HSTS, Content-Security-Policy, X-Frame-Options, X-Content-Type-Options and Referrer-Policy without code changes.
True or false

TLS and security headers at the edge

Exam skills 3.1.2 · 5.1.1

Check how CloudFront enforces TLS and adds headers.

Log in to try this activity Interactive activities are free with an account.

Logging

Standard logs go to S3, CloudWatch Logs or Firehose; real-time logs go to Kinesis Data Streams.

Worked example

A private S3 origin with KMS encryption

Static content in an S3 bucket encrypted with a customer managed key must be reachable only through one CloudFront distribution. With origin access control, the bucket policy and the key policy both name the CloudFront service principal, restricted to that distribution's ARN:

# Bucket policy
{ "Sid": "AllowCloudFrontOAC", "Effect": "Allow",
  "Principal": { "Service": "cloudfront.amazonaws.com" },
  "Action": "s3:GetObject", "Resource": "arn:aws:s3:::acme-web-assets/*",
  "Condition": { "StringEquals": {
    "AWS:SourceArn": "arn:aws:cloudfront::111111111111:distribution/E2EXAMPLE1234" } } }

# Statement added to the KMS key policy
{ "Sid": "AllowCloudFrontDecrypt", "Effect": "Allow",
  "Principal": { "Service": "cloudfront.amazonaws.com" },
  "Action": "kms:Decrypt", "Resource": "*",
  "Condition": { "StringEquals": {
    "AWS:SourceArn": "arn:aws:cloudfront::111111111111:distribution/E2EXAMPLE1234" } } }

Block Public Access stays on, because OAC access is not public. A response headers policy adds Strict-Transport-Security and a Content-Security-Policy, and the viewer protocol policy redirects HTTP to HTTPS. Forgetting the key policy statement is the usual reason an OAC setup returns 403 only for encrypted objects.

Check yourself

How can a private Application Load Balancer be served through CloudFront without exposing it?

Show answer

With CloudFront VPC origins.

Exam Tips

Remember for the exam

  • To serve private S3 content only through CloudFront, use origin access control (OAC).
  • To serve a private ALB without exposing it, use CloudFront VPC origins.
  • For paid subscribers accessing many video files, use signed cookies; for one file, use a signed URL.
  • To add HSTS and CSP headers without changing the application, use a response headers policy.
  • A custom certificate for CloudFront must be in ACM in us-east-1.
  • To block a whole country simply, use CloudFront geographic restrictions.