Palzin Track
Get 15% off with code PTRACKSIGNUP15 

Laravel Diary Logo

Serving Images with CloudFront + S3

aws
Table of Contents

Are you looking to efficiently serve images using AWS CloudFront and S3? This guide will walk you through the steps to set up this powerful combination for optimized image delivery.

First, navigate to "Services > S3" in your AWS console. Then, create an S3 bucket with the following recommended settings:

Create S3 Bucket

Make sure to block all public access to the bucket, as shown in the image above.

Now, go to "Services > CloudFront" and create a distribution. Here's what you need to configure:

  • Origin Domain Name: Select the S3 bucket you created in step 1.
  • Restrict Bucket Access: Set this to "Yes" to restrict access to S3 only from CloudFront.
  • Origin Access Identity: If you're creating your first CloudFront Distribution, choose "Create a New Identity." If you've already created an "origin access identity," you can select it from "Use an Existing Identity." Typically, one "origin access identity" is sufficient.
  • Grant Read Permissions on Bucket: Set this to "Yes" and choose "Update Bucket Policy." This will automatically add a policy setting to S3's Bucket Policy. Refer to step 3 for more details.

All other values can remain as their default, which are typically the recommended settings.

Create CloudFront Distribution

CloudFront Distribution Settings

As shown in the screenshots above, the S3 images are served with the URL of the "Domain Name."

To ensure everything is set up correctly, go to "Services > S3 > [Your bucket] > Permissions > Bucket Policy."

S3 Bucket Policy

You'll see that a policy, as depicted in the image above, is automatically set.

Now, for a quick operation check, upload an image to your S3 bucket for testing. Use the default and recommended values.

Upload Image to S3

Image Upload Check

First, open the S3 "Object URL" and verify that it returns an "Access Denied" message.

Access Denied in S3

Next, open CloudFront's "Domain Name," and you should see the image displayed correctly.

For more in-depth information and details, you can refer to the official AWS CloudFront documentation.

This setup allows you to efficiently serve images using the power of CloudFront and S3 while ensuring secure access control. Enjoy your optimized image delivery! 🚀

::Share it on::

Comments (0)

What are your thoughts on "Serving Images with CloudFront + S3"?

You need to create an account to comment on this post.

Related articles