Recently I need to setup a POC website for one of my project to validate the idea, and I decided to go with wordpress since it’s a very power tool and easy to setup. Since all my app are on ECS already so I decide to do wordpress on ECS as well.
WordPress provides a docker image already. And to make use of the docker image there are two things stateful needed to be handle:
- database data
- wp-content directory
For database, I already have a RDS instance for it. It’s also easy to config in wordpress image.
The wp-content directory is where wordpress stores all the uploaded files, installed plugins and theme files. It must be mounted to docker so changes are persistent outside ECS.
EFS is used to do this: I created a EFS volume, and mount it for each of my ECS instances (set it up in user data so it’ll mount automatically for new instances), then in the ECS task definition mount the efs drive to docker volume /var/www/html/wp-content.
For a comparison between EFS/EBS/S3, check EFS FAQ. Basically EFS makes a good balance between performance and availability, it fits my usage for concurrent accessibility.
Assuming a ECS cluster already exists.
- Create an EFS volume, make sure it’s in the same availability zones with ECS cluster.
- I used spot instance for my ECS cluster (highly recommended for cost saving), so I just created a new spot fleet request and in user data we just need to join ECS cluster and attach the efs drive. A sample user data is showed below.
- Update ECS tasks and services to use the mount. I also have a sample script that register task and update service below.
# join ECS cluster
echo ECS_CLUSTER=cluster-name >> /etc/ecs/ecs.config
# mount efs
yum install -y amazon-efs-utils
sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 efs-id.efs.us-east-1.amazonaws.com:/ /efs
mount | grep efs
sudo cp /etc/fstab /etc/fstab.bak
echo 'efs-id.efs.us-east-1.amazonaws.com:/ /efs nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 0 0' | sudo tee -a /etc/fstab
sudo mount -a
Update ECS Task
client = boto3.client('ecs')
image = 'wordpress'
app_name = 'my-website'
# register new version of task
resp = client.register_task_definition(
# update ecs service to pick new task
family = resp['taskDefinition']['family']
revision = resp['taskDefinition']['revision']
new_task_arn = family + ':' + str(revision)
resp = client.update_service(
if __name__ == '__main__':