/terraform-WebAppWithRDS

Install an Apache web server with PHP, and create a MySQL database. The web server runs on an Amazon EC2 instance using Amazon Linux, and the MySQL database is an Amazon RDS MySQL DB instance. Both the Amazon EC2 instance and the Amazon RDS DB instance run in a VPC based in Amazon Virtual Private Cloud service

Primary LanguageHCL

Create a Web Server and an Amazon RDS Database via Terraform

A common scenario includes an Amazon RDS DB instance in an Amazon VPC, that shares data with a Web server that is running in the same VPC. In this repo a VPC for this scenario is created. The following diagram shows this scenario

Amazon RDS DB Instance

Inspriration for this repo came from this tutorial; Create a Web Server and an Amazon RDS Database.

This solution has been tested on CentOS 7.4:

  • You may use the provided Vagrantfile for testing the solution locally if you like
  • Not tested on Windows but as per the documentation here you will need to use Pageant as an SSH authentication agent

This repository uses AWS provided modules from Terraform Module Registry

Assumption

  • You have installed Terraform version which is >= 0.11.8
  • You are using CentOS 7.4
  • .PEM key is available under: "~/.ssh/". In my case it is called personal.pem
  • AWS credentials are available at "~/.aws/credentials" which should look like as follows:
[default]
aws_access_key_id = <KEY>
aws_secret_access_key = <SECRET>

Instructions

Please follow the instructions below to stand up Apache HTTPD + MySQL in AWS using Terraform:

eval $(ssh-agent)
ssh-add -k ~/.ssh/personal.pem
ssh-add -k ~/.ssh/id_rsa
git clone git@github.com:shazChaudhry/terraform-WebAppWithRDS.git && cd terraform-WebAppWithRDS
terraform init
terraform plan
terraform apply -auto-approve

Testing

  • In your favorite web browser, navigate to http://YOUR_EC2_WEBSERVER/calldb.php (you will need to check AWS console for the instance IP address)
  • Refresh the page a few time and note that number of rows will be incremented

Clean up

terraform show
terraform destroy -force