terraform-deploy-wordpress-site / network.tf
network.tf
Raw
# creating vpc
resource aws_vpc "wp_vpc"{
  cidr_block = var.vpc_cidr

  tags = {
    Name = "wp_vpc"
  }
}
# creating ELB subnet A
resource "aws_subnet" "elb_subnetA" {
  vpc_id = aws_vpc.wp_vpc.id
  cidr_block = var.elb_subnetA
  availability_zone = var.az_1

  tags = {
    Name = "elb_subnetA"
  }
}

# creating ELB subnet B
resource "aws_subnet" "elb_subnetB" {
  vpc_id = aws_vpc.wp_vpc.id
  cidr_block = var.elb_subnetB
  availability_zone = var.az_2

  tags = {
    Name = "elb_subnetB"
  }
}

# creating server subnet A
resource aws_subnet "server_subnetA"{
  vpc_id = aws_vpc.wp_vpc.id
  cidr_block = var.server_subnetA
  availability_zone = var.az_1

  tags = {
    Name = "server_subnetA"
  }
}
# creating server subnet B
resource aws_subnet "server_subnetB"{
  vpc_id = aws_vpc.wp_vpc.id
  cidr_block = var.server_subnetB
  availability_zone = var.az_2

  tags = {
    Name = "server_subnetB"
  }
}

# creating database subnet A
resource aws_subnet "db_subnetA"{
  vpc_id = aws_vpc.wp_vpc.id
  cidr_block = var.db_subnetA
  availability_zone = var.az_1
  tags = {
    Name = "db_subnetA"
  }
}

# creating database subnet B
resource aws_subnet "db_subnetB"{
  vpc_id = aws_vpc.wp_vpc.id
  cidr_block = var.db_subnetB
  availability_zone = var.az_2
  tags = {
    Name = "db_subnetB"
  }
}

# creating an internet gateway
resource "aws_internet_gateway" "wp_igw" {
  vpc_id = aws_vpc.wp_vpc.id

  tags = {
    Name = "wp_igw"
  }
}

# creating a public route table for ELB
resource "aws_route_table" "public_rtb" {
  vpc_id = aws_vpc.wp_vpc.id

  tags = {
    Name = "public_rtb"
  }
}

# creating a route for ELB
resource "aws_route" "public_rt" {
  route_table_id = aws_route_table.public_rtb.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id = aws_internet_gateway.wp_igw.id

}

# associating the route table to elb subnet A
resource "aws_route_table_association" "public_rtb_assoc1" {
  subnet_id = aws_subnet.elb_subnetA.id
  route_table_id = aws_route_table.public_rtb.id
}

# associating the route table to elb subnet B
resource "aws_route_table_association" "public_rtb_assoc2" {
  subnet_id = aws_subnet.elb_subnetB.id
  route_table_id = aws_route_table.public_rtb.id
}
# creating an elastic IP for a NAT gateway
resource "aws_eip" "Nat-Gateway-EIP" {
  depends_on = [
    aws_route_table_association.public_rtb_assoc1
  ]
  vpc = true
}
# Creating a NAT gateway in public subnet 1
resource "aws_nat_gateway" "cloudNAT" {
  depends_on = [
    aws_eip.Nat-Gateway-EIP
  ]

  # Allocating the Elastic IP to the NAT Gateway!
  allocation_id = aws_eip.Nat-Gateway-EIP.id
  
  # Associating it in the Public Subnet!
  subnet_id = aws_subnet.elb_subnetA.id
  tags = {
    Name = "NAT gateway 1"
  }
}

# Creating a Route Table for the Nat Gateway 
resource "aws_route_table" "private_rtb" {
  depends_on = [
    aws_nat_gateway.cloudNAT
  ]

  vpc_id = aws_vpc.wp_vpc.id

  route {
    cidr_block = "0.0.0.0/0"
    nat_gateway_id = aws_nat_gateway.cloudNAT.id
  }

  tags = {
    Name = "Route Table for NAT Gateway"
  }

}

# Associating route table for NAT gateway to server subnetA
resource "aws_route_table_association" "private_rtb_assoc1" {
  depends_on = [
    aws_route_table.private_rtb
  ]

#  Private Subnet ID for adding this route table to the DHCP server of Private subnet!
  subnet_id      = aws_subnet.server_subnetA.id

# Route Table ID
  route_table_id = aws_route_table.private_rtb.id
}

# Associating route table for NAT gateway to server subnet B
resource "aws_route_table_association" "private_rtb_assoc2" {
  depends_on = [
    aws_route_table.private_rtb
  ]

#  Private Subnet ID for adding this route table to the DHCP server of Private subnet!
  subnet_id      = aws_subnet.server_subnetB.id

# Route Table ID
  route_table_id = aws_route_table.private_rtb.id
}