# AWS Networking using Terraform.

AWS networking with Infrastructure as Code

#### 1

#### Creating VPC, subnets, Internet Gateway, Route Table, EC2

### What do we want to do?

Statement: We have to create a web portal for our company with all the security as much as possible. So, we use the WordPress software with a dedicated database server.

The database should not be accessible from the outside world for security purposes. We only need public WordPress for clients. So here are the steps for proper understanding!

#### Steps:

1.  Write an Infrastructure as code using Terraform, which automatically creates a VPC.
2.  In that VPC we have to create 2 subnets:

a) public subnet \[ Accessible for Public World! \]

b) private subnet \[ Restricted for Public World! \]

3\. Create a public-facing internet gateway to 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 the outside world, update and associate it with the public subnet.

5\. Launch an EC2 instance that 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 the instance for further login into it.

6\. Launch an EC2 instance that has MYSQL setup already with security group allowing port 3306 in a 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 the public subnet so that our client can connect our site. MySQL instance has to be part of a private subnet so that the outside world can’t connect to it.

*Don’t forget to add auto IP assign and auto DNS name assignment options to be enabled.*

I have used Code snippets images for better visualization. You can find code this code file in this [Github repository](https://github.com/ShubhamRasal/portfolio).

### Pre-requisite :

1.  AWS account.
2.  [Download AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-windows.html) and [Configure it](https://medium.com/@developer.shubham.rasal/create-aws-ec2-instance-using-terraform-3a3a2d273048).
3.  Download terraform. [*Download*](https://www.terraform.io/downloads.html)

### Let’s Start…

### 1\. Configure the Provider

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729541209/25071e2c-d53c-4a65-a301-f62f2e37ab13.png)

### 2\. Create VPC

Below code creates VPC with given cider block, the tenancy is default and we want DNS hostname URL for that we are enabling it. VPC will create in the region you have mentioned in the above provider resource.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729542742/1e6c2432-71f5-4c45-a45e-8d1500023bf2.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729545197/a964cb22-87fc-4055-a7cc-12eb1ace6796.png)

### 2\. Create two subnets in VPC

Here we are just creating two subnets and we named public and private. A VPC spans all of the Availability Zones in the Region. After creating a VPC, we want to add **subnets** in two different Availability Zone.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729547456/6a7ca1b2-777e-48bf-b71b-8792c4ad3521.png)

above code will create two subnets named *‘public\_subnet*’ and ‘*private\_subnet*’ in **‘*ap-south-1a’*** and **‘*ap-south-1b’***

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729550073/9b4d950d-54b6-4d47-a0e2-206aefcd1a66.png)

### 3\. Create Internet Gateway

An **internet gateway** is a horizontally scaled, redundant, and highly available VPC component that allows communication between your VPC and the **internet**. [Read More](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Internet_Gateway.html)

![](https://cdn-images-1.medium.com/max/800/1*T81FVMkOgGVrPTCED9uouA.png)

we want to connect our VPC to the internet so that’s why we need to create Internet Gateway.

![](https://cdn-images-1.medium.com/max/800/1*nbK85k0lmH3m8Fdp5M0Jew.png)

### 4\. Create a Route 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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729556552/da7625d1-1af5-4790-af80-97578181592a.png)

to create route table we have used *aws\_route\_table* resource. We want to access the internet so add cider\_block to `0.0.0.0/0` , (quad-zero route) and use the internet gateway that we create above. The destination for the route is `0.0.0.0/0` , which represents all IPv4 addresses. The target is the internet gateway that's attached to your VPC.

![](https://cdn-images-1.medium.com/max/800/1*VRgJpd04XVQJGpCGxzGU5w.png)

To read more about the Route table you can refer to this. [ROUTE\_TABLE](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Route_Tables.html)

### 5\. Associate Route table with Public Subnet

Now we want to use the internet only for public subnet so associate route table with that subnet only. To do this we will use *aws\_route\_table\_association* resource in terraform which takes subnet id and route table id.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729561844/cc98cf5b-6368-4f02-a821-80d8f0e4e3bf.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729563629/6b2dcd6b-a44a-4e4e-9b51-bbc8a4f0fb93.png)

Now we have created VPC and created two subnets in different Availability Zones in the region and given Internet connection by creating an Internet gateway for VPC. Now lets launch instances of WordPress and MySQL.

### 6.Create Key Pair 🔑

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729565435/be00c17f-9a78-43fe-96f8-08bd253fca86.png)

![](https://cdn-images-1.medium.com/max/800/1*PzWezZ7X_zQkrF0IKsDhVQ.png)

We are using here tls\_private\_key for creating two resources and resource local\_file is used to store this key locally. aws\_key\_pair is used to create key-pair in AWS and will attach this key in AWS.

### 7\. Create a Security Group for WordPress

Ingress rules are for the traffic that enters the boundary of a network. Egress rules imply to traffic exits instance or network.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729569614/0887724b-b1d3-4eb7-9fcf-7187f02035ff.png)

We want to manage and configure this instance so we are giving the ssh ingress/inbound rule and we are launching WordPress so access it so we are enabling HTTP inbound traffic for all. You can notice we have create a security group in *myvpc*.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729571294/5eee3edb-9560-4df7-b4f6-b327f3cbcda4.png)

### 8\. Launch WordPress EC2 instance 🌍

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729573561/34dcc646-de5b-4817-81ef-630e4e0433a3.png)

we use the Linux EC2 instance, **public subnet in our VPC** and attach key-pair that we have created above. Also, we have attached the security group. We want IP address that’s why we need to give *associate\_public\_ip\_address* value true.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729575386/b2f7eff5-5e9a-4267-a87d-4d3b1814f24d.png)

### 9\. Create a Security Group for MySQL

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729577952/d2964be2-ec9f-4e30-ad13-3e993653080b.png)

Create a Security group allowing SSH and MySQL port.

![](https://cdn-images-1.medium.com/max/800/1*H5Hm7gerTu0LEpijH7l7uA.png)

### 10\. Launch MySQL EC2 instance 🌍

![](https://cdn-images-1.medium.com/max/800/1*Bu6_YcOToMqApy5SLXz_oQ.png)

We launch MySQL instance in **a private subnet** with the Security group that we have created for MySQL.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729584514/cd2c2b29-da64-4a10-b3b1-f3ff8936fb9a.png)

### **11\. Installation**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729586416/82bc8cc2-6adb-48b3-b472-2ee17a810989.png)

Using null\_resource and remote-exec provisioner we install WordPress using docker in EC2 instance that we have created for WordPress. Copy WordPress instance IP and paste in the browser you will see below output.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729589327/33683755-2bfe-4d73-85ce-e541cd251374.png)

### 12.Configuration MYSQL

You can connect MYSQL instance using SSH from WordPress instance and install and configure MySQL server. **OR** You can directly select MySQL ami.

You can refer to this link on [How to Installing and Configuring MySQL on Linux](https://docs.oracle.com/javacomponents/advanced-management-console-2/install-guide/mysql-database-installation-and-configuration-advanced-management-console.htm#JSAMI122).

after configuration MySQL, you can give hostname and database name to WordPress, and your website will be ready.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685729591911/a449c666-461b-4a7e-9cbc-1270f208fdf6.png)

### Conclusion

We have created VPC, two subnets, internet gateway, route table,ec2 instances as per need. You can find this code in this [repository.](https://github.com/ShubhamRasal/aws-terraform)

Don’t forget to give clap if you like and feel helpful.

This task was given by [Mr.Vimal Daga](https://www.linkedin.com/in/vimaldaga) sir. I thank Vimal sir for mentoring me.
