Skip to content

Commit 11a70f6

Browse files
committed
Added opentofu project for creating a custom VPC on AWS
1 parent 4e3abfd commit 11a70f6

File tree

11 files changed

+756
-1
lines changed

11 files changed

+756
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
./webapp/node_modules/*
22
./webapp/package-lock.json
33
webapp/node_modules/
4-
webapp/package-lock.json
4+
webapp/package-lock.json
5+
iac/aws/terraform/creating-custom-vpc/.terraform/

iac/aws/terraform/creating-custom-vpc/.terraform.lock.hcl

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
variable aws_region {
2+
default = "us-east-1"
3+
description = "AWS region where the resources will be provisioned"
4+
}
5+
6+
# Configure the AWS Provider
7+
terraform {
8+
required_providers {
9+
aws = {
10+
source = "hashicorp/aws"
11+
version = "~> 5.0"
12+
}
13+
# helm = {
14+
# source = "hashicorp/aws"
15+
# version = "~> 2.6"
16+
# }
17+
}
18+
}
19+
20+
# Configure region and profile
21+
provider "aws" {
22+
region = var.aws_region
23+
profile = "myaws"
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resource "aws_vpc" "mycustomvpc" {
2+
cidr_block = "10.0.0.0/16"
3+
enable_dns_support = true
4+
enable_dns_hostnames = true
5+
6+
tags = {
7+
"owner" = "vinod"
8+
"Name" = "my custom VPC"
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource "aws_internet_gateway" "igw" {
2+
vpc_id = aws_vpc.mycustomvpc.id
3+
tags = {
4+
"owner" = "vinod"
5+
"Name" = "IGW"
6+
}
7+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
resource "aws_subnet" "private-us-east-1a" {
2+
vpc_id = aws_vpc.mycustomvpc.id
3+
cidr_block = "10.0.1.0/24"
4+
availability_zone = "us-east-1a"
5+
6+
tags = {
7+
"subnet" = "private-us-east-1a"
8+
"Name" = "Private Subnet"
9+
}
10+
}
11+
12+
resource "aws_subnet" "private-us-east-1b" {
13+
vpc_id = aws_vpc.mycustomvpc.id
14+
cidr_block = "10.0.2.0/24"
15+
availability_zone = "us-east-1b"
16+
17+
tags = {
18+
"subnet" = "private-us-east-1b"
19+
"Name" = "Private Subnet"
20+
}
21+
}
22+
23+
resource "aws_subnet" "public-us-east-1a" {
24+
vpc_id = aws_vpc.mycustomvpc.id
25+
cidr_block = "10.0.3.0/24"
26+
availability_zone = "us-east-1a"
27+
map_public_ip_on_launch = true
28+
29+
tags = {
30+
"subnet" = "public-us-east-1a"
31+
"Name" = "Public Subnet"
32+
}
33+
}
34+
35+
resource "aws_subnet" "public-us-east-1b" {
36+
vpc_id = aws_vpc.mycustomvpc.id
37+
cidr_block = "10.0.4.0/24"
38+
availability_zone = "us-east-1b"
39+
map_public_ip_on_launch = true
40+
41+
tags = {
42+
"subnet" = "public-us-east-1b"
43+
"Name" = "Public Subnet"
44+
}
45+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
resource "aws_eip" "nat" {
3+
vpc = true
4+
5+
tags = {
6+
"Name" = "EIP"
7+
"Owner" = "Vinod"
8+
}
9+
10+
}
11+
12+
resource "aws_nat_gateway" "nat" {
13+
allocation_id = aws_eip.nat.id
14+
subnet_id = aws_subnet.public-us-east-1a.id
15+
16+
tags = {
17+
"Name" = "NAT Gateway"
18+
"Owner" = "Vinod"
19+
}
20+
21+
# To ensure proper ordering, it is recommended to add an explicit dependency
22+
# on the Internet Gateway for the VPC.
23+
depends_on = [aws_internet_gateway.igw]
24+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
resource "aws_route_table" "privateroute" {
2+
vpc_id = aws_vpc.mycustomvpc.id
3+
4+
route {
5+
cidr_block = "0.0.0.0/0"
6+
nat_gateway_id = aws_nat_gateway.nat.id
7+
}
8+
9+
tags = {
10+
Name = "private"
11+
}
12+
}
13+
14+
resource "aws_route_table" "publicroute" {
15+
vpc_id = aws_vpc.mycustomvpc.id
16+
17+
route {
18+
cidr_block = "0.0.0.0/0"
19+
gateway_id = aws_internet_gateway.igw.id
20+
}
21+
22+
tags = {
23+
Name = "public"
24+
}
25+
}
26+
27+
resource "aws_route_table_association" "privateassociation_a" {
28+
subnet_id = aws_subnet.private-us-east-1a.id
29+
route_table_id = aws_route_table.privateroute.id
30+
}
31+
resource "aws_route_table_association" "privateassociation_b" {
32+
subnet_id = aws_subnet.private-us-east-1b.id
33+
route_table_id = aws_route_table.privateroute.id
34+
}
35+
resource "aws_route_table_association" "publicassociation_a" {
36+
subnet_id = aws_subnet.public-us-east-1a.id
37+
route_table_id = aws_route_table.publicroute.id
38+
}
39+
resource "aws_route_table_association" "publicassociation_b" {
40+
subnet_id = aws_subnet.public-us-east-1b.id
41+
route_table_id = aws_route_table.publicroute.id
42+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Description
2+
Creating a custom VPC using OpenTofu.
3+
4+
## Architecture
5+
![Custom AWS VPC](https://drive.google.com/file/d/1-1enJhmxFLkUp2jaqOkKGFqueP6cF68W/view?usp=sharing)
6+

0 commit comments

Comments
 (0)