Set up Wordpress using Ansible and Terraform
Tags: webdev, ansible, terraform, wordpress
Published: 2018-11-08 00:14:42

---
title: Set up Wordpress using Ansible and Terraform
published: true
description: Create a Wordpress server in minutes using Ansible and Terraform.
tags: #webdev, #ansible, #terraform, #wordpress
cover_image: https://blog.davidjeddy.com/wp-content/uploads/2018/04/terraform_logo.png
---

<!-- wp:heading -->
<h2>Intro</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><a href="https://blog.davidjeddy.com/2018/11/07/just-how-effective-is-web-server-caching-very/">In my previous blog</a> I setup a <a href="https://wordpress.com/">Wordpress</a> site using <a href="https://www.terraform.io/">Terraform</a> and <a href="https://www.ansible.com/">Ansible</a> for load testing. In this blog I share some details about the logic within the setup.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Industery Trends</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>How it all fits together</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Build all the things!</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>So, now that we know what the tools do. Lets get our hands dirty. Installing Terraform, Ansible and the <a href="https://www.terraform.io/docs/providers/aws/">AWS CLI</a> is covered in length in other articles, thus will not be covered here.)</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In a terminal change directory where we want to clone to configurations into.</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">cd /path_to_project_root</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>Next, clone the project</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">git clone https://github.com/davidjeddy/high_load_cms_for_less_than_the_price_of_lunch.git</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>Once completed, which should not take long, change into the directory that contains the configuration we want to use</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">cd ./high_load_cms_for_less_than_the_price_of_lunch/configs/nginx_cache</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>Now that we are in the directory for the configuration we want to create lets initialize and apply our desired infrastructure.</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">terraform init<br></pre>
<!-- /wp:preformatted -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">terraform plan -out tf.plan</pre>
<!-- /wp:preformatted -->

<!-- wp:image {"id":1125} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/screely-1541635140021.png" alt="" class="wp-image-1125"/></figure>
<!-- /wp:image -->

<!-- wp:image {"id":1126} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/screely-1541635233585.png" alt="" class="wp-image-1126"/></figure>
<!-- /wp:image -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">terraform apply tf.plan</pre>
<!-- /wp:preformatted -->

<!-- wp:image {"id":1127} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/screely-1541635397974.png" alt="" class="wp-image-1127"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>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.<br><br>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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Knocking it all down</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">terraform destroy<br></pre>
<!-- /wp:preformatted -->

<!-- wp:image {"id":1128} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/screely-1541635469175.png" alt="" class="wp-image-1128"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->