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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.