Deploy Two Tier Architecture in AWS
Deploy Two Tier Architecture in AWS
Components:
1. VPC
2. Internet Gateway
3. Subnets - Private and public subnets
4. Load balancer
5. Route tables
6. RDS MySQL
region = "ap-south-1"
Creating VPC.tf
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/16"
tags = {
name = "Two-tier-vpc"
vpc_id = aws_vpc.vpc.id
tags = {
name = "two-tier-vpc-igw"
vpc_id = aws_vpc.vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "ap-south-1a"
map_public_ip_on_launch = true
tags = {
name = "public-1"
vpc_id = aws_vpc.vpc.id
cidr_block = "10.0.2.0/24"
availability_zone = "ap-south-1b"
map_public_ip_on_launch = true
tags = {
name = "public-2"
vpc_id = aws_vpc.vpc.id
cidr_block = "10.0.3.0/24"
availability_zone = "ap-south-1a"
map_public_ip_on_launch = false
tags = {
name = "private-1"
vpc_id = aws_vpc.vpc.id
cidr_block = "10.0.4.0/24"
availability_zone = "ap-south-1b"
map_public_ip_on_launch = false
tags = {
name = "private-2"
}
Creating Security-resources.tf
resource "aws_security_group" "alb-sg" {
name = "alb-sg"
vpc_id = aws_vpc.vpc.id
ingress {
from_port = "0"
to_port = "0"
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
egress {
from_port = "0"
to_port = "0"
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
# create ALB
name = "two-tier-alb"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.alb-sg.id]
name = "alb-tg"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.vpc.id
depends_on = [aws_vpc.vpc]
target_group_arn = aws_lb_target_group.alb-tg.arn
target_id = aws_instance.webserver_1.id
port = 80
depends_on = [aws_instance.webserver_1]
target_group_arn = aws_lb_target_group.alb-tg.arn
target_id = aws_instance.webserver_2.id
port = 80
depends_on = [aws_instance.webserver_2]
}
resource "aws_lb_listener" "listener_lb" {
load_balancer_arn = aws_lb.two_tier_alb.arn
port = "80"
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.alb-tg.arn
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
tags = {
name = "rt-igw"
subnet_id = aws_subnet.public_1.id
route_table_id = aws_route_table.rt-igw.id
}
subnet_id = aws_subnet.public_2.id
route_table_id = aws_route_table.rt-igw.id
name = "public-sg"
vpc_id = aws_vpc.vpc.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
egress {
from_port = 0
to_port =0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
name = "private-sg"
vpc_id = aws_vpc.vpc.id
ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = ["10.0.0.0/16"]
security_groups = [aws_security_group.public_sg.id]
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
egress {
from_port = 0
to_port =0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
Creating EC2.tf
resource "aws_instance" "webserver_1" {
ami = "ami-0e35ddab05955cf57"
instance_type = "t2.micro"
key_name = "two-tier-aws-terraform"
availability_zone = "ap-south-1a"
vpc_security_group_ids = [aws_security_group.public_sg.id]
associate_public_ip_address = true
subnet_id = aws_subnet.public_1.id
user_data = <<-EOF
#!/bin/bash
EOF
tags = {
name = "webserver-1"
}
}
resource "aws_instance" "webserver_2" {
ami = "ami-0e35ddab05955cf57"
instance_type = "t2.micro"
key_name = "two-tier-aws-terraform"
availability_zone = "ap-south-1b"
vpc_security_group_ids = [aws_security_group.public_sg.id]
associate_public_ip_address = true
subnet_id = aws_subnet.public_2.id
user_data = <<-EOF
#!/bin/bash
EOF
tags = {
name = "webserver-2"
}
}
Creating DB.tf
resource "aws_db_subnet_group" "db_subnet" {
name = "db_subnet"
allocated_storage =5
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
identifier = "db-instance"
db_name = "mydatabase"
username = "admin"
password = "password"
db_subnet_group_name = aws_db_subnet_group.db_subnet.id
vpc_security_group_ids = [aws_security_group.private_sg.id]
publicly_accessible = false
skip_final_snapshot = true
Let’s deploy!
configure aws through Vs code with Access Key ID and Secret Access Key
terraform init
2. EC2 instances
3. Load Balancer
4. Target Group