DEV Community

Cover image for Set up Wordpress using Ansible and Terraform
David J Eddy
David J Eddy

Posted on

Set up Wordpress using Ansible and Terraform

Intro

In my previous blog I setup a Wordpress site using Terraform and Ansible for load testing. In this blog I share some details about the logic within the setup.

Industery Trends

Over the past decade the term 'DevOps' has taken the web software world by storm. With the ability to increase the velocity of delivering product (software changes) to the user for the organization it is understandable. Bringing value to the business is the number one motivator for engineers. We are hired to provide the product of software; quickly, with quality, and consistency. 

To facilitate this new tools and mind sets have come to rise. Ideas like immutable infrastructure, repeatable processes, and deterministic results. For the the prior stated project the tools chosen were Terraform for infrastructure and Ansible for provisioning.

How it all fits together

The terminal, once the realm of DOS and Linux geeks are not as important to frontend engineers as it is to Unix administrators. The terminal is the number on tool every software engineer needs to master to really understand how software works.

Terraform, a program offered by Hashicorp, uses HTTP API calls to start, stop, and update the services provided by a wide range of service providers. Need compute? Define a compute instance and apply the changes. Need a new database? Define it as code and apply. Logging into the web console of the provider and see the services running. Terraform's power comes from being able to create, change, and destroy services quickly. For example, the Wordpress configuration is ready for visitors in under 3 minutes. Over, and over, and over. This is the power of Infrastructure as Code (IaC).

The next tool used is Ansible. While Terraform starts the services we still need a way to install applications, copy files, change permissions, etc. Actions that have to occurs ON the machines. This is what provisioning software like Ansible do. They change things ON the machine.

Build all the things!

So, now that we know what the tools do. Lets get our hands dirty. Installing Terraform, Ansible and the AWS CLI is covered in length in other articles, thus will not be covered here.)

In a terminal change directory where we want to clone to configurations into.

cd /path_to_project_root

Next, clone the project

git clone https://github.com/davidjeddy/high_load_cms_for_less_than_the_price_of_lunch.git

Once completed, which should not take long, change into the directory that contains the configuration we want to use

cd ./high_load_cms_for_less_than_the_price_of_lunch/configs/nginx_cache

Now that we are in the directory for the configuration we want to create lets initialize and apply our desired infrastructure.

terraform init
terraform plan -out tf.plan

terraform apply tf.plan

Given the configuration our usage of Ansible is transparent. Terraform stands up the hardware and calls Ansible to provision the application files and settings for us. Some might consider this a cheat, but in the world of automation and ease of use...automate everything.

If everything goes as plan you should see the servers IP address once the setup completes. Visiting the IP address in a browser should reward us with a basic Wordpress installation.

Knocking it all down

Now that everything is up and running, how do we knock it all down? Better question, why would we want to? Because Immutable Infrastructure. The ability to rebuild the configuration over and over with the same predictable state on a whim.

terraform destroy

After answering the confirmation that we do indeed want to tear it all down, wait a couple minutes and poof, no more server. This is also good because who wants to pay for a development server when sleeping? Not me, and not most businesses. Cost savings is a cornerstone of business.

Conclusion

Wrapping it all up we took a look at how to quickly stand up a new server with Wordpress installed and ready to serve requests in a matter of minutes. In a predictable, repeatable, dependable manner. Hopefully by using this example you can see the power of being able to quickly iterate on the infrastructure our applications run on.

Top comments (9)

Collapse
 
bearguy profile image
Stephen Peterkins

Great post! It's awesome to showcase that these automation tools aren't just for constructing large networks. They can also be applied to smaller projects to make management simpler

Collapse
 
david_j_eddy profile image
David J Eddy

Indeed. IaC can get complicated quick. This configuration helps showcase some of the simpler usages. No modules nor dependant services. Just a basic web app install. As noted above I have two more posts tangentially related to this same infrastructure if you are interested.

Collapse
 
bearguy profile image
Stephen Peterkins

I'll have a look, thanks

Collapse
 
bgadrian profile image
Adrian B.G.

Sounds like using a 2tone truck to carry a 2kg IKEA mattress.

Collapse
 
david_j_eddy profile image
David J Eddy

Not at all. Now I can stand up a WordPress site in minutes with two commands. No more starting instance, manually installing LEMP, configuring Nginx, install WordPress via web installer, etc.

Would it be overkill for a single quick idea? Sure. Has this already saved me hours of time on two other ideas? Yes.

At the end of the day though, it is not a fix-all solution. Just a tool that does a thing.

Collapse
 
bgadrian profile image
Adrian B.G.

Sorry, my beef is with WordPress, I don't see its place in the current world.

As for Terraform is a great tool. From what I saw it plays better with Salt rather than Ansible, but I'm no expert in this field.

Thread Thread
 
david_j_eddy profile image
David J Eddy

Ah, gotcha. I went with a CMS that had the largest name recognition. I will check out Salt. I went with Ansible due to it being agent-less. But, more tools for the tool bel is always good.

Collapse
 
slaffik profile image
Slava Abakumov
Collapse
 
david_j_eddy profile image
David J Eddy

This is great! I will forever remember the correct capitalization. :D