Deploying Wordpress on AWS Fargate with Amazon EFS File System

Mani
5 min readApr 30, 2020
Wordpress running on AWS Fargate with Amazon EFS

From https://aws.amazon.com/about-aws/whats-new/2020/04/amazon-ecs-aws-fargate-support-amazon-efs-filesystems-generally-available/

Amazon Elastic Container Service (ECS) tasks running on both Amazon Elastic Compute Cloud (EC2) and AWS Fargate can now mount Amazon Elastic File System (EFS) file systems. ECS tasks using EFS will automatically mount the file systems specified by the customer in the task definition and make them available to the containers in the task across all availability zones in the region. This enables persistent, shared storage to be defined and used at the task and container level in ECS.

Applications like Wordpress, Drupal, and JIRA use EFS to store user data and application state, with active/standby container pairs running in different availability zones for high availability. Data science teams use EFS to store shared training data and home directories, allowing notebook containers and distributed training jobs to access the data in parallel.

…..

In the past, I have worked with several customers who wanted to deploy Wordpress on Amazon ECS. Since Wordpress ideally needs a shared file system like EFS, to store content, templates, plugins and other stuff, this meant having to do some kludgy stuff like mounting the file systems at the EC2 instances and then accessing them via containers on Amazon ECS. This was not a neat solution, as the file system is being managed outside the container orchestration platform, and becomes one more thing to manage on your own ..

This announcement, eagerly awaited and asked by several customers will help accelerate adoption of EFS within Amazon ECS on both EC2 and Fargate.

Please note as of writing this blog, if you need to use a cloud native way of using EFS with Amazon EKS on EC2, its via the CSI driver — https://docs.aws.amazon.com/eks/latest/userguide/efs-csi.html and for Amazon EKS with Fargate, its on the roadmap - https://github.com/aws/containers-roadmap/issues/826

This blog is meant to be a quick how-to guide to deploy Wordpress on AWS Fargate and leveraging this new functionality of accessing EFS from within an container running on Fargate.

There is also an exhaustive blog at https://salzam.com/high-traffic-wordpress-website-docker-aws-ecs-codepipeline-load-balancer-rds-efs/ where the author has done a fantastic job of customizing the official wordpress docker image to separate out the deployment of wordpress plugins on EFS, which is probably the way to use in a real-world deployment, and deploys Wordpress on ECS with EC2.

In this blog, the key difference is that I have decided to use only the official Wordpress docker image and will deploy on AWS Fargate.

Step 1

Please do the prep work for the following :

  1. Create an VPC or use the default VPC with private subnets, NAT Gateway, Internet Gateway and a Bastian Host. The Bastian host will help you to query/manage the RDS MySQL database, mount EFS data volumes to test stuff and other useful stuff for devops.
  2. Create separate security groups for access between the Fargate instances, Load Balancers, RDS, Bastian and EFS
  3. Create an IAM role for the Fargate tasks to run — I gave permissions to access RDS, EFS and Systems Manager

Step 2

  1. Create an EFS file system
efs file system for use by Wordpress

2. Create an RDS MySQL Databases instance. https://aws.amazon.com/getting-started/hands-on/deploy-wordpress-with-amazon-rds/4/ has steps for creating the RDS database, connecting to the RDS MySQL from Bastian host and creating a wordpress user .. Make sure that the security groups for RDS allows traffic from Bastian host and AWS Fargate instance.

Creating an wordpress user from

3. Create an Application Load Balancer, listening on port 80 and with a target group for IP address and make sure you have the following health check configured (HTTP success codes, to account for redirections)

ALB — Target Group — Health Check

Step 3

The key aspect of the Task definition includes the volume reference to the EFS file system, that was created earlier:

EFS volume in the ECS Task definition

and then referencing this volume from the container path:

Mount point

The environment variables for referencing the RDS MySQL database are in the clear and this is not a good practice ;-) Please use AWS Secrets Manager or System Manager Parameter Store ..

  • Create an ECS service, and select the task definition, ALB and the rest of the usual stuff ..

One gotcha, please make sure you select the platform version specifically as 1.4.0 and NOT LATEST :

ECS Service — Platform version
Wordpress service with two Wordpress tasks

Finally

Access the Wordpress application by accessing the load balancer URL. You will need to do some initial setup like admin name, password etc. for the first time, create your first wordpress blog and publish the blog ..

wordpress admin page ..

If you are curious about the shared file system and whats going on there .. You can mount the EFS file system from your Bastian host and check the file system ..

EFS Mount from Bastian host

That’s it. Hope this was useful, Enjoy and Namaste !!

--

--

Mani

Principal Solutions Architect at AWS India, and I blog/post about interesting stuff that I am curious about and which is relevant to developers & customers.