Skip to content

Commit

Permalink
subnet upscale
Browse files Browse the repository at this point in the history
  • Loading branch information
LamSut committed Nov 28, 2024
1 parent 51b46ed commit 9e5e6d0
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 30 deletions.
8 changes: 4 additions & 4 deletions ec2/main.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
resource "aws_instance" "amazon" {
count = 3
count = 1

ami = var.ami_free_amazon
instance_type = var.instance_type_free

key_name = var.key_name

subnet_id = var.subnet
subnet_id = var.subnet1
security_groups = [var.security_group]

tags = {
Expand All @@ -22,7 +22,7 @@ resource "aws_instance" "ubuntu" {

key_name = var.key_name

subnet_id = var.subnet
subnet_id = var.subnet2
security_groups = [var.security_group]

tags = {
Expand All @@ -38,7 +38,7 @@ resource "aws_instance" "windows" {

key_name = var.key_name

subnet_id = var.subnet
subnet_id = var.subnet1
security_groups = [var.security_group]

tags = {
Expand Down
4 changes: 3 additions & 1 deletion ec2/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ variable "key_name" {

variable "security_group" {}

variable "subnet" {}
variable "subnet1" {}

variable "subnet2" {}

variable "ami_free_amazon" {
type = string
Expand Down
3 changes: 2 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module "vpc" {

module "ec2" {
source = "./ec2"
subnet = module.vpc.public_subnet
subnet1 = module.vpc.public_subnet1
subnet2 = module.vpc.public_subnet2
security_group = module.vpc.security_group
}
8 changes: 4 additions & 4 deletions tests/freeEC2.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ run "ubuntu_tests" {
condition = alltrue([
for ami in module.ec2.ami_ubuntu : ami == var.ami_ubuntu_ubuntu4_04
])
error_message = "Amazon Linux Servers: Invalid or not a free AMI type!"
error_message = "Ubuntu Servers: Invalid or not a free AMI type!"
}

assert {
condition = alltrue([
for instance_type in module.ec2.instance_type_ubuntu : instance_type == var.instance_type
])
error_message = "Amazon Linux Servers: Invalid or not a free instance type!"
error_message = "Ubuntu Servers: Invalid or not a free instance type!"
}
}

Expand All @@ -51,13 +51,13 @@ run "windows_tests" {
condition = alltrue([
for ami in module.ec2.ami_windows : ami == var.ami_ms_windows_22
])
error_message = "Amazon Linux Servers: Invalid or not a free AMI type!"
error_message = "Windows Servers: Invalid or not a free AMI type!"
}

assert {
condition = alltrue([
for instance_type in module.ec2.instance_type_windows : instance_type == var.instance_type
])
error_message = "Amazon Linux Servers: Invalid or not a free instance type!"
error_message = "Window Servers: Invalid or not a free instance type!"
}
}
18 changes: 16 additions & 2 deletions tests/vpc.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ run "vpc_tests" {
}
}

run "subnet_tests" {
run "subnet1_tests" {
command = plan

assert {
Expand All @@ -21,7 +21,21 @@ run "subnet_tests" {
}

assert {
condition = module.vpc.vpc_sm <= module.vpc.subnet_sm
condition = module.vpc.vpc_sm <= module.vpc.subnet1_sm
error_message = "Subnet mask of the Subnet must be equal or greater than VPC!"
}
}

run "subnet2_tests" {
command = plan

assert {
condition = module.vpc.map_public_ip_on_launch == true
error_message = "EC2 should be able to receive a public IP address!"
}

assert {
condition = module.vpc.vpc_sm <= module.vpc.subnet2_sm
error_message = "Subnet mask of the Subnet must be equal or greater than VPC!"
}
}
Expand Down
56 changes: 44 additions & 12 deletions vpc/main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#1 virtual private cloud, 1 subnet, 1 security group
//vpc
resource "aws_vpc" "my_vpc" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
Expand All @@ -9,26 +9,57 @@ resource "aws_vpc" "my_vpc" {
}
}

resource "aws_subnet" "public_subnet" {
resource "aws_internet_gateway" "my_vpc_igw" {
vpc_id = aws_vpc.my_vpc.id

tags = {
Name = "vpc_igw"
}
}

//subnet 1
resource "aws_subnet" "public_subnet1" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = var.subnet_cidr
cidr_block = var.subnet1_cidr
map_public_ip_on_launch = true
availability_zone = "us-east-1a"

tags = {
"Name" = "public_subnet"
"Name" = "public_subnet1"
}
}

resource "aws_internet_gateway" "my_vpc_igw" {
resource "aws_route_table" "public_subnet_route_table1" {
vpc_id = aws_vpc.my_vpc.id

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

tags = {
Name = "vpc_igw"
Name = "public_subnet_route_table1"
}
}

resource "aws_route_table_association" "public_subnet_route_table1" {
subnet_id = aws_subnet.public_subnet1.id
route_table_id = aws_route_table.public_subnet_route_table1.id
}

//subnet2
resource "aws_subnet" "public_subnet2" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = var.subnet2_cidr
map_public_ip_on_launch = true
availability_zone = "us-east-1a"

tags = {
"Name" = "public_subnet2"
}
}

resource "aws_route_table" "public_subnet_route_table" {
resource "aws_route_table" "public_subnet_route_table2" {
vpc_id = aws_vpc.my_vpc.id

route {
Expand All @@ -37,22 +68,23 @@ resource "aws_route_table" "public_subnet_route_table" {
}

tags = {
Name = "public_subnet_route_table"
Name = "public_subnet_route_table2"
}
}

resource "aws_route_table_association" "public_subnet_route_table" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public_subnet_route_table.id
resource "aws_route_table_association" "public_subnet_route_table2" {
subnet_id = aws_subnet.public_subnet2.id
route_table_id = aws_route_table.public_subnet_route_table2.id
}

//security group
resource "aws_security_group" "security_group" {
vpc_id = aws_vpc.my_vpc.id
name = "my_security_group"
description = "Public Security Group"

ingress {
from_port = 22
from_port = 22 //for SSH
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
Expand Down
18 changes: 13 additions & 5 deletions vpc/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//for using vpc

output "public_subnet" {
value = aws_subnet.public_subnet.id
output "public_subnet1" {
value = aws_subnet.public_subnet1.id
}

output "public_subnet2" {
value = aws_subnet.public_subnet2.id
}

output "security_group" {
Expand All @@ -21,13 +25,17 @@ output "enable_dns_support" {
// for test subnet

output "map_public_ip_on_launch" {
value = aws_subnet.public_subnet.map_public_ip_on_launch
value = aws_subnet.public_subnet1.map_public_ip_on_launch
}

output "vpc_sm" {
value = tonumber(split("/", aws_vpc.my_vpc.cidr_block)[1])
}

output "subnet_sm" {
value = tonumber(split("/", aws_subnet.public_subnet.cidr_block)[1])
output "subnet1_sm" {
value = tonumber(split("/", aws_subnet.public_subnet1.cidr_block)[1])
}

output "subnet2_sm" {
value = tonumber(split("/", aws_subnet.public_subnet2.cidr_block)[1])
}
7 changes: 6 additions & 1 deletion vpc/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ variable "vpc_cidr" {
default = "10.0.0.0/16"
}

variable "subnet_cidr" {
variable "subnet1_cidr" {
type = string
default = "10.0.1.0/24"
}

variable "subnet2_cidr" {
type = string
default = "10.0.2.0/24"
}

0 comments on commit 9e5e6d0

Please sign in to comment.