Hosting WordPress and MariaDB using AWS

Visual Sources: AWS, Terraform, WordPress, MariaDB

In a previous article, I gave a glimpse into how we can use WordPress using various Amazon Web Services like EC2, VPC, etc. In this article, we will be setting up a similar environment without using the available AMIs for WordPress and MySQL database servers. Rather, we will be installing MariaDB, which is a community fork of MySQL, and WordPress onto AMIs with Linux as the base operating system.

We will also be looking into more services offered by AWS for networking like Route Tables and NAT Gateways.

Our main objective is to set up WordPress and MySQL on different AWS EC2 instances, by creating the following:

  1. Virtual Private Network (VPC)
  2. A public subnet (which has internet connectivity)
  3. A private subnet (which does not have internet connectivity)
  4. Internet Gateway for allowing internet access to the public subnet
  5. NAT Gateway for the private subnet
  6. Route Tables, which will be associated with the subnets
  7. Two AWS EC2 instances, with MySQL and WordPress, installed on separate instances.

Before beginning the setup, we need the following pre-requisites:

  1. AWS Account (Reference 2)
  2. Installation of AWS Version 2 CLI command (Reference 3)
  3. Installation of Terraform (Reference 4)

Writing the Terraform Code

To make the code easier to read and maintain at later stages, it is better to write the code for each resource in separate files, which can be later provisioned in the root module (often named as, by calling the respective resource module. By using input variables and outputs, we can write a basic framework of code that can be customized as per the requirements of the user. The directory structure I have followed is shown below. Note that the folder .terraform is created after executing terraform init on the command line, and the files terraform.tfstate and terraform.tfstate.backup are created only after the successful provisioning of at least one of the resources. The file task4_key.pem is created if the key generation occurs successfully.

Figure 1: Directory Structure for all the files and folders

Each of the files we write will need to be stored in separate folders if we want to use them as modules. While it is possible to use a single directory to store all files, using separate directories for each file helps in creating modules that behave like molds which can be customized as per our requirements.

We begin by writing the code for the Virtual Private Network (VPC).

Next, we write the code for provisioning subnets. Even though we need two subnets (public and private), we just need one module which can be called as many times as needed in the root module (

Then, we write the files for provisioning Internet Gateway, NAT Gateway, and Elastic IP. The Internet Gateway provides internet connectivity to the public subnet. The NAT (Network Address Translation) Gateway allows a roundabout way for the private subnet to access the Internet. The Gateway is designed such that any EC2 instances launched in the private subnet can connect to the internet, but instances from outside cannot connect to the instances in the private subnet. The Elastic IP is another service provided by AWS, which allows us to allocate a public IP to the NAT Gateway, which will, in turn, allow the instances of the private subnet to access the internet.

Next, we write the files for creating route tables and associating the route tables with the respective subnets.

To create EC2 instances, we need the respective security groups as well as a key that will allow us SSH access into the instance(s). Hence, we write the files for key generation ( and provisioning security groups (

Finally, we create two files — one for the EC2 instance with MariaDB ( another for the EC2 instance hosting WordPress (

We then create a final file to link the MariaDB database with the WordPress we have hosted on the separate EC2 instance. To do this, we will need to login to the WordPress instance using SSH and finish the set up of WordPress as a whole. This is done using the null-resource provided by Terraform.

Once all the files have been written, we can create all the resources by calling the respective modules in the file. In this file, we can see that there are some variables used for user customization. These variables can be either declared directly in the root module or they can also be written in a separate file named

In the, the EC2 instances for both WordPress and MariaDB are launched using the Amazon Linux 2 AMI. This is to make the process of setting up WordPress and MariaDB easier (Reference 7,8).

Now, we can successfully create all the resources using the following commands, with the current directory set to the location of the root module.

terraform init #Used to initialize providers & modules being used
terraform plan #Performs a dry run to find errors
terraform validate #Checks for any syntax errors
terraform apply --auto-approve #Used to create all the resources

Once all the resources have been successfully created, we can begin the installation of WordPress by using the public IP of the EC2 instance where WordPress has been downloaded. In your web browser, type the following to begin the installation

<Public IP of instance>/wp-admin/install.php
Figure: Installation of WordPress




ECE Undergrad | ML, AI and Data Science Enthusiast | Avid Reader | Keen to explore different domains in Computer Science

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Getting started with Programming


How to use the google cloud rest apis

Simplicity to Empower You: Home Automation with helloSystem and Tasmota

Why We Became Data Council (1/14/19)

Top 5 Programming languages for Web development in 2022

From social care professional to App developer: there isn’t an App for that!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akshaya Balaji

Akshaya Balaji

ECE Undergrad | ML, AI and Data Science Enthusiast | Avid Reader | Keen to explore different domains in Computer Science

More from Medium

How to deploy a functioning WordPress web page in 30 minutes

DynamoDB : SQL Injection Walkthrough

How to set up WordPress with a dedicated Database Server on AWS Cloud?

AWS VPC Peering Using Transit Gateway