Connecting WordPress to Amazon RDS using Terraform
Tags: terraform, aws, devops, linux
Published: 2020-11-19 09:15:43

##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
```
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](https://learn.hashicorp.com/tutorials/terraform/install-cli)) 
- Clone Terraform file from this GitHub [Repository]
(https://github.com/abhivaidya07/wordpress_rds)
```
$ git clone https://github.com/abhivaidya07/wordpress_rds
$ cd wordpress_rds
```
- 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
```
- Run the terraform file.
```
$ terraform apply
```
- RDS instance takes sometime to get created...
```
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
```
- 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](https://dev-to-uploads.s3.amazonaws.com/i/z36kggc2ihw3kzzl09ct.png)

- 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](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_ConnectToInstance.html) 

- Click on _Submit_ button.
- Then click _Run the installation_ button. 
- Fill the required details and click on _Install WordPress_ button. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3j471fzvg7u9fe5l30a3.png)

- Login using credentials just created.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pqqapgn4qj83uauu6noo.png)

- Hurray !! Your WordPress application is working.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xnnchksm9u26hhm4wfey.png)

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