AWS multi-tire architecture  Project :
Tags: aws, cloud, webdev
Published: 2020-12-09 17:26:08

As we all know in today's era everybody is moving towards cloud because deploying and maintaining applications in our local environment could be compound and costly .

Here is the __architecture diagram__ below .

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

Explain :
The architecture comprises of a private network and a public network in which the load balancer resides where we can basically implement a path based routing . The separation of private network is to strength the security of our application where we make sure the integrity of our application .
This is a multi-tire architecture project which comprises of individual business logics individually scalable and monitorable .
 
# __Steps :__

__This project is build on `AWS` so you must have a `AWS` account and `ssh-client` like `putty` if you are on windows or for mac or Linux use `openssh` .__

### __First we have to create a VPC__ :
__VPC is an isolated space inside our region [More info for click me](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) .__ 

creating a __VPC__ with a name and with a __ipv4 CIDR(classes inter-Domain Routing is a method used to allocate IP address) block 10.1.0.0/16 range__ where the application is going to deploy . 

![VPC creation](https://dev-to-uploads.s3.amazonaws.com/i/na1jis5xji7pxa9439jf.png)

### __Creating a subnet for the access :__

Partitioning the network according to the public and private access . 
Let's define 4 regions (You can choose the number according to your need)  and later in the configuration part we can decide which one to deploy in the public and in the private network .
 
In our network all the internet access and non-internet access part is going to deploying in public and private __subnet__ respectively .

So the public subnet is having a public IP address and the private subnet is having a private IP address .

* __Configuration for our subnet :__

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

Let's have a look at the configuration of four of our subnets . 

tag - public-2a
region - 2a
VPC - main network
ipv4 - 10.1.1.0/24 this range is a part of the main network range 

tag - public-2b
region - 2b
VPC - main network
ipv4 - 10.1.2.0/24

tag - private-2a
region - 2a
VPC - main network
ipv4 - 10.1.3.0/24

tag - private-2b
region - 2b
VPC - main network
ipv4 - 10.1.4.0/24

### __Create an Internet gateway :__
suppose you want to have a internet access in your home then you contact to the internet service provide to bring up a connection to your home . similarly here we have to create a __internet gateway__ for the same purpose .

![Internet-gateway](https://dev-to-uploads.s3.amazonaws.com/i/47rfqg1ga7hh0ccgmgmj.png)

## __steps :__
1. create a new internet gateway .
2. attach it to your VPC

when you deploy a resource in the cloud it is always deployed in the subparts . just like you have WIFI in the home and you do not have a active connection to that then your laptop does not know weather to connect to the WIFI or not .

### __Creating route tables :__
So we have created 4 subparts of our main network then we just have to define which one to give the internet access and which one to not .

![route-table](https://dev-to-uploads.s3.amazonaws.com/i/hliqv8n34136pefrt4gs.png)

1. create two one is __Active__ and another one is __non-active__ 
2. Go to the actions -- connect the __public subnets__ to the __Active route-table__ and then
 the __private subnets__ to the -- __non-active route-table__ .
3. Make sure you have deployed both of them in your main network (VPC) .

###__Now we have to specify the route to the gateway .__

* __Steps :__
1. Select the gateway which we want to give the internet access and in our case it is named as __Internet__ .

2. Edit routes --> Add a route 

3. 0.0.0.0/0 internet gateway

* we connected two subnets to the route table and they can connect to the internet by using the internet gateway .

* All the resources that we are going to communicate with each other because they are the part of the same network .

* __So consider a scenario where__ in your __home network__ your laptop and mobile is connected which leads the communication between two . 

* The subparts can communicate with each other whether they have the internet access or not . Which make sure the communication to our private network . This kind of division ensures the security of our infrastructure .

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


###The resources which are public subnet we have to assign a public __IP address__ to them  in order to talk to them .

* Just select your public network and then go to actions modify auto assign IP and then tick the enable public assign IP .

* __steps:__
1. subnets 
2. public-2a
3. modify auto.... 
4. enable auto assign 
5. save

![assign IP address](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5yr3y4fvfliat0zvp627.png)

![assign IP address](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ixdd2kzk8tlfp7gjh92.png)

Public IP address is the address which can be reach out to the network .

* All the address we have specified earlier are the __local IP address.__

###__Lets Move to the application deployment part :__

* __Steps :__
1. Create a new EC2 instance  
2. in the configure instance 
3. network -- main-network 
4. subnet -- public subnet
5. go with the __default firewall__ as of now and then we have to restrict the firewall later .
6. Then lunch it .

7. select the security group 
8. edit inbound rule -- > my IP  

* Connect it to your machine :
```ssh -i pem file and host@ipaddr```

Installing the webserver for the access to our webpage .

```sudo apt install apache2```

```$ cd /var/www/html```

So this is a sample blogging project . where i have two routes as home and the about route . Obviously same steps can be followed in different real life projects . 

create a new index.html page with the content as follows :
```html
<h1> Welcome to the homepage </h1>
```

###__Create an AMI of the home-page :__
home page is now ready to deploy in the private subnet .

* __Steps:__
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/69jv693o0dvmdmlkt0os.png)

* And then name it as you wish here i name it as __blog-home__

* Lets again go to the same path where our home page id resides  and then change the index.html .
```html
<h1>This is the about page</h1>
```

* Then create an AMI out of it . By following the same steps but here we have to name it as __about-page__ .

### __Configure both of the images :__
We have to give two of our web pages public-2a and public-2b as the subnet . And make sure they reside in our main-network(VPC) .

* __Steps :__
1. Lunch Instances
2. My AMIs

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

3. Select main-network(VPC) 
4. private-2a (For the home page) 
5. default security group .

* And for our about page select the subnet in the private-2b and rest of all are same .

###__Note :__
If you see it not has a public IP and it has a private IP address, 
which is only reachable from our __public instance__ . which in our case the __proxy-server__ .

After creating both the AMI's we now can have a look at our next architecture .
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wi8rs6aq3bdo6re8awud.png)

As per the diagram we have to define a load-balancer which can indeed talk to our AMIs . As per the path based routing specified routing .

### __Make sure we can connect to our private networks through the proxy-sever :__

* __Steps:__
1. Copy the pem file which is in our local system to the proxy server . 
```$ scp -i pemfile ubuntu@publicip:/home/ubuntu```

2. Next connect to the private server(blog-home) through __private IP .__

```$ ssh -i pemfile ubuntu@privateip```

If you are trying to connect to one of our machine which is deployed in the private subnet it could not because it is not a part of our main network .

* check in both home and about server if the apache server is running or not .

### __Configure our load balancer to connect to the server .__
![cloud-load-balncer](https://dev-to-uploads.s3.amazonaws.com/i/wbycj12kdoattn8gp63e.png)

* __Steps :__
1. load-balancer
2. create a load balance
3. application load balancer (which helps in path based routing) 

* __Configuration :__
1. name --> blog-demo
2. Protocol --> HTTP
3. VPC--> main
4. availability jones public-2a and public-2b
5. default firewall

# __Make sure you have the security group same as your servers . otherwise it will throw an error says server is not healthy__

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

6. routing give it a name and then select the blog-demo and blog-about .
 

### __target group (set of severs that you can connect to)__
1.name --> home-page-server
2. next --> select main-home
3. home target group should have the home server attached if not then edit it .
4. create one target group for about-page-server 

### __Then edit rules for the Load-balancer :__
* __Steps :__
1. load balancer 
2. in the listeners 
3. edit rules 
4. configure the path /about* --> about-page-server --> save
5. configure the path /home* --> home-page-server --> save

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

Then copy the load balancer DNS URL and paste it in the browser .

```$
ssh to stream server .

cd /var/www/html
mkdir watch
cp index.html /about

ssh to home server . 

cd /var/www/html
mkdir home
cp index.html /home
```

Now our servers are inside a private subnet and we can only connect to them through using a proxy server that we have deployed in our public region .

### __NAT Gateway :__

In case we need to update our server or the application then we could not do in our current architecture . We have to configure `NAT` (Network address translation) which converts the private IP to public and then we can communicate to the Internet .

![AWS NAT gateway](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cllgvzmsoywf6ni2kf3z.png) 

#### Steps to attach a NAT gateway 
1. Navigate to the Nat Gateways 
2. create a NAT Gateway
![Nat setup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwyaq3cn9xniozsczzvs.png)

3. Edit the private route table routing configuration .
Destination : 0.0.0.0/0 
Target : NAT-gateway
![create nat](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dd72ypu6iot3xj6zg7ld.png)
__Now your private instance is attached to the internet .__
```$ping www.google.com```


# Thank You For reading 🔥🔥🔥