Complete AWS Infrastructure Using Terraform

Aman Jhagrolia
4 min readJul 25, 2020

--

AWS — VPC to EC2 Using Terraform

Terraform is an open-source infrastructure as a code software tool. It enables users to define and provision a datacenter infrastructure using a high-level configuration language known as Hashicorp Configuration Language, or optionally JSON.

Terraform is a great automation tool which can automate complete cloud infrastructure. After writing code, with just single command complete infrastructure can be created and destroyed.

Here I will create complete Infrastructure on AWS Cloud and then Deploy WordPress on it. Starting from VPC creation we’ll reach to Deploy WordPress as an EC2 Instance with MySQL Database. Everything is automated using terraform.

Here you can get my terraform code which is divided into different modules and multiple files. Now let's see what each module and file is doing.

variables.tf - This terraform file contains all the variables that are used to build the complete aws infrastructure

main.tf - This is main terraform file that has providers and outputs. Also, all other modules that are used are imported here.

VPC Module :-

This Module is for creating and configuring VPC Resources in AWS

vpc-and-subnets.tf - This terraform file is to create a VPC and two Subnets (Public Subnet and Private Subnet)

rt-and-ig.tf - This will create Internet Gateway and Route Table. After that, it will also associate Route Table with our Public Subnet.

EC2 Module :-

This module is for creating and configuring EC2 Resources in AWS

key-and-sg.tf - This terraform file will create key-pair and Security Groups for us. This will create two Security Groups, One for WordPress and another for MySQL.

instances.tf - This is the terraform code that launches the WordPress and MySQL instance for us. It will launch WordPress in Public Subnet and MySQL in Private Subnet.

On Applying Terraform Code :-

Now after writing complete terraform code we just need to apply it using “terraform apply” command and now terraform start building our Infrastructure.

Finally, we can see that 14 resources are created which include VPC, Subnets, Internet Gateway, Route Table, Key-Pair, Security Groups, Instances, etc. And in the output, it also gives the Private DNS Name of Database host i.e. MySQL which we will use while configuring the WordPress site.

Terraform Apply

Here is the status of the VPC Dashboard before and after applying the terraform code. We can clearly see that after applying the number of VPC resources is increased.

VPC Dashboard

Here are VPCs, One is the default which is provided by AWS and the Other one is that we have created just now using terraform.

VPCs

These are Subnets and two of them are just created in our VPC (One is Public and the other one is Private)

Subnets

One new Route Table is also created.

Route Tables

Here is my Internet Gateway that is created and attached to VPC

Internet Gateways

Two security groups are also created, one for WordPress which allows clients to connect to WordPress Site and another one is for MySQL Instance which allows WordPress Instance to connect to MySQL Database to store data.

Security Groups

One Key-pair is also created and using this we can log in to our WordPress Instance over SSH.

Key-Pairs

Here are our two instances running Mysql and WordPress. As WordPress is running in Public Subnet so Pubilc IP is associated with it, but MySQL is running in Private Subnet so we can see that there is no Public IP associated with it.

EC2 Instances

Finally, We are connected to our WordPress. Now we can configure and use it.

WordPress Site

As our MySQL Database is running in Private Subnet, So we can’t connect to it from the Public world, only our WordPress can connect to it through VPC internal Network, this is good for Security but In this complete scenario, we have one challenge that our MySQL Instance can't connect to the Internet. So if we need to update or download something in the future then we can't do so. We’ll Solve this Challange in the Next Article. Click here to see it!

At Last, I conclude my article in the Hope that it will help you, Thanks!!

--

--

No responses yet