Using Terraform with AWS

Azeemushan Ali
6 min readSep 19, 2020

In this article we are going to discuss how to create vpc , subnet , Internet gateway , routing table and launch Wordpress and MySQL instances on the top of public cloud aws using Terraform.

Pre-Requisites

  • We require AWS IAM API keys (access key and secret key) for creating and deleting permissions for AWS resources.
  • Terraform should be installed on the Local VM.

Amazon Resources Created Using Terraform

Statement: We have to create a web portal for our company with all the security as much as possible.

So, we use Wordpress software with dedicated database server.

Database should not be accessible from the outside world for security purposes.

We only need to public the WordPress to clients.

So here are the steps for proper understanding!

Steps:

1) Write a Infrastructure as code using terraform, which automatically create a VPC.

2) In that VPC we have to create 2 subnets:

  • Public subnet [ Accessible for Public World! ]
  • Private subnet [ Restricted for Public World! ]

3) Create a public facing internet gateway for connect our VPC/Network to the internet world and attach this gateway to our VPC.

4) Create a routing table for Internet gateway so that instance can connect to outside world, update and associate it with public subnet.

5) Launch an ec2 instance which has Wordpress setup already having the security group allowing port 80 so that our client can connect to our WordPress site.

Also attach the key to instance for further login into it.

6) Launch an ec2 instance which has MYSQL setup already with security group allowing port 3306 in private subnet so that our wordpress vm can connect with the same.

Also attach the key with the same.

Note: Wordpress instance has to be part of public subnet so that our client can connect our site.

MySQL instance has to be part of private subnet so that outside world can’t connect to it.

Don’t forgot to add auto ip assign and auto dns name assignment option to be enabled.

AWS VPC

Amazon Virtual Private Cloud (Amazon VPC) lets you provision a logically isolated section of the AWS Cloud where you can launch AWS resources in a virtual network that you define. You can use both IPv4 and IPv6 in your VPC for secure and easy access to resources and applications.

Time to Work fellas

I have created a folder name as “Hybrid-T3 and a file to write all the Terraform in this file name task3.tf .

Step-1 — Create VPC

First we need to create provider . The Amazon Web Services (AWS) provider is used to interact with the many resources supported by AWS. The provider needs to be configured with the proper credentials before it can be used.

Below Terraform code is to create aws VPC in which

cidr_block — is, When we create a VPC, you must specify a range of IPv4 addresses for the VPC in the form of a Classless Inter-Domain Routing (CIDR) block; for example, 10.0. 0.0/16.

Step-2-a — Create public subnet

A public subnet is a subnet that’s associated with a route table that has a route to an Internet gateway. A private subnet with a size /24 IPv4 CIDR block for example “192.168.0.0/24”

Step-2-a :- Create private subnet

now both the subnet are private, to make public one of the above subnet we need to create route table and Internet gateway for vpc and then associate to public subnet.

Step-3 — Create Internet gateway

An internet gateway serves two purposes: to provide a target in your VPC route tables for internet-routable traffic, and to perform network address translation (NAT) for instances that have been assigned public IPv4 addresses.

Step-4 — Create Routing table

A route table contains a set of rules, called routes, that are used to determine where network traffic from your subnet or gateway is directed.

After creating route table associate it with the one of the subnet to make it public.

Step-5 — Create security group and launch Wordpress instances

Below code is to create security group for WordPress.

A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic.

Below is the code to launch WordPress EC2 instances.

Step-5 — Create security group and launch MySQL instances

Below code is to create security group for WordPress.

Below code is to launch MySQL database EC2 instances.

now initialize the tera tera directory using command.

terraform init

now to run the code use

terraform apply -auto-approve

now access the WordPress using WordPress public ip .

and to connect WordPress with MySQL database we need instance id of MySQL .

This is the final web page of WordPress .

and good practice to check things manually. Whether our terraform code has configured everything correctly or not .

VPC

Public Subnet

Private Subnet

Internet Gateway

Routing Table

Security Group

Everything looks great !! And here comes a pro tip —

“ You can also take this same setup to another level, much more secured”

Read this for detailed instruction —

All the codes discussed earlier can be found on my Github Repo & connect with me on Linkedin !!

Thank you Everyone for reading .!! Bella Ciao

--

--