Secure Access to Amazon CloudFront Content Using Pre-Signed URLs
Overview
Amazon CloudFront provides a way to restrict access to static content using pre-signed URLs. This implementation ensures that only authorized users can access the content by requiring a signed URL generated with a private key.
This document details the step-by-step process of setting up pre-signed URLs for secure access to CloudFront content.
Prerequisites
- AWS Account with necessary IAM permissions
- OpenSSL installed on your local machine
- AWS CLI installed and configured
- AWS SDK installed in your application
Step 1: Generate Public and Private Key Pair
CloudFront requires a public-private key pair for signing URLs.
💡 Commands
Open a terminal and run the following OpenSSL command to generate a 2048-bit RSA key pair:
openssl genrsa -out private_key.pem 2048
openssl rsa -pubout -in private_key.pem -out public_key.pem
Step 2: Upload the Public Key to CloudFront
- Sign in to the AWS Management Console.
- Navigate to CloudFront.
- In the left pane, select Public Keys under Key Management.
- Click Create public key.
- Paste the copied content into the Public Key field.
- Click Create.
Step 3: Create a Key Group in CloudFront
- In the AWS CloudFront console, navigate to Key Groups.
- Click Create key group.
- Under Public Keys, select the previously created public key (e.g., CloudFrontKey).
- Click Create key group.
Step 4: Modify CloudFront Distribution to Restrict Viewer Access
- Go to the CloudFront Distributions section.
- Select the desired CloudFront distribution.
- Click the Behaviors tab and select the behavior associated with the static content.
- In Restrict Viewer Access, choose Yes.
- Under Trusted Key Groups, select the created key group (e.g., TrustedUsersKeyGroup).
- Click Save Changes.
Step 5: Install AWS SDK for Application Integration
The AWS SDK will be used to generate signed URLs in the application.
Conclusion
You have successfully implemented pre-signed URLs for secure access to static content in AWS CloudFront. This ensures that only users with a valid signed URL can access the content while preventing unauthorized access.
Additional Security Considerations
- Store the private key securely using AWS Secrets Manager or another secure storage.
- Rotate the private key periodically for enhanced security.