Skip to content

Using Terraform to Deploy WordPress with AWS EC2 instance and RDS

Cloud Automation enables IT teams, to create, modify or delete resources on the cloud automatically. AWS has its own platform for automation i.e AWS cloud formation but it can be only used to provision AWS resources.

Then there is Terraform. Terraform is an open-source infrastructure as a code software tool created by HashiCorp. Users define and provision data center infrastructure using a declarative configuration language known as HashiCorp Configuration Language (HCL). It somewhat looks like JSON format but a better version.

In this tutorial, I am going to use Terraform to create AWS resources and automate WordPress installation using EC2 Userdata.

For this tutorial, we need some prerequisites. If you don’t have them ready in your system please follow the given links.

Prerequisites

  1. Download Terraform

2. Install AWS CLI

3. Grab the access key and secret key and store them in a file

Make sure you store the access key and secret key in ~yourhomedir/.aws/credentials file in the following format:

[default]
aws_access_key_id=XXXXXXXXXXXXXXXXXXXXX
aws_secret_access_key=XXXXXXXXXXXXXXXXXXXXXXXXX

Or execute aws configure

5. Install Git bash

AWS Resources

I am going to create an EC2 instance where WordPress will be installed and an RDS instance where MySQL database for WordPress will be provisioned.

We are going to use Terraform to automate cloud infrastructure (i.e create instances and security groups) and EC2 USERDATA to install an apache server and WordPress.

EC2 USERDATA is a bunch of scripts that can be used to send and execute processes after EC2 is provisioned. It will be run only once. I am using a bash script to execute the Linux command .

Userdata script is stored in TPL ( template file ) so that Terraform can render UserData script to edit the RDS endpoint, which can be known only after RDS creation is finalized.

We are going to create VPC under which we are going to create 3 subnets in three different Availability zone.

EC2 will be provisioned to public subnet and RDS will be installed to private subnet

I am making sure security groups are properly configured so that only the EC2 web server can communicate with RDS.

Terraform script workflow

Terraform will use AWS CLI to interact with AWS resources. After confirming correct credentials, terraform start creating resources according to the written TF script. Terraform creates resources in a parallel manner but if one resource depends upon another resource, Terraform makes sure that the initial resource is created first. For eg security group is created before creating the instance.

In this case, since the WordPress database depends upon the RDS endpoint, I use depends_on on EC2 resource script to avoid creating EC2 instances before RDS.

Terraform will create resources in the following manner:

Process

Make sure terraform is installed and AWS credentials are properly configured.

Download and unzip terraform projects here.

The project consists of the following files:

  1. main_script.tf Terraform script defining all required resources
  2. terraform.tfvars User values of varaiable used in the project
  3. user.tfvars User defined Database password
  4. user_data.tpl Userdata for AWS LInux 2
  5. userdata_ubuntu.tpl Userdata for AWS Ubuntu 18 LTS
  6. variables.tf Variables value for aws resources

Go to the directory where you unzipped the downloaded project. Use any IDE to edit the variable in terraform.tfvars file. Change WordPress database user password in user.tfvars file. You can change database entries and EC2 instance types. Terraform script is default for for AWS Ubuntu20 LTS . If you want to use AWS Linux 2 then change value of IsUbuntu = false in terraform.tfvars. AMI id will be imported automatically .

Open a command prompt and navigate to the project folder and

Run git clone https://github.com/devbhusal/terraform-ec2-RDS-wordpress.git

Go to the project folder cd terraform-ec2-RDS-wordpress

Initialize Terraform   terraform init to install the plugin.

Generate Keypair using       ssh-keygen -f mykey-pair  

To verify what resources will create run

terraform plan -var-file="user.tfvars"

To apply run terraform apply -var-file="user.tfvars"

The elastic IP address will be displayed after all resources have been created.

After successfull provisioning of AWS Resources,Using remote-exec and private key, EC2 instance will be connected via SSH. Tail command will used to check prgress of WordPress Installation. Once Installation is done ,You will be provided with Public Ip address of WebServer.

Type IP address in your favorite web browser to confirm WordPress is installed. You will be prompted with a select language installation page.

To destroy all resources created by Terraform run terraform destroy -var-file="user.tfvars"

Thank you for reading!

Follow https://github.com/devbhusal/terraform-ec2-RDS-wordpress

5 thoughts on “Using Terraform to Deploy WordPress with AWS EC2 instance and RDS”

  1. Pingback: Using Terraform and Ansible to provision Wordpress On AWS EC2 - Dev Bhusal

  2. Thank you. I think this is one of the best examples I have seen that easily pulls several pieces together and you explanation was fantastic.

Leave a Reply

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