Hosting WordPress Media with Amazon S3 and CloudFront

English

Action Items
・Create an S3 bucket to store and serve images -> Cost-effective storage
・Install and configure the WP Offload Media Lite plugin
・Set up CloudFront -> Speed up delivery with caching

I'm following along with what I learned in my Udemy course, but I also found a helpful article covering the same topic that I'll reference here:
https://qiita.com/kono-hiroki/items/6cccf3e33cb7d844c6f7

・Storing WordPress Images in S3

閲覧者→インターネット→EC2-wordpress →S3
                     ←…文字           ↓
                     ←…‥… 画像‥‥←

Workflow Overview
-Create an S3 bucket
Set access to public, enable ACLs, give bucket owners full read/write permissions, and grant read-only access to everyone else.
-Create an IAM account with programmatic access to S3

Creating the S3 Bucket

I went to AWS S3, selected bucket creation, and walked through the wizard. I've actually done this a few times before.

The bucket name needs to be globally unique. In my case, I joined my subdomain name parts with hyphens. I used my usual Tokyo region. I wasn't entirely sure about 'Object Ownership', but it turns out that when serving files through this S3 image setup, you need to enable and configure ACLs, otherwise uploads won't go through. Not being 100% sure, I started with ACLs disabled as a test, but as expected, WordPress couldn't write images to S3, so I came back and enabled ACLs later.


Since we're serving content from here, I needed to make access as open as possible, so I unchecked 'Block all public access'.

This prompted a warning checkbox below confirming the acknowledgment—which I checked.

Stepping back a bit, I then configured the ACL settings.

Under 'Edit object ownership' in the S3 bucket, I selected 'ACLs enabled', checked the acknowledgment box below, left the object ownership on the default setting, and clicked 'Save changes'. This enables granular control via ACLs.
The explanation for 'ACLs disabled (recommended)' mentions it is 'owned by this account'. Is this referring to the IAM account configured here? Since the IAM account used for programmatic S3 access is separate, would that cause it to fail?

I configured this under the S3 Permissions tab, setting the bucket owner to 'Read/Write' and everyone else to 'Read' only. I wasn't entirely sure how strict I needed to be, but following the tutorial (though this is touched on a bit later in the guide), these access settings successfully allowed WordPress to write to S3.

-Creating an IAM Account for Programmatic S3 Access

Since I already had a programmatic S3 access user set up, I just reused that.
User: s3User | Policy: AmazonS3FullAccess

The policy lived up to its name. Upon creation, you get:Access Key IDandSecret Access KeyThese are crucial. They get plugged into our program—in this case, WP Offload Media Lite. They're also used when accessing S3 via the AWS CLI.

-Installing the WP Offload Media Lite Plugin

Search for the plugin within WordPress and install it. (It's 'offload' as in offloading traffic, not 'off-road' like a truck!).

Once installed, a prompt appeared asking me to configure wp-config.php. This is where you add the Access Key ID and Secret Access Key for the s3User we just created so WP Offload Media can access S3. I copied the code snippet, SSH'd into my EC2 instance, and pasted it into the WordPress configuration file, wp-config.php.

Just replace the `***` in `'access-key-id'=> '******'` and `'secret-access-key'=>'******'` with your actual s3User credentials.
Just in case, I restarted Apache2—probably using `sudo systemctl restart apache2`.

Refreshing the plugin page changed the status. It seemed to successfully detect my AWS S3 bucket and prompted me to make a selection.

With S3 now visible, additional configuration options appeared.
At this point, uploading an image to the Media Library still wasn't writing to S3 until I sorted out the ACL settings. You can instantly verify if uploads are working by checking the object list in your AWS S3 bucket.

This is the image I used to test the upload. I right-clicked and selected "Open image in new tab" to check the URL. Eventually, the address automatically updated to my final subdomain, but at this stage, it was still pointing to the Amazon S3 domain.

Serving Images via CloudFront

Next, we set up CloudFront.
The setup looks something like this: content is delivered from edge caching servers closer to the user, resulting in faster loading times and reduced load on S3.

閲覧者→インターネット→EC2-wordpress →S3
                      ←…文字           ↓
              ←…CloudFront‥… 画像‥‥←

CloudFrontのところで「ディストレイビューションを作成する」とします。CNAMEに自分のドメインから配信されるようにサブドメインを記述します。何やらこれをやるのはSLL化してないとダメだそうで、でもここから簡単にできます。下の「証明書をリクエスト」をすればトントンと進みます。

-In Route53, define an A record (alias) mapping `static.the-world-star.com` to the CloudFront distribution domain.
-Configure WP Offload Media Lite
Instead of serving files from the Amazon S3 domain or the CloudFront domain, we want them served from our custom subdomain: `static.the-world-star.com`. To achieve this...
Go to settings (was it Delivery?). Click where it says S3 and change it to CloudFront, which will reveal the Alternate Domain (CNAME) settings field. Enter `static.the-world-star.com` right there.

Checking S3...All image files were already uploaded to S3. It looks like the final setup step of WP Offload Media automatically copied existing images over.-> Correction: Old files weren't actually uploaded. Checking the EC2 server, the legacy files were still sitting there. Does the "remove files" option only apply to newly uploaded items?
Looking at older posts, links to images were still pointing to the old paths (without 'static'). At this point, I either need to manually update old posts to include 'static' in the URLs, or perhaps CloudFront can rewrite old image URLs to use 'static' so they fetch from S3 properly. -> Looks like I'll need to upload the old images as well.

Leave a Reply