Lesson 3 of 4 Amazon CloudFront

Securing Content with Origin Access Control, Signed URLs and HTTPS

This lesson explains how CloudFront controls who can reach your content, keeps origins private, enforces HTTPS and adds security headers, all at the edge before requests reach the origin.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify the access control that answers each kind of question, such as origin access control or signed URLs.
  • Explain why an origin access control bucket policy needs the AWS:SourceArn condition.
  • Distinguish signed URLs from signed cookies.
  • Describe how the viewer protocol policy and origin protocol policy enforce HTTPS.

Restricting access

CloudFront provides five access controls, and each answers a different question. Origin access control decides whether anything other than CloudFront can read the origin. Signed URLs and signed cookies decide which users may retrieve private content. Geographic restrictions decide which countries may receive content at all. AWS WAF decides whether an individual request is legitimate. The controls are complementary, and a distribution that serves private content usually combines several of them.

Origin access control

Without origin access control (OAC), an S3 bucket behind CloudFront must be public, which lets users bypass CloudFront and every control configured on it. With OAC, CloudFront signs each request to S3 with AWS Signature Version 4, and the bucket policy grants read access only to the CloudFront service principal acting for one distribution. The bucket stays private with S3 Block Public Access enabled, and a user who requests an object directly from S3 receives 403 Access Denied.

Signed URLs and signed cookies

OAC protects the origin, but anyone who can reach the distribution can still retrieve its content. To limit content to authorised users, such as paying subscribers, a cache behaviour is configured to require signed requests. After the application authenticates a user, it creates a signature with a private key and gives it to the user, either in the query string of a URL or as cookies. CloudFront verifies the signature at the edge with the matching public key and rejects any request whose signature is missing, invalid or expired. A signed URL authorises one object, which suits a single download. Signed cookies authorise every object that matches a path pattern, which suits a members' area or a video delivered as hundreds of segment files, and they leave the URLs unchanged.

Geographic restrictions

Geographic restrictions, also called geoblocking, allow or block viewers by country, for example to meet licensing terms that permit distribution only in certain countries. CloudFront determines the viewer's country from the IP address by using a third-party GeoIP database, and returns HTTP 403 Forbidden to viewers in blocked countries; a custom error page can explain the restriction. The setting is either an allow list or a block list, and it applies to the whole distribution. When different paths need different country rules, or country must be combined with other conditions, an AWS WAF geographic match rule is used instead.

AWS WAF

AWS WAF inspects the content of each request. A web ACL associated with the distribution evaluates its rules in priority order before CloudFront returns a response, including a cached response, so malicious requests are stopped at the edge and never reach the origin. Rules can match SQL injection and cross-site scripting patterns, IP address sets and request rates, and AWS managed rule groups provide common protections without custom rules. A web ACL for CloudFront is created with the global (CloudFront) scope in us-east-1. The AWS WAF module describes rules and actions in detail.

The following table summarises what each control decides and a typical use of each.

ControlDecidesTypical use
Origin access control (OAC)Whether anything other than CloudFront can read the S3 originKeeping a bucket private while CloudFront serves its content
Signed URLsWhich users can retrieve one object, and until whenA purchased software download
Signed cookiesWhich users can retrieve all objects under a pathA subscribers' area or segmented video
Geographic restrictionsWhich countries receive content from the distributionContent licensed for certain countries only
AWS WAFWhether each request is legitimateBlocking SQL injection, bad bots and request floods

Origin access control

The bucket policy below allows only one distribution to read objects. The AWS:SourceArn condition is essential: without it, any CloudFront distribution, including one in another account, could read the bucket through its own OAC.

{
  "Effect": "Allow",
  "Principal": {"Service": "cloudfront.amazonaws.com"},
  "Action": "s3:GetObject",
  "Resource": "arn:aws:s3:::web-assets/*",
  "Condition": {"StringEquals": {"AWS:SourceArn": "arn:aws:cloudfront::111122223333:distribution/E2ABCDEF123456"}}
}

OAC replaces the older origin access identity (OAI). Unlike OAI, it supports objects encrypted with SSE-KMS, provided the key policy allows the CloudFront service principal to use kms:Decrypt for the distribution, and it supports S3 buckets in every Region.

OAC applies to S3 and a few other AWS origins, such as Lambda function URLs. A custom origin, such as an Application Load Balancer, is protected in other ways. CloudFront can add a custom header with a secret value to every origin request, and an ALB listener rule forwards only requests that carry it. The origin's security group can also allow inbound traffic only from the AWS managed prefix list for CloudFront origin-facing servers. The two are often combined, because the prefix list admits every CloudFront distribution, not only yours. CloudFront VPC origins remove public exposure entirely by letting CloudFront reach an ALB or EC2 instance in a private subnet.

Signed URLs and signed cookies

Signed requests are enabled per cache behaviour by restricting viewer access and choosing a trusted key group, which holds up to five public keys uploaded to CloudFront. The application keeps the matching private key, typically in Secrets Manager. Because a key group can contain several keys, a new key can be added before the old one is removed, which rotates signing keys without rejecting valid requests.

Each signature is created over a policy statement. A canned policy contains only an expiry time and produces shorter URLs. A custom policy can also set a start time, restrict the viewer's IP address range and use a wildcard in the resource path, which is how one set of signed cookies grants access to every file under /courses/*. If a request carries both a signed URL and signed cookies, CloudFront uses the signed URL.

CloudFront signed URLs are often confused with S3 presigned URLs, which serve a different purpose. The following table compares the three mechanisms.

CloudFront signed URL
Grants access toOne object
Verified byCloudFront, at the edge
Signed withA private key whose public key is in a trusted key group
URL changesYes: the signature is in the query string
Typical useA single download, or clients that cannot store cookies
CloudFront signed cookies
Grants access toAll objects that match the policy's path
Verified byCloudFront, at the edge
Signed withA private key whose public key is in a trusted key group
URL changesNo
Typical useA members' area or streaming video segments
S3 presigned URL
Grants access toOne S3 object
Verified byAmazon S3
Signed withThe IAM credentials of the signer
URL changesYes: the signature is in the query string
Typical useDirect upload to or download from S3, without CloudFront

HTTPS and security headers

Encryption in transit applies to two connections: from the viewer to CloudFront, and from CloudFront to the origin. Each connection is configured separately.

The viewer protocol policy, set per cache behaviour, controls the viewer connection. HTTP and HTTPS accepts both protocols. Redirect HTTP to HTTPS returns a 301 redirect for HTTP requests, which suits websites that users type into a browser. HTTPS only rejects HTTP requests with 403, which suits APIs whose clients should never use HTTP. To serve a custom domain name such as www.example.com over HTTPS, the distribution needs a certificate for that name in AWS Certificate Manager in the us-east-1 Region, whatever Region the origin is in. The distribution's security policy sets the minimum TLS version that viewers may use.

The origin protocol policy controls the connection to a custom origin: HTTP only, HTTPS only, or Match viewer, which uses the protocol of the viewer's request. With HTTPS, the origin must present a certificate from a trusted certificate authority that matches the origin domain name, so a self-signed certificate on the origin causes 502 errors.

Response headers policies add HTTP headers to responses at the edge, without changes to the application. Security headers such as Strict-Transport-Security (HSTS), Content-Security-Policy, X-Content-Type-Options and X-Frame-Options instruct browsers to use HTTPS only and to resist cross-site scripting and clickjacking. The same policy can add CORS headers, and AWS provides managed policies, such as SecurityHeadersPolicy, for common cases.

The following table summarises these settings.

SettingControlsExample
Viewer protocol policyThe protocol between the viewer and CloudFrontRedirect HTTP to HTTPS for a website
ACM certificate in us-east-1HTTPS for a custom domain nameA certificate for www.example.com
Origin protocol policyThe protocol between CloudFront and a custom originHTTPS only to an Application Load Balancer
Response headers policyHeaders that CloudFront adds to responsesHSTS and a content security policy
Worked example

Private course videos

An online course site serves videos from S3 through CloudFront. Videos must be viewable only by paying members, and nobody may download them from the bucket directly.

RequirementControl
Bucket not reachable directlyOrigin access control, with a bucket policy allowing only this distribution
Only members can watchSigned cookies issued by the application after sign-in, covering /courses/*
Video URLs stay unchangedCookies rather than signed URLs
Security headers on every pageA response headers policy with HSTS and a content security policy

The cookies expire with the member's session, and the trusted key group lets the team rotate the signing key without downtime.

Check yourself

How can an S3 bucket be made reachable only through a CloudFront distribution?

Show answer

Use origin access control (OAC) and a bucket policy that allows the CloudFront service principal for that distribution, while S3 Block Public Access stays on.

Exam Tips

Remember for the exam

  • To allow S3 access only through CloudFront, use origin access control.
  • To restrict access to many private files, use signed cookies; for a single file, use a signed URL.
  • To add security headers to every response, use a response headers policy.
  • CloudFront certificates must be in us-east-1.
  • To block countries, use geographic restrictions; to block exploits, attach AWS WAF.