How to deploy a full environment for WordPress site via docker by using Terraform?
By: Mohammed Lutfalla
Tags: Terraform, AWS, DevOps, WordPress, PHP

How to deploy a full environment for WordPress site via docker by using Terraform? Mohammed Lutfalla Follow Dec 28 · 5 min read

For many users, preparing an environment in AWS to host their website/system can be tough, especially if they’re new to Cloud and what does actually that means.

Also, after the preparation is done, the creation of resources might take a while too! But, what if you can do the following within 20 min window? Check the list:

Create a VPC and all essentials for public and private subnets.

Create an EFS and prepare an access point to this storage option.

Create a launch template to replicate your configuration into multiple EC2s in different subnets.

Prepare your website as Docker image to have consistent replica of your main concern (your website and its DB).

It feels wired right? Actually no, because this is the beauty of IAC and Terraform. Why don’t we go deeper? Prepare your swimming suit.

NOTE: This is a POC script and got tested, yes it can be better but for the article purpose, I guess this is a good starting point.

What you will get?

Check the image below, do not get tricked yet, wait for the clarafications:

After you run the code “ as it is”, you will get a load balancer placed in one of the public subnets, one EC2 machine that got deployed by Auto Scaling Launch Template and EFS.

These things are enough to run your website, at least for this use case that I am talking about.

But what actually happened when I run the script is the following:

For VPC, the script will make a VPC, subnets (public and private), routing tables and internet gateway.

For Auto Scaling, the script will make a launching template with what is needed to replicate the configuration into multiple instances as it required and will make sure that the minimum required instances are always meet the configuration.

For Load Balancer, the script will prepare an endpoint associated with health check metrics.

For EC2, the script will launch an instance from the configuration script that got attached to the Auto Scaling launching template, this will install and setup Docker inside the machine and will pull the project from GitHub.

For EFS, the script will create all needed resources like security group, access point and access point attachment for this storage.

Is that too much? I guess not, some other systems needs a lot more. But, let us agree on one thing, doing all these things in 20 min manually is exhausting.

The steps of deployment:

It is really easy and needs few clicks, let us start shall we?

Before we start, clone this link.

1- Do you have Terraform installed?

First thing first, check if you have Terraform installed in your machine.

2- Do you have your AWS credentials?

Because we are going to deploy these instruction to AWS, make sure one of these two options are available:

AWS CLI is configured, So you should only know which profile will use in case of multiple configurations are placed.

AWS access and secret keys, because of course we need credentials to access.

NOTE: Check provider.tf once you decide which connection to use.

3- Some necessary commands.

Open a terminal window and navigate into the project.

Because we are going to make a new instances and we might need to access the instance, so run these commands.

ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f $PWD/id_rsa

NOTE: Do not change the key name unless you go to vars.ft and change it there also.

Then, run this command to get all needed modules:

terraform init

4- Trigger the script.

There is only one step left, maybe two if you want to check what will happen.

To check what are the resources that will be created, run this command:

terraform plan

To bring all these planned resources to the life, run this command:

terraform apply

After all resources got alligned in the correct order, a message will ask you to approve these changes, type yes.

And now you can relax till the whole process is finished.

NOTE: After Terraform script is done, please wait 5–10 min before checking the website, the reason is that the infrastructure is done but the instance script is in progress. EFS mounting needs some time to be done.

To clean everything, run this command:

terraform destroy

Important points:

1- Why EFS in this simple use case? Because I am using Docker to host website and DB all together, and you have Auto Scaling launch template, EFS should insure the consistency of your DB because the DB has a volume pointed into EFS. Any new instance will use EFS as volume.

2- At the end of the script execution, Terraform will return an ELB link and this will not work directly. Why?? Because Wordpress first initial page is going to be /admin to finish all needed configuration. So, go to aws console -> EC2 -> your created instance and visit its public IP to finish the setup. Then all going to be good.

Summary:

I have no idea if this precess would be helpful for anyone, but since I know how to make it, why not to share it?

Creating a full basic environment in AWS gets funny turns, especially for new comers to this platform. This script will give them few important points to think about for now on, using Docker is really good option, using Terraform is not hard and finally, IAC is something they need to work on.