๐-๐๐ข๐๐ซ ๐๐ซ๐๐ก๐ข๐ญ๐๐๐ญ๐ฎ๐ซ๐ ๐ฐ๐ข๐ญ๐ก ๐๐๐ซ๐ซ๐๐๐จ๐ซ๐ฆ
Benefits of 2-Tier Architecture:A 2-tier architecture is like a simple way to build an application. Think of it as having two main parts: the front part that you interact with (like a website) and the back part where all the data is stored (like a database). This setup is easy to create and manage, making it great for smaller projects all the way to big enterprise businesses. Itโs cost-effective, faster to deploy, and can perform many applications.
# Provider and version and the region
provider "aws" {
region = "us-east-1"
}
# Define the VPC with a CIDR Block
resource "aws_vpc" "leonvpc" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
}
# Public Subnets (for the EC2 instance)
resource "aws_subnet" "publicsub1" {
vpc_id = aws_vpc.leonvpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"
map_public_ip_on_launch = true
tags = {
Name = "Publicsub1"
}
}
resource "aws_subnet" "publicsub2" {
vpc_id = aws_vpc.leonvpc.id
cidr_block = "10.0.2.0/24"
availability_zone = "us-east-1b"
map_public_ip_on_launch = true
tags = {
Name = "Publicsub2"
}
}
# Private Subnets (For the Relational DataBase)
resource "aws_subnet" "privatesub1" {
vpc_id = aws_vpc.leonvpc.id
cidr_block = "10.0.3.0/24"
availability_zone = "us-east-1a"
tags = {
Name = "Privatesub1"
}
}
resource "aws_subnet" "privatesub2" {
vpc_id = aws_vpc.leonvpc.id
cidr_block = "10.0.4.0/24"
availability_zone = "us-east-1b"
tags = {
Name = "Privatesub2"
}
}
# Creating the Internet Gateway (for public subnet access)
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.leonvpc.id
}
# Creating a Route Table for the public subnets
resource "aws_route_table" "public" {
vpc_id = aws_vpc.leonvpc.id
}
resource "aws_route" "internet_access" {
route_table_id = aws_route_table.public.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
# Associate the route table with public subnets
resource "aws_route_table_association" "publicassociation1" {
subnet_id = aws_subnet.publicsub1.id
route_table_id = aws_route_table.public.id
}
resource "aws_route_table_association" "publicassociation2" {
subnet_id = aws_subnet.publicsub2.id
route_table_id = aws_route_table.public.id
}
# Security Group for EC2 instances
resource "aws_security_group" "webserver_secg" {
name = "webserver_secg"
description = "Allow inbound traffic HTTP and SSH"
vpc_id = aws_vpc.leonvpc.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"]
}
}
# Security Group for Relational Data Base MySQL
resource "aws_security_group" "relationaldatabase_secg" {
name = "relationaldatabase_secg"
description = "Allow inbound traffic MySQL"
vpc_id = aws_vpc.leonvpc.id
ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = [aws_subnet.publicsub1.cidr_block, aws_subnet.publicsub2.cidr_block]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# EC2 Instance 1 (EC2 instance in Public Subnet 1)
resource "aws_instance" "leonEC21" {
ami = "ami-085ad6ae776d8f09c" # Update with a proper Amazon Linux AMI
instance_type = "t2.micro"
subnet_id = aws_subnet.publicsub1.id
vpc_security_group_ids = [aws_security_group.webserver_secg.id]
tags = {
Name = "leonEC21"
}
user_data = <<-EOT
#!/bin/bash
yum install -y httpd
service httpd start
chkconfig httpd on
EOT
}
# EC2 Instance 2 (EC2 instance in Public Subnet 2)
resource "aws_instance" "leonEC22nd" {
ami = "ami-085ad6ae776d8f09c" # Update with a proper Amazon Linux AMI
instance_type = "t2.micro"
subnet_id = aws_subnet.publicsub2.id
vpc_security_group_ids = [aws_security_group.webserver_secg.id]
tags = {
Name = "leonEC22nd"
}
user_data = <<-EOT
#!/bin/bash
yum install -y httpd
service httpd start
chkconfig httpd on
EOT
}
# Create RelationalDatabase MySQL Instance
resource "aws_db_instance" "mydb" {
identifier = "mydb"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t3.micro"
allocated_storage = 20
db_name = "mydatabase"
username = "admin"
password = "password9999"
db_subnet_group_name = aws_db_subnet_group.main.name
vpc_security_group_ids = [aws_security_group.relationaldatabase_secg.id]
multi_az = false
storage_type = "gp2"
skip_final_snapshot = true
}
# Data Base Subnet Group
resource "aws_db_subnet_group" "main" {
name = "main-subnet-group"
subnet_ids = [aws_subnet.privatesub1.id, aws_subnet.privatesub2.id]
tags = {
Name = "Main DataBase Subnet Group"
}
}_____________________Code Breakdown______________________
provider "aws" {
region = "us-east-1"
}โบThis first set of code shows the AWS provider and sets the region to us-east-1. Make sure every resource created has to same location of us-east-1
resource "aws_vpc" "leonvpc" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
}โบThe next block creates a VPC with a CIDR block 10.0.0.0/16, we will be enabling DNS support and DNS hostnames because instances in the VPC can be accessible using hostnames rather than IP addresses.
resource "aws_subnet" "publicsub1" {
vpc_id = aws_vpc.leonvpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"
map_public_ip_on_launch = true
tags = {
Name = "Publicsub1"
}
}
resource "aws_subnet" "publicsub2" {
vpc_id = aws_vpc.leonvpc.id
cidr_block = "10.0.2.0/24"
availability_zone = "us-east-1b"
map_public_ip_on_launch = true
tags = {
Name = "Publicsub2"
}
}โบThe next set of code resources is showing two public subnets within the VPC, each with a CIDR block and availability zone specified, you have to use the same location for availability zones and the only the only change was 1a and 1b. It cant be both 1a because the second subnet wont show up.
resource "aws_subnet" "privatesub1" {
vpc_id = aws_vpc.leonvpc.id
cidr_block = "10.0.3.0/24"
availability_zone = "us-east-1a"
tags = {
Name = "Privatesub1"
}
}
resource "aws_subnet" "privatesub2" {
vpc_id = aws_vpc.leonvpc.id
cidr_block = "10.0.4.0/24"
availability_zone = "us-east-1b"
tags = {
Name = "Privatesub2"
}
}โบWe will do the same thing again but for the private subnets, but these subnets will be for the relational database.
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.leonvpc.id
}โบCreating the Internet Gateway for the VPC, allowing public subnets to access the internet.
resource "aws_route_table" "public" {
vpc_id = aws_vpc.leonvpc.id
}
resource "aws_route" "internet_access" {
route_table_id = aws_route_table.public.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}โบThis route table is created and a route that directs internet traffic (0.0.0.0/0) to the internet gateway.
resource "aws_route_table_association" "publicassociation1" {
subnet_id = aws_subnet.publicsub1.id
route_table_id = aws_route_table.public.id
}
resource "aws_route_table_association" "publicassociation2" {
subnet_id = aws_subnet.publicsub2.id
route_table_id = aws_route_table.public.id
}โบAssociating the route table with the public subnets, enabling internet access for those subnets.
resource "aws_security_group" "webserver_secg" {
name = "webserver_secg"
description = "Allow inbound traffic HTTP and SSH"
vpc_id = aws_vpc.leonvpc.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"]
}
}โบThis set of security group rules allows inbound HTTP (port 80) and SSH (port 22) traffic from any IP address and allows all outbound traffic.
resource "aws_security_group" "relationaldatabase_secg" {
name = "relationaldatabase_secg"
description = "Allow inbound traffic MySQL"
vpc_id = aws_vpc.leonvpc.id
ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = [aws_subnet.publicsub1.cidr_block, aws_subnet.publicsub2.cidr_block]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}โบNext security group allows inbound MySQL (port 3306) traffic from the public subnets and allows all outbound traffic.
resource "aws_instance" "leonEC21" {
ami = "ami-085ad6ae776d8f09c" # Update with a proper Amazon Linux AMI
instance_type = "t2.micro"
subnet_id = aws_subnet.publicsub1.id
vpc_security_group_ids = [aws_security_group.webserver_secg.id]
tags = {
Name = "leonEC21"
}
user_data = <<-EOT
#!/bin/bash
yum install -y httpd
service httpd start
chkconfig httpd on
EOT
}โบWe are creating an EC2 instance in Public Subnet 1 with an Amazon Linux AMI and installs an Apache web server using the provided user data script.
resource "aws_instance" "leonEC22nd" {
ami = "ami-085ad6ae776d8f09c" # Update with a proper Amazon Linux AMI
instance_type = "t2.micro"
subnet_id = aws_subnet.publicsub2.id
vpc_security_group_ids = [aws_security_group.webserver_secg.id]
tags = {
Name = "leonEC22nd"
}
user_data = <<-EOT
#!/bin/bash
yum install -y httpd
service httpd start
chkconfig httpd on
EOT
}โบSecond resource creates an EC2 instance in Public Subnet 2 with an Amazon Linux AMI and installs an Apache web server using the provided user data script.
resource "aws_db_instance" "mydb" {
identifier = "mydb"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t3.micro"
allocated_storage = 20
db_name = "mydatabase"
username = "admin"
password = "password9999"
db_subnet_group_name = aws_db_subnet_group.main.name
vpc_security_group_ids = [aws_security_group.relationaldatabase_secg.id]
multi_az = false
storage_type = "gp2"
skip_final_snapshot = true
}โบNext resource creation will be a MySQL Relational Database instance with the specified settings, including the Database name, username, sizing,password, and subnet group.
resource "aws_db_subnet_group" "main" {
name = "main-subnet-group"
subnet_ids = [aws_subnet.privatesub1.id, aws_subnet.privatesub2.id]
tags = {
Name = "Main DataBase Subnet Group"
}
}โบThis resource creates a Data base subnet group for the Relational data base instance, using the private subnets.
terraform init
terraform validate
terraform fmt
terraform plan
terraform apply
terraform state list
terraform destroy_________Once You are satisfied with the code we can begin________
Get Leon B.โs stories inย yourย inbox
Join Medium for free to get updates fromย thisย writer.
โฆฟ init: this command initializes a Terraform configuration
โฆฟ validate: this command validates and checks the syntax errors and verifies the configuration files is acceptable arguments.
โฆฟ fmt: the format command will actually fix the files spacing and reformat as necessary
โฆฟ plan: this command will create an execution plan and show you what Terraform will do for you or what your desire was
โฆฟ apply:this command will apply all the configurations listed in the script and execute it
โฆฟ state list: this command will list all the resources created from the code you inputted
โฆฟ destroy: THIS IS THE LAST COMMAND WHEN YOU ARE FULLY FINISHED WITH YOUR PROJECT !! !! !!
โบI ran โterraform applyโ and all my resources created with no BACKFIRE!! now I ran the โterraform stateโ list which will list all the resources I created in my IAC code block.
___________________Lets See What We Cooked Up_____________
โบ Here is both the EC2 instances created.
โบ These are the Security group rules associated with the EC2 instances.
โบ All three security groups have been made for the VPC, EC2 instance, and Relational DataBase.
โบVPC has been created
โบ Subnets have been created
โบ DataBase created
โบ Route table also created with the subnet associations
โบ Here is the resource map showing the VPC, subnets rout table, and the network connections.
โบ I copied the public IP address from one of the EC2 instance and pasted into web browserโฆ Apache Web server is up and running.
Thanks you all for looking at my post and I hope you learned from it. You can reach me on my LinkedIn and Github.








