Multi-tier Architecture on AWS
Tags: aws
Published: 2021-07-18 18:30:16

# About
Build a multi-tier architecture project with various AWS services for real time environments.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jcmwykdympjdccouwia8.PNG)
* R53 - To create record sets within hosted zones.
* VPC - To create subnets, internet gateway, route tables, security groups.
* SNS - For notification services.
* ELB -  For load balancing.
* ACM - For  SSL certificates.
* EC2 - Instances with elastic IP and Auto Scaling group attached.
* RDS -  Master database with multi - AZ configuration.

# Steps :
# 1. Public and private subnets, IGW, NAT Gateway,etc.
`VPC`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wjmvvla2qwddj32aiohu.PNG)
`Public Subnet - 1`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahg3e21a5w0vhwh44d9y.PNG)
`Public Subnet - 2`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xpg4nfizdhio1jfgmrl.PNG)
`Private Subnet - 1`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qxvcbi2fexncz4ycvknf.PNG)
`Private Subnet - 2`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6jf39eumkia51o1dzvp4.PNG)
`Created`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jkgkxf5701dx2u1pac40.PNG)
`IGW`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m70l8yqvl158rmdh89zs.PNG)
`Attached to VPC`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bb57jyv4857paqi27fqc.PNG)
`Private Route table for VPC`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvs8pu7ti6r5hsqswcrr.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/exmi5hudx20cok5yn5fy.PNG)
`NAT Gateway with attached Elastic IP`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gmryttyxszyhjode3yt.PNG)
`Associate private subnets with NAT Gateway`
Associate NAT gateway to Private RT
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eepjo8r5b0wwz73cym3g.PNG)
Attach private subnets to Private RT
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rxsfdet09r9q9iv3cgwr.PNG)
Attach IGW to Public RT
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpegj3o3jqnoye0mq4mc.PNG)

VPC setup completed!
# 2. Create EC2 instance with Userdata (Bootstrapping) script.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6b7d7oc6hat2a48vz9y0.PNG)
Amazon Linux 2 -T2 micro
VPC - Komla-VPC
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w07dsuq6pauj88neo19k.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ksxhsl5pw0pxi4o8uac8.PNG)
`Create IAM role for EC2 service for S3 access`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rr8986ic9fo6n88pcax5.PNG)
`Attach it to the EC2 instance`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rv59vz3wupsfkeqpi92w.PNG)
`Create SNS topic`
 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f7fbii573dlqqnerh6d9.PNG)
`Add subscription email`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ohgh7sag64ckslxdoo1e.PNG)

EC2 setup complete!
# 3. Create RDS Security Group and RDS subnet group with Private Subnets.
`Create security group`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ehh8887f5rh2bals4zza.PNG)
`Provided access to only webserver SG`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cd807ssf730my3yl99we.PNG)
`Create Database`
Type - MySQL
VPC - Komla-VPC
SG - Security group for incoming traffic from EC2 on port 3306.
Instance - db.t2.micro
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chnblte2nbq9cbreja8j.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s99fm5z6ngdm7y892qn1.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y4jvs2qwb8zo92k8wctf.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jvfzxh3qtr9re4fat5f0.PNG)

Database created!
`Create ELB`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ud5t0v3re015x96qx6n.PNG)
VPC - Komla-VPC
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7q697nhbgpdbcoq96ryr.PNG)
Spread across Public subnets
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6nx5ygbyy242a7jjrx5.PNG)
Create SG for load balancer
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ali9z13zlm1o78dofp6s.PNG)
Health check parameters
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ldxw1dqb731abmz4h5c.PNG)
Add EC2 instance
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iwpd7syf3kuyqij5aeq8.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/95whklf1z9sxrlz6vnw1.PNG)
ELB created!
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqz4f4z77iwt30l0h96x.PNG)
Apache server operating. EC2 instance healthy! 
#4. Launch RDS instance with Multi-AZ.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j1uluwewfbd3rn4f2ikx.PNG)
# 5. Install WordPress.
Latest version of WordPress requires atleast php 5.6. Amazon t2 micro comes with php 5.4 standard. To bypass this issue I installed php 7. [Refer](https://stackoverflow.com/questions/34873685/how-to-install-php-7-on-ec2-t2-micro-instance-running-amazon-linux-distro#35258346).
>


               sudo amazon-linux-extras install -y php7.2
               sudo service httpd restart

>
Installing Wordpress: [Official tutorial](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/hosting-wordpress.html)
               wget https://wordpress.org/latest.tar.gz
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/00vspj31hv22hw1ed9cx.PNG)
Extracted the downloaded package from wordpress with 
`tar -xzf latest.tar.gz`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44sapjos9kprvga4ceck.PNG)
Moved the contents of wordpress directory to /var/www/html
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ytqosv7s3fsighhsnct.PNG)
`cd /etc/httpd/conf`
   edit httpd.conf
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rowv11f4cs18h21t83f6.PNG)
Search for "AllowOverride None" change `None` to `All`.
It is in `/var/www/html` directory.
Change permissions on html directory.
   `chown -R apache:apache /var/www/html`.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/guyqdm13aukl49ha1bap.PNG)
Wordpress opens on the Public IP of EC2 instance!
http://18.***.161.241/wp-admin/setup-config.php
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7xx0hupc7k6ker4vw8oq.PNG)
Check whether RDS is accessible via EC2 instance.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ygfjzrbvalavz4txho3k.PNG)
After configuring the dbname,username, password, hostname the wordpress application is up and running on Public IP of EC2 instance on the browser!
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r3bhuolce3kxirzt2rzr.PNG)
# 6. Create Route53 hosted zone to map with domain.
The ELB DNS is configured as an A record for the domain name `wordpress.komlalebu.com`.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29reb9pl1243wbb5h8j3.PNG)
# 7. Create AMI for EC2.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ru4immb7udq19qtktvxh.PNG)
# 8. Create ELB.
The EC2 instances are now accessed with the ELB DNS.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6hxrse7dlha5mn6fe480.PNG)
# 9. Create launch configuration from AMI.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9xka4kdogno063o6d78c.PNG)
Running two desired instances
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m54emit8l8272jscjipp.PNG)
# 10. Create ASG for launch configuration.
>
VPC - Komla-VPC
Subnets - Public subnets
ELB - Komla-ELB
Desired - 2 instances
Minimum - 1 instances
Maximun -  2 instances
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dsx1bt0ij5xpsb8pbo18.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rd90sat27k415g3gpn8i.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cnv145umnys8p8ara10b.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/skfabdb15rhwm6xrqbns.PNG)

Scaling policy
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80y1612hdgpo0lgdl9pt.PNG)
# 11. Health Check and fault tolerance.
Terminated all instances created by Auto Scaling for testing.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d0av9gk9rs4orenxkllu.PNG)
Health check failed and a new instance was created from AMI with ASG.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9hdhe9gmcl12h0dbcbi9.PNG)
Notification received in mail whenever instances are terminated or launched. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/86mfwy7i0sb93gu6i6f5.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1fxe3jl23gwh2e019d99.PNG)
Website up and running within minutes of server failure!