Skip to content

Commit

Permalink
Added opentofu project for creating a custom VPC on AWS
Browse files Browse the repository at this point in the history
  • Loading branch information
vinod827 committed May 5, 2024
1 parent 4e3abfd commit 11a70f6
Show file tree
Hide file tree
Showing 11 changed files with 756 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
./webapp/node_modules/*
./webapp/package-lock.json
webapp/node_modules/
webapp/package-lock.json
webapp/package-lock.json
iac/aws/terraform/creating-custom-vpc/.terraform/
20 changes: 20 additions & 0 deletions iac/aws/terraform/creating-custom-vpc/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions iac/aws/terraform/creating-custom-vpc/000.provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
variable aws_region {
default = "us-east-1"
description = "AWS region where the resources will be provisioned"
}

# Configure the AWS Provider
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
# helm = {
# source = "hashicorp/aws"
# version = "~> 2.6"
# }
}
}

# Configure region and profile
provider "aws" {
region = var.aws_region
profile = "myaws"
}
10 changes: 10 additions & 0 deletions iac/aws/terraform/creating-custom-vpc/001.vpc.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "aws_vpc" "mycustomvpc" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true

tags = {
"owner" = "vinod"
"Name" = "my custom VPC"
}
}
7 changes: 7 additions & 0 deletions iac/aws/terraform/creating-custom-vpc/002.internet.gateway.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.mycustomvpc.id
tags = {
"owner" = "vinod"
"Name" = "IGW"
}
}
45 changes: 45 additions & 0 deletions iac/aws/terraform/creating-custom-vpc/003.subnets.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
resource "aws_subnet" "private-us-east-1a" {
vpc_id = aws_vpc.mycustomvpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"

tags = {
"subnet" = "private-us-east-1a"
"Name" = "Private Subnet"
}
}

resource "aws_subnet" "private-us-east-1b" {
vpc_id = aws_vpc.mycustomvpc.id
cidr_block = "10.0.2.0/24"
availability_zone = "us-east-1b"

tags = {
"subnet" = "private-us-east-1b"
"Name" = "Private Subnet"
}
}

resource "aws_subnet" "public-us-east-1a" {
vpc_id = aws_vpc.mycustomvpc.id
cidr_block = "10.0.3.0/24"
availability_zone = "us-east-1a"
map_public_ip_on_launch = true

tags = {
"subnet" = "public-us-east-1a"
"Name" = "Public Subnet"
}
}

resource "aws_subnet" "public-us-east-1b" {
vpc_id = aws_vpc.mycustomvpc.id
cidr_block = "10.0.4.0/24"
availability_zone = "us-east-1b"
map_public_ip_on_launch = true

tags = {
"subnet" = "public-us-east-1b"
"Name" = "Public Subnet"
}
}
24 changes: 24 additions & 0 deletions iac/aws/terraform/creating-custom-vpc/004.nat.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

resource "aws_eip" "nat" {
vpc = true

tags = {
"Name" = "EIP"
"Owner" = "Vinod"
}

}

resource "aws_nat_gateway" "nat" {
allocation_id = aws_eip.nat.id
subnet_id = aws_subnet.public-us-east-1a.id

tags = {
"Name" = "NAT Gateway"
"Owner" = "Vinod"
}

# To ensure proper ordering, it is recommended to add an explicit dependency
# on the Internet Gateway for the VPC.
depends_on = [aws_internet_gateway.igw]
}
42 changes: 42 additions & 0 deletions iac/aws/terraform/creating-custom-vpc/005.routes.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
resource "aws_route_table" "privateroute" {
vpc_id = aws_vpc.mycustomvpc.id

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

tags = {
Name = "private"
}
}

resource "aws_route_table" "publicroute" {
vpc_id = aws_vpc.mycustomvpc.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}

tags = {
Name = "public"
}
}

resource "aws_route_table_association" "privateassociation_a" {
subnet_id = aws_subnet.private-us-east-1a.id
route_table_id = aws_route_table.privateroute.id
}
resource "aws_route_table_association" "privateassociation_b" {
subnet_id = aws_subnet.private-us-east-1b.id
route_table_id = aws_route_table.privateroute.id
}
resource "aws_route_table_association" "publicassociation_a" {
subnet_id = aws_subnet.public-us-east-1a.id
route_table_id = aws_route_table.publicroute.id
}
resource "aws_route_table_association" "publicassociation_b" {
subnet_id = aws_subnet.public-us-east-1b.id
route_table_id = aws_route_table.publicroute.id
}
6 changes: 6 additions & 0 deletions iac/aws/terraform/creating-custom-vpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Description
Creating a custom VPC using OpenTofu.

## Architecture
![Custom AWS VPC](https://drive.google.com/file/d/1-1enJhmxFLkUp2jaqOkKGFqueP6cF68W/view?usp=sharing)

Loading

0 comments on commit 11a70f6

Please sign in to comment.