Deploy 3 Tier High Availability Application using Terraform in AWS

Hello All,

Today we will be deploying a 3 tier Application using Terraform in AWS.

Project Details and Link

AWS services: EC2 Instances, VPC, Nat Gateway, Internet Gateway, security groups, Classic Load balancer.

github link:- github.com/piya199616/Terraform-aws-project..

Project Diagram:

Pre-requisites -

  1. AWS Account

  2. Terraform installed in the local system

  3. AWS CLI installed in the Local system

  4. Knowledge of AWS and Terraform.

What we will provision:

  • 1 VPC ,6 subnets (public, Private, Database) ,1 Nat gateway in Public Subnet , 1 Internet Gateway attached to VPC.

  • 1 Bastion host and 2 private EC2 instances in 2 AZs.

  • 1 Classic Load balancer.

  • 3 Security Groups (bastion host, Private Ec2, and Load balancer)

  • with the help of provisioner, we will copy the file from local(private key file you copied) to the Bastion host, Run some commands on remote host.

Steps:

1. Install Terraform CLI on your local system and configure AWS credentials with AWS configure command.

2. Create a Terraform settings block that includes provider and terraform version details.

3 . Create a bash script for the installation of Httpd.

Note for every resource I have created a variable.tf, output. tf, and main_code.tf.

4. Create VPC for that I have used Modules:-

1 VPC, 6 subnets (Public, Private, Database),1 Nat gateway in Public Subnet, 1 Internet Gateway attached to VPC.

In GitHub refer c4–01-vpc-variables.tf contains variables

c4–02-vpc-module.tf actual code

c4–03-vpc-outputs.tf →all outputs related to VPC.

Module link- https://registry.terraform.io/modules/terraform-aws-modules/vpc/aws/latest

5. Create EC2 instances :-1 Bastion host and 2 private EC2 instances in 2 AZs.

Module link-https://registry.terraform.io/modules/terraform-aws-modules/ec2-instance/aws/latest

In GitHub refer-

c7–01-ec2instance-variables.tf

c7–02-ec2instance-outputs.tf

c7–03-ec2instance-bastion.tf

c7–04-ec2instance-private.tf

6. We will attach an EIP for the bastion host so that we can have a static IP.

In GitHub refer:-c8-elasticip.tf

7. Create a data source file so that it can dynamically fetch data related to the latest ami in AWS.

In GitHub refer:-c6-ami-datasource.tf

8. Create a Classic Load balancer.

Module link:-https://registry.terraform.io/modules/terraform-aws-modules/elb/aws/latest

In GitHub refer:-

c10–01-ELB-classic-loadbalancer-variables.tf

c10–02-ELB-classic-loadbalancer.tf

c10–03-ELB-classic-loadbalancer-outputs.tf

9. 3 Security Groups (bastion host, Private Ec2, and Load balancer)

In Github refer:-

c5–01-securitygroup-variables.tf

c5–02-securitygroup-outputs.tf

c5–03-securitygroup-bastionsg.tf

c5–04-securitygroup-privatesg.tf

c5–05-securitygroup-loadbalancersg.tf

10. with the help of the provisioner we will copy the file from the local(the private key file you copied) to the Bastion host with file provisioner, Run some commands on the remote host through Remote-provisioner.

In Github refer:-

c9-nullresource-provisioners.tf

11. For tags I have created a local file.

In git hub refer :- c3-local-values.tf

12. Now Run Terraform Init, Terraform validate, Terraform plan, and Terraform apply.

13. Verify your code :

1. Verify EC2 Instances
2. Verify Load Balancer SG
3. Verify Load Balancer Instances are healthy
4. Access sample app using Load Balancer DNS Name

14. Destroy your Resource with Terraform destroy command.

Fork my repo for more details, all code is present in my Github project link as mentioned above.

If you need any help then feel free to ping me on Linkedin:

Linkedin Profile:-linkedin.com/in/priyanka-burela