Skip to content

Terraform Project for AWS Infrastructure..1 VPC ○ 3 public subnets ○ 3 private subnets ○ 3 Private routing tables ○ 1 public routing table ○ 1 internet gateway ○ 3 Elastic IP's for NAT Gateways ○ 3 NAT GateWays

Notifications You must be signed in to change notification settings

Tks-Devops/Terraform-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Terraform Project for AWS Infrastructure

Step 1: Install Terraform

Download and install Terraform from terraform.io.

Verify installation:

terraform --version

Step 2: Create Terraform Files

Create a new directory for Terraform:

mkdir terraform_project && cd terraform_project

Create main.tf:

nano main.tf

Paste the following configuration:
HCL

provider "aws" {
  region = "us-east-1"
}

# Fetch Availability Zones
data "aws_availability_zones" "available" {}

# Create VPC
resource "aws_vpc" "main" {
  cidr_block           = "10.0.0.0/16"
  enable_dns_support   = true
  enable_dns_hostnames = true
  tags = {
    Name = "main-vpc"
  }
}

# Create Public Subnets
resource "aws_subnet" "public" {
  count                   = 3
  vpc_id                  = aws_vpc.main.id
  cidr_block              = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)
  availability_zone       = element(data.aws_availability_zones.available.names, count.index)
  map_public_ip_on_launch = true
  tags = {
    Name = "public-subnet-${count.index + 1}"
  }
}

# Create Private Subnets
resource "aws_subnet" "private" {
  count             = 3
  vpc_id            = aws_vpc.main.id
  cidr_block        = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index + 3)
  availability_zone = element(data.aws_availability_zones.available.names, count.index)
  tags = {
    Name = "private-subnet-${count.index + 1}"
  }
}

# Create Internet Gateway
resource "aws_internet_gateway" "main" {
  vpc_id = aws_vpc.main.id
  tags = {
    Name = "main-internet-gateway"
  }
}

# Create Public Route Table
resource "aws_route_table" "public" {
  vpc_id = aws_vpc.main.id
  tags = {
    Name = "public-route-table"
  }
}

# Add Route to Internet Gateway in Public Route Table
resource "aws_route" "public_internet_access" {
  route_table_id         = aws_route_table.public.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.main.id
}

# Associate Public Subnets with Public Route Table
resource "aws_route_table_association" "public" {
  count          = 3
  subnet_id      = aws_subnet.public[count.index].id
  route_table_id = aws_route_table.public.id
}

# Create Elastic IPs for NAT Gateways
resource "aws_eip" "nat" {
  count = 3
  vpc   = true
  tags = {
    Name = "nat-eip-${count.index + 1}"
  }
}

# Create NAT Gateways
resource "aws_nat_gateway" "nat" {
  count         = 3
  allocation_id = aws_eip.nat[count.index].id
  subnet_id     = aws_subnet.public[count.index].id
  tags = {
    Name = "nat-gateway-${count.index + 1}"
  }
}

# Create Private Route Tables
resource "aws_route_table" "private" {
  count  = 3
  vpc_id = aws_vpc.main.id
  tags = {
    Name = "private-route-table-${count.index + 1}"
  }
}

# Add Routes to NAT Gateways in Private Route Tables
resource "aws_route" "private_nat_access" {
  count                  = 3
  route_table_id         = aws_route_table.private[count.index].id
  destination_cidr_block = "0.0.0.0/0"
  nat_gateway_id         = aws_nat_gateway.nat[count.index].id
}

# Associate Private Subnets with Private Route Tables
resource "aws_route_table_association" "private" {
  count          = 3
  subnet_id      = aws_subnet.private[count.index].id
  route_table_id = aws_route_table.private[count.index].id
}

Initialize and apply:
bash

terraform init
terraform apply

About

Terraform Project for AWS Infrastructure..1 VPC ○ 3 public subnets ○ 3 private subnets ○ 3 Private routing tables ○ 1 public routing table ○ 1 internet gateway ○ 3 Elastic IP's for NAT Gateways ○ 3 NAT GateWays

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published