Photo by CHUTTERSNAP / Unsplash
Photo by CHUTTERSNAP / Unsplash

Deploy WordPress on AWS Elastic Container Service

Create a WordPress stack with AWS ECS and RDS database using Terraform

Guillaume Vincent
Guillaume Vincent

This article is a continuation of two previous articles. In the first one, we created an Ansible role including tests with the Molecule framework. From this base, we saw how to build a Docker image with Packer and Ansible in the second article. Now we have the WordPress image present in the AWS Elastic Container Registry (ECR).

Here we are going to write the Terraform code to deploy WordPress with Elastic Container Service (ECS). ECS is a fully managed container orchestration service. We will use AWS Fargate for containers for serverless compute engine.  In addition to ECS, we will use Relational Database Service (RDS) for the MySQL database.

The presented Terraform code here uses an AWS S3 bucket to store remote states. Each component like RDS is a module included in a dedicated layer. The dependencies between modules are solved through the remote states in S3. Therefore, the presentation order on this page matters. You can use the AWS Free Tier Program to deploy code for free. The presented architecture is not fully high-available to avoid costs and to be reproducible. The full code project is here.

Configure and Deploy AWS VPC

For the Virtual Private Network (VPC) resources, we are going to use the default one to avoid extra cost. We use a single availability zone and one NAT gateway to avoid network transfer costs.

Create the aws-vpc layer in terraform/environments/dev/main.tf:

Create the aws-vpc module in terraform/modules/aws-vpc:

In terraform/environments/dev/aws-vpc initialize and apply the layer:

$ terraform init
$ terraform apply

Configure and Deploy AWS RDS

For the database, we are going to configure and deploy a single RDS instance running MySQL.

Create the aws-rdslayer in terraform/environments/dev/aws-rds:

Create the aws-rdsmodule in terraform/modules/aws-rds:

The password is generated using Terraform resources to not expose sensitive information in the code.

In terraform/environments/dev/aws-rds initialize and apply the layer:

$ terraform init
$ terraform apply
The created RDS database in the AWS web console
The created RDS database in the AWS web console

Configure and Deploy AWS ECS

Create the aws-ecs layer in terraform/environments/dev/aws-ecs:

Create the aws-ecs module in terraform/modules/aws-ecs:

For observability, we create an AWS Cloudwatch group to watch the container's logs:

In terraform/environments/dev/aws-ecs initialize and apply the layer :

$ terraform init
$ terraform apply

Configure and Deploy WordPress Image In AWS ECS

Create the aws-ecs-wordpress layer in terraform/environments/dev/aws-ecs-wordpress:

To deploy WordPress containers in the ECS cluster, we need to define :

  • An ECS service that allows to run and maintain a specified number of instances of a task definition. It will monitor the tasks if instances fail or stop and schedule new instances to meet the desired number of instances.
  • An ECS task definition defining how to run the Docker containers in AWS ECS

In addition to that, we will create a dedicated security group and IAM permissions for the ECS service.

Create the aws-ecs-wordpress module in terraform/modules/aws-ecs-wordpress:

In terraform/environments/dev/aws-ecs-wordpress initialize and apply the layer:

$ terraform init
$ terraform apply

In the AWS web console, you can see the ECS cluster with the Fargate service including the WordPress running task :

The WordPress ECS service with the running task
The WordPress ECS service with the running task

When you click on the ECS service you have more information about the task. Click on the task ID :

The container logs are searchable in the created log group in Cloudwatch:

The Cloudwatch log group of the ECS WordPress service
The Cloudwatch log group of the ECS WordPress service

When you click on the log group you can watch the container's logs:

The container's logs in the Cloudwatch group

Conclusion

Through this series of articles, we have seen how to set up a framework to configure, build and deploy images in ECS :

  • Ansible manages the configuration recipes. The Ansible roles are reusable and testable using Molecule and Docker. It ensures good quality and improves development velocity.
  • Packer reuses the Ansible code to build a docker image locally or push it to the AWS Elastic Registry.
  • Terraform deals with the AWS infrastructure.

Resources

GitHub - guivin/ecs-ansible-packer-terraform-wordpress: A POC framework to create Wordpress docker image with Ansible/Packer and deploy it to AWS ECS using Terraform
A POC framework to create Wordpress docker image with Ansible/Packer and deploy it to AWS ECS using Terraform - GitHub - guivin/ecs-ansible-packer-terraform-wordpress: A POC framework to create Wor...
Infrastructure as CodeCloud

Guillaume Vincent

DevOps Engineer & AWS Certified Solution Architect. Cloud enthusiast and automation addict