DEV Community

Cover image for Connecting WordPress to Amazon RDS using Terraform
Abhishek Vaidya
Abhishek Vaidya

Posted on

Connecting WordPress to Amazon RDS using Terraform

Introduction

In this blog using terraform, we will launch WordPress application in an Amazon EC2 instance and connect it to Amazon RDS (database instance).

What is WordPress ?

WordPress is a free, open-source website creation platform which requires access to database to store information. Therefore we need database to make WordPress accessible.

What is Amazon RDS ?

Amazon Relational Database Service (Amazon RDS) makes it easy to set up, operate, and scale a relational database in the cloud.
Here we will use Amazon RDS as database for WordPress application.

What is Terraform ?

Terraform is used for writing infrastructure as Code to provision and manage any cloud, infrastructure, or service. In this blog we will provision different services provided by Amazon Web Services (AWS).

Getting Started !!

  • Configure AWS CLI, because terraform uses these credentials to authenticate with AWS.
$ aws configure
Enter fullscreen mode Exit fullscreen mode

Give correct access_key and secret_key.

Note: If you are using aws student account give session token, by pasting it in .aws/credentials file

  • Install Terraform (Refer this link)
  • Clone Terraform file from this GitHub Repository
$ git clone https://github.com/abhivaidya07/wordpress_rds
$ cd wordpress_rds
Enter fullscreen mode Exit fullscreen mode
  • Repository contains two files:
    • script.sh: Contains commands which install WordPress application.
    • main.tf: Terraform file which creates EC2 instance with WordPress installed in it (using script.sh) and also creates RDS database instance.
  • RDS database instance is of db.t2.micro type and contains username and password as root and redhat123 respectively.

You can change it by updating username and password parameter under aws_db_instance resource.

  • Initialize Terraform to install required providers.
$ terraform init
Enter fullscreen mode Exit fullscreen mode
  • Run the terraform file.
$ terraform apply
Enter fullscreen mode Exit fullscreen mode
  • RDS instance takes sometime to get created...
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
Enter fullscreen mode Exit fullscreen mode
  • Creates EC2 instance with WordPress application installed in it and also creates RDS instance with mysql 5.7 engine, wordpress database and 20 GiB storage.
  • EC2 instance name - wordpress
  • RDS instance name - sample
  • Browse to public ip of EC2 instance i.e. wordpress

Alt Text

  • Give Database Name - wordpress (Created in RDS instance)
  • Give Username - root (If you have not changed)
  • Give Password - redhat123 (If you have not changed)
  • Give Database Host - Endpoint of RDS instance

Note: To access Endpoint refer this link

  • Click on Submit button.
  • Then click Run the installation button.
  • Fill the required details and click on Install WordPress button.

Alt Text

  • Login using credentials just created.

Alt Text

  • Hurray !! Your WordPress application is working.

Alt Text

  • Now your WordPress application is using Amazon RDS as a backend !!

Top comments (0)