Sub topic 14
Restricting private content with signed URLs and cookies
This chapter assumes you have a working distribution with a private bucket behind Origin Access Control (OAC). It also assumes you can run openssl and the AWS CLI. OAC stops viewers from bypassing CloudFront to reach S3. It does not decide which viewers CloudFront serves. Signed URLs and signed cookies do that.
How signing works
You create an RSA key pair. CloudFront stores the public key, and your application keeps the private key. When a user is entitled to a file, your application signs a URL or cookie with the private key. At the edge, CloudFront verifies the signature and the expiry time. It checks this before it looks in the cache or contacts S3.
Setting it up
- Generate a key pair:
openssl genrsa -out private_key.pem 2048
openssl rsa -pubout -in private_key.pem -out public_key.pem- Upload
public_key.pemto CloudFront as a public key. - Put the public key in a key group.
- On the cache behavior that serves private paths, turn on viewer access restriction and choose that key group as the trusted key group.
In CloudFormation, the behavior reference looks like this:
DefaultCacheBehavior:
TargetOriginId: s3-origin
ViewerProtocolPolicy: redirect-to-https
CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6
TrustedKeyGroups:
- !Ref PrivateKeyGroupUse a separate behavior with a path pattern such as /private/* if only part of the site is restricted. Otherwise every request to that behavior needs a signature, including your HTML pages.
Creating a signed URL
The AWS CLI can sign a URL for testing. The key pair ID is the ID CloudFront gave the public key.
aws cloudfront sign \
--url "https://d111111abcdef8.cloudfront.net/private/report.pdf" \
--key-pair-id K2JCJMDEHXQW5F \
--private-key file://private_key.pem \
--date-less-than 2026-12-31T00:00:00ZIn production, sign inside your application with an AWS SDK or a crypto library, and keep the private key in a secrets store.
Signed URLs or signed cookies
| Need | Use |
|---|---|
| One file, such as a download link | Signed URL |
| Clients that cannot set cookies, such as some media players | Signed URL |
| Many files, such as a whole video library or `/private/*` | Signed cookies |
| Keep URLs unchanged for existing links | Signed cookies |
Signed cookies are three cookies: CloudFront-Policy, CloudFront-Signature, and CloudFront-Key-Pair-Id. The policy is a custom policy. It can use wildcards in the resource, an expiry time, and optionally an IP range. A canned policy covers one exact resource with an expiry only.
Common errors
- 403 on every request. Check that the behavior points to the key group you created, that
Key-Pair-Idmatches the public key, and that the signature was made with the matching private key. - Link expires immediately. Check the expiry timestamp and your server's clock. The time must be in the future and in UTC.
- Signature accepted but file missing. The signature only authorizes access. S3 still needs the object and an OAC bucket policy.
- Cookies not sent. Cookies must be set for a domain that matches the distribution domain, so use a custom domain such as
cdn.example.comthat shares a parent domain with your application. - Long-lived links. A leaked signed URL works until it expires. Keep lifetimes short and generate them on demand.
Exercise
Your site serves public pages and a members-only /private/* section with hundreds of images. Which signing method and cache behavior setup fits best, and why?
Show answer
Create a separate cache behavior for /private/* that trusts your key group, and issue signed cookies after the user logs in. One set of cookies covers every image through a wildcard in a custom policy, so you do not sign hundreds of URLs. The default behavior stays unrestricted for public pages.