Setting up AWS S3 and CloudFront for fast media delivery is a straightforward process that requires a few key steps.
First, you need to create an S3 bucket, which is essentially a container for your media files. This can be done through the AWS Management Console or using the AWS CLI.
Next, you'll want to configure your bucket for media delivery by setting the bucket policy to allow public read access. This is essential for serving your media files to users worldwide.
To speed up media delivery, you can also enable S3's content delivery network (CDN) feature, which automatically caches your media files at edge locations around the globe.
Setting Up AWS S3 and CloudFront
To set up AWS S3 and CloudFront, start by logging into the AWS console and creating an S3 bucket. Select a region and give your bucket a unique name. Note that if you don't want people to access your website directly from S3, you'll need to create an Origin Access Identity.
To configure your S3 bucket to serve your static website, go to the "Static Website Hosting" box and select "Enable static website hosting." You can then set the default root object to your index file, such as index.html.
To set up CloudFront, navigate to the CloudFront page and click "Create Distribution." Choose a Web distribution and select your S3 bucket as the origin domain. Set the origin access to "Origin access control settings" and enable Shield to minimize your origin's load. In the Default cache behavior settings, set the Viewer protocol policy to "Redirect HTTP to HTTPS."
Upload Files to S3
To upload files to your S3 bucket, you can use the AWS CLI. The syntax to copy a folder from your computer to an S3 bucket is aws s3 cp --recursive source destination.
You can also use the AWS CLI to upload files to your S3 bucket in an automated CI/CD pipeline. The command to upload the "dist" folder to the S3 bucket "teco-web-app" is aws s3 cp --recursive dist s3://teco-web-app.
If you don't have the AWS CLI on your computer, you can install it by following the link provided in the documentation.
Set Up CloudFront
To set up CloudFront, start by creating a distribution. This can be done by searching for CloudFront in the AWS console search bar and opening the CloudFront dashboard. From here, select the Origin domain of your bucket from the dropdown and give the name of the CloudFront distribution.
You'll also need to set the origin access as Origin access control settings and create an access control setting for this CloudFront distribution. This will help minimize your origin's load and improve its availability.
Enable Shield to reduce your origin's operating costs. In Default cache behavior settings, set the configurations as follows: Cache key and origin requests should be set to "Use viewer's IP address as the cache key and include the origin request in the cache key." This will help cache content more effectively.
You'll also need to set up the Function associations - this can be left as default for now. In the final section, choose the settings that best fit your use case and requirements.
Once the CloudFront distribution is created, you'll need to add the policy to the S3 bucket policy. To do this, go to the S3 bucket, click on Permissions, and then Edit bucket policy.
Configuring S3 and CloudFront
To configure WP Offload Media to use Amazon CloudFront, head over to your WordPress site and visit the WP Offload Media settings page.
You'll need to pick Amazon CloudFront as the new Delivery Provider and click the Save Delivery Provider button to save your changes.
The Delivery Settings panel will now show "Amazon CloudFront" as the Delivery Provider, and a new Use Custom Domain Name (CNAME) option, which is turned off by default.
Toggle the switch on to enable the Use Custom Domain Name (CNAME) option.
You'll see a text box where you should enter the dXXXXXXXXXXX.cloudfront.net style distribution domain name that was generated for the Amazon CloudFront distribution.
Be sure to delete the https:// bit after pasting it into the Use Custom Domain Name (CNAME) field, only the domain name should be entered.
Click the Save Changes button to start serving your offloaded Media Library items via Amazon CloudFront.
It's a good idea to quickly check that all is in order, and an offloaded Media Library item is being served by Amazon CloudFront.
You can do this by hopping on over to the site's Media Library and finding an offloaded image, clicking on it to view its details.
If you used grid mode for viewing the Media Library, you should see the image displayed, and File URL near the bottom of the right-hand sidebar showing the expected CloudFront domain.
If you used list mode for viewing the Media Library, you should see the image displayed, and File URL near the top of the right-hand sidebar showing the expected CloudFront domain.
Congratulations, your site is now serving media offloaded to an Amazon S3 bucket via an Amazon CloudFront CDN distribution!
Security and Permissions
Security and Permissions are crucial when it comes to protecting your AWS S3 and CloudFront setup.
To restrict access to your S3 bucket, you can use Origin Access Identity (OAI), which allows you to restrict access to the bucket while allowing access through CloudFront.
CloudFront integrated AWS WAF is a powerful tool that allows you to protect your applications from common web exploits like SQL injections.
AWS Shield is a managed DDoS protection service that helps safeguard your application from Distributed Denial of Service attacks.
To use CloudFront in your applications, you need to set up an IAM role and permissions for your web application.
This typically involves giving your application the PutObject, GetObject, and DeleteObject permissions for the S3 bucket, which can be achieved by creating an IAM user or role.
Here are the specific permissions required for your web application:
- PutObject
- GetObject
- DeleteObject
For Put and Delete operations, you need to create an IAM role and attach the below IAM policy.
Web Application Setup
To set up a web application, start by creating an S3 bucket, where you can upload your website files. This is where your website's static files will be stored.
You'll also need to configure S3 to serve your static website, which involves enabling static website hosting and setting the index file for your site. This is a crucial step in making your website accessible to users.
Next, create an IAM role and attach the necessary permissions to allow your web application to interact with S3. This typically involves granting PutObject, GetObject, and DeleteObject permissions.
Here are the key steps to consider:
- Create an S3 bucket and upload your website files
- Configure S3 to serve your static website
- Create an IAM role and attach necessary permissions
With these steps in place, you'll be able to set up a web application that leverages the power of S3 and CloudFront to deliver fast and secure content to your users.
Static Website Hosting
Static website hosting is a great way to serve objects through your web application.
To set it up, you'll need to start by searching for S3 and opening the S3 console. From there, you can create an S3 bucket to store your objects.
Uploading objects to the S3 bucket is a crucial step in the process. You can upload objects for testing or for your project.
To enable static website hosting on the S3 bucket, change the Static website hosting to Enable, and leave the Hosting type to Host a static website.
In the text input with the label "Index document", type "index.html", which represents the entry file of the website.
Here's a quick rundown of the steps to follow:
- Search for S3 and open the S3 console
- Create an S3 bucket
- Upload objects to the S3 bucket
- Enable static website hosting on the S3 bucket
- Set the entry point of the CloudFront Distribution
By following these steps, you'll be able to set up static website hosting and serve objects through your web application.
WP Offload Media Configuration
To configure WP Offload Media, start by deploying an Amazon CloudFront distribution that uses your bucket as its origin. This will allow you to serve your site's offloaded media via the distribution's domain name.
Head over to your WordPress site and visit the WP Offload Media settings page, where you'll find the Delivery Settings panel. Click the Edit button to the right of the Delivery Provider's name, which should currently say "Amazon S3".
Pick Amazon CloudFront as the new Delivery Provider, then click the Save Delivery Provider button to save your changes. You'll now see "Amazon CloudFront" as the Delivery Provider, and a new Use Custom Domain Name (CNAME) option.
Toggle the Use Custom Domain Name (CNAME) switch on, and enter the dXXXXXXXXXXX.cloudfront.net style distribution domain name that was generated for the Amazon CloudFront distribution. Don't forget to delete the https:// bit at the beginning.
You'll also see a new Serve Private Media from CloudFront option, but leave it turned off for now. You can use the Private Media doc to configure signed CloudFront URLs for private media or downloads later.
Click the Save Changes button to start serving your offloaded Media Library items via Amazon CloudFront. The URL Preview at the bottom of the settings page shows you an example of the kinds of URLs that the site's offloaded media will use.
It's a good idea to quickly check that all is in order, and an offloaded Media Library item is being served by Amazon CloudFront. You can do this by visiting the site's Media Library and finding an offloaded image.
If you used grid mode for viewing the Media Library, you should see the image displayed, and File URL near the bottom of the right-hand sidebar showing the expected CloudFront domain. If you used list mode, you should see the image displayed, and File URL near the top of the right-hand sidebar showing the expected CloudFront domain.
You're also given the opportunity to add tags, but this is purely optional. If you're a heavy user of AWS, you may want to add tags to help with management tasks, but otherwise you can leave them out.
To enable Block All Public Access for the bucket, switch back to WP Offload Media's settings page and click the Edit button to the right of "Amazon S3" in the Storage Settings panel's header. Toggle the switch "on" in the "Block All Public Access" panel's header, and then check the box to confirm that you've set up the Origin Access Identity and have a correct bucket policy.
Click the Update Bucket Security button to apply the changes. WP Offload Media will now update the bucket's Block All Public Access setting to fully enable it.
To serve private media via Amazon CloudFront, you can configure signed CloudFront URLs that expire after a few seconds. This is useful for serving WooCommerce or Easy Digital Downloads product files, for example.
By default, WP Offload Media creates signed expiring URLs that use the raw S3 URL format, but you can configure it to serve those product downloads via Amazon CloudFront for improved speed, reduced costs, and better branded URLs.
Frequently Asked Questions
Why put CloudFront in front of S3?
CloudFront helps improve the security and accessibility of your S3 objects by restricting access with authentication methods like signed URLs and IAM policies. By placing CloudFront in front of S3, you can enhance the protection and delivery of your content.
Do you pay for S3 to CloudFront?
No, you don't pay for data transferred from AWS S3 to CloudFront. However, you will incur costs for data transferred out of CloudFront edge locations to the internet or origin.
Why is CloudFront redirecting to S3?
CloudFront is redirecting to S3 due to internal DNS propagation issues when setting up an S3 bucket. This can be resolved by configuring a public website in S3 before setting up the CloudFront distribution.
Sources
- https://stratus10.com/blog/how-to-host-a-static-website-with-s3-and-cloudfront
- https://blog.tericcabrel.com/host-static-website-aws-s3-cloudfront/
- https://fathomtech.io/blog/using-aws-s3-and-cloudfront-for-fast-static-web-sites/
- https://dev.to/aws-builders/setup-cloudfront-amazon-s3-to-deliver-objects-on-the-web-apps-securely-efficiently-2gnk
- https://deliciousbrains.com/wp-offload-media/doc/cloudfront-setup/
Featured Images: pexels.com