Skip to content

Commit 22314f0

Browse files
Added 3 tier architecture
1 parent 76a6382 commit 22314f0

File tree

11 files changed

+188
-76
lines changed

11 files changed

+188
-76
lines changed

main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module "nat_gateway" {
4646
cluster_prefix = var.cluster_prefix
4747
cluster_environment = var.cluster_environment
4848
public_subnet_ids = module.public_subnet.public_subnet_ids
49+
cluster_architecture = var.cluster_architecture
4950
}
5051

5152
# AWS VPC Subnets Module - Private Subnet
@@ -67,4 +68,5 @@ module "security_group" {
6768
vpc_id = aws_vpc.vpc.id
6869
cluster_prefix = var.cluster_prefix
6970
cluster_environment = var.cluster_environment
71+
cluster_architecture = var.cluster_architecture
7072
}

modules/nat-gateways/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ data "aws_availability_zones" "available_zones" {}
44
# AWS Elastic IPs
55
resource "aws_eip" "eip" {
66
vpc = true
7-
count = length(data.aws_availability_zones.available_zones.names)
7+
count = var.cluster_architecture == "2-tier" || var.cluster_architecture == "3-tier" ? length(data.aws_availability_zones.available_zones.names) : 0
88

99
tags = {
1010
Name = "${var.cluster_prefix}-${count.index + 1}"
@@ -16,7 +16,7 @@ resource "aws_eip" "eip" {
1616
resource "aws_nat_gateway" "nat_gateway" {
1717
allocation_id = element(aws_eip.eip.*.id, count.index)
1818
subnet_id = element(var.public_subnet_ids, count.index)
19-
count = length(data.aws_availability_zones.available_zones.names)
19+
count = var.cluster_architecture == "2-tier" || var.cluster_architecture == "3-tier" ? length(data.aws_availability_zones.available_zones.names) : 0
2020

2121
tags = {
2222
Name = "${var.cluster_prefix}-${count.index + 1}"

modules/nat-gateways/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ variable "cluster_environment" {
88
type = string
99
}
1010

11+
variable "cluster_architecture" {
12+
description = "To apply generic cluster_environment to AWS VPC Resources"
13+
type = string
14+
}
15+
1116
variable "public_subnet_ids" {
1217
description = "list of public subnets in order of availability zones so that NAT Gateway's can be created in those respective subnets"
1318
type = list(any)

modules/security-groups/main.tf

Lines changed: 129 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,156 @@
11
# AWS Public Security Group
2-
module "public_security_group" {
3-
source = "./resources"
4-
vpc_id = var.vpc_id
5-
cluster_prefix = var.cluster_prefix
6-
cluster_environment = var.cluster_environment
7-
sg_type = "public"
8-
sg_description = "Allow connections from internet"
2+
# module "public_security_group" {
3+
# source = "./resources"
4+
# count = var.cluster_architecture == "1-tier" || var.cluster_architecture == "2-tier" || var.cluster_architecture == "3-tier" ? 1 : 0
5+
# vpc_id = var.vpc_id
6+
# cluster_prefix = var.cluster_prefix
7+
# cluster_environment = var.cluster_environment
8+
# sg_type = "public"
9+
# sg_description = "Allow connections from internet"
10+
# cluster_architecture = var.cluster_architecture
11+
# }
12+
13+
resource "aws_security_group" "public_security_group" {
14+
count = var.cluster_architecture == "1-tier" || var.cluster_architecture == "2-tier" || var.cluster_architecture == "3-tier" ? 1 : 0
15+
name = "${var.cluster_prefix}-public"
16+
description = "Allow connections from internet"
17+
vpc_id = var.vpc_id
18+
revoke_rules_on_delete = true
19+
20+
egress {
21+
description = "Allow all outbound"
22+
from_port = 0
23+
to_port = 65535
24+
protocol = "-1"
25+
cidr_blocks = ["0.0.0.0/0"]
26+
}
27+
28+
ingress {
29+
description = "Allow http inbound public"
30+
from_port = 80
31+
to_port = 80
32+
protocol = "tcp"
33+
cidr_blocks = ["0.0.0.0/0"]
34+
}
35+
36+
ingress {
37+
description = "Allow https inbound public"
38+
from_port = 443
39+
to_port = 443
40+
protocol = "tcp"
41+
cidr_blocks = ["0.0.0.0/0"]
42+
}
43+
44+
tags = {
45+
Name = "${var.cluster_prefix}-public"
46+
Environment = var.cluster_environment
47+
Type = "public"
48+
}
949
}
1050

1151
# AWS Public Security Group Rules
12-
resource "aws_security_group_rule" "allow_http_inbound_public" {
13-
type = "ingress"
14-
from_port = 80
15-
to_port = 80
16-
protocol = "tcp"
17-
cidr_blocks = ["0.0.0.0/0"]
18-
security_group_id = module.public_security_group.security_group_id
19-
}
52+
# resource "aws_security_group_rule" "allow_http_inbound_public" {
53+
# type = "ingress"
54+
# from_port = 80
55+
# to_port = 80
56+
# protocol = "tcp"
57+
# cidr_blocks = ["0.0.0.0/0"]
58+
# security_group_id = aws_security_group.public_security_group.id
59+
# }
2060

21-
resource "aws_security_group_rule" "allow_https_inbound_public" {
22-
type = "ingress"
23-
from_port = 443
24-
to_port = 443
25-
protocol = "tcp"
26-
cidr_blocks = ["0.0.0.0/0"]
27-
security_group_id = module.public_security_group.security_group_id
28-
}
61+
# resource "aws_security_group_rule" "allow_https_inbound_public" {
62+
# type = "ingress"
63+
# from_port = 443
64+
# to_port = 443
65+
# protocol = "tcp"
66+
# cidr_blocks = ["0.0.0.0/0"]
67+
# security_group_id = aws_security_group.public_security_group.id
68+
# }
2969

3070
# AWS Private Security Group
31-
module "private_security_group" {
32-
source = "./resources"
33-
vpc_id = var.vpc_id
34-
cluster_prefix = var.cluster_prefix
35-
cluster_environment = var.cluster_environment
36-
sg_type = "private"
37-
sg_description = "The private security group to allows inbound traffic from public group"
71+
# module "private_security_group" {
72+
# source = "./resources"
73+
# count = var.cluster_architecture == "2-tier" || var.cluster_architecture == "3-tier" ? 1 : 0
74+
# vpc_id = var.vpc_id
75+
# cluster_prefix = var.cluster_prefix
76+
# cluster_environment = var.cluster_environment
77+
# sg_type = "private"
78+
# sg_description = "The private security group to allows inbound traffic from public group"
79+
# cluster_architecture = var.cluster_architecture
80+
# }
81+
82+
resource "aws_security_group" "private_security_group" {
83+
count = var.cluster_architecture == "2-tier" || var.cluster_architecture == "3-tier" ? 1 : 0
84+
name = "${var.cluster_prefix}-private"
85+
description = "The private security group to allows inbound traffic from public group"
86+
vpc_id = var.vpc_id
87+
revoke_rules_on_delete = true
88+
89+
egress {
90+
from_port = 0
91+
to_port = 65535
92+
protocol = "-1"
93+
cidr_blocks = ["0.0.0.0/0"]
94+
}
95+
96+
tags = {
97+
Name = "${var.cluster_prefix}-private"
98+
Environment = var.cluster_environment
99+
Type = "private"
100+
}
38101
}
39102

40103
# AWS Private Security Group Rules
41104
resource "aws_security_group_rule" "allow_inbound_private" {
105+
count = var.cluster_architecture == "2-tier" || var.cluster_architecture == "3-tier" ? 1 : 0
42106
type = "ingress"
43107
from_port = 0
44108
to_port = 65535
45109
protocol = "-1"
46-
source_security_group_id = module.public_security_group.security_group_id
47-
security_group_id = module.private_security_group.security_group_id
110+
source_security_group_id = aws_security_group.public_security_group[0].id
111+
security_group_id = aws_security_group.private_security_group[0].id
48112
}
49113

50114
# AWS Storage Security Group
51-
module "storage_security_group" {
52-
source = "./resources"
53-
vpc_id = var.vpc_id
54-
cluster_prefix = var.cluster_prefix
55-
cluster_environment = var.cluster_environment
56-
sg_type = "storage"
57-
sg_description = "The storage security group to allows inbound traffic from private group"
115+
# module "storage_security_group" {
116+
# source = "./resources"
117+
# count = var.cluster_architecture == "3-tier" ? 1 : 0
118+
# vpc_id = var.vpc_id
119+
# cluster_prefix = var.cluster_prefix
120+
# cluster_environment = var.cluster_environment
121+
# sg_type = "storage"
122+
# sg_description = "The storage security group to allows inbound traffic from private group"
123+
# cluster_architecture = var.cluster_architecture
124+
# }
125+
126+
resource "aws_security_group" "storage_security_group" {
127+
count = var.cluster_architecture == "3-tier" ? 1 : 0
128+
name = "${var.cluster_prefix}-storage"
129+
description = "The storage security group to allows inbound traffic from private group"
130+
vpc_id = var.vpc_id
131+
revoke_rules_on_delete = true
132+
133+
egress {
134+
from_port = 0
135+
to_port = 65535
136+
protocol = "-1"
137+
cidr_blocks = ["0.0.0.0/0"]
138+
}
139+
140+
tags = {
141+
Name = "${var.cluster_prefix}-storage"
142+
Environment = var.cluster_environment
143+
Type = "storage"
144+
}
58145
}
59146

60147
# AWS Storage Security Group Rules
61148
resource "aws_security_group_rule" "allow_inbound_storage" {
149+
count = var.cluster_architecture == "3-tier" ? 1 : 0
62150
type = "ingress"
63151
from_port = 0
64152
to_port = 65535
65153
protocol = "-1"
66-
source_security_group_id = module.private_security_group.security_group_id
67-
security_group_id = module.storage_security_group.security_group_id
154+
source_security_group_id = aws_security_group.private_security_group[0].id
155+
security_group_id = aws_security_group.storage_security_group[0].id
68156
}

modules/security-groups/output.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
output "public_security_group_id" {
2-
value = module.public_security_group.security_group_id
1+
output "public_security_group_ids" {
2+
value = aws_security_group.public_security_group.*.id
33
}
44

5-
output "private_security_group_id" {
6-
value = module.private_security_group.security_group_id
5+
output "private_security_group_ids" {
6+
value = aws_security_group.private_security_group.*.id
77
}
88

9-
output "storage_security_group_id" {
10-
value = module.storage_security_group.security_group_id
9+
output "storage_security_group_ids" {
10+
value = aws_security_group.storage_security_group.*.id
1111
}
Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
# AWS Security Group
2-
resource "aws_security_group" "security_group" {
3-
name = "${var.cluster_prefix}-${var.sg_type}"
4-
description = var.sg_description
5-
vpc_id = var.vpc_id
6-
revoke_rules_on_delete = true
2+
# resource "aws_security_group" "security_group" {
3+
# name = "${var.cluster_prefix}-${var.sg_type}"
4+
# description = var.sg_description
5+
# vpc_id = var.vpc_id
6+
# revoke_rules_on_delete = true
77

8-
tags = {
9-
Name = "${var.cluster_prefix}-${var.sg_type}"
10-
Environment = var.cluster_environment
11-
Type = var.sg_type
12-
}
13-
}
8+
# egress {
9+
# from_port = 0
10+
# to_port = 65535
11+
# protocol = "-1"
12+
# cidr_blocks = ["0.0.0.0/0"]
13+
# }
14+
15+
# tags = {
16+
# Name = "${var.cluster_prefix}-${var.sg_type}"
17+
# Environment = var.cluster_environment
18+
# Type = var.sg_type
19+
# }
20+
# }
1421

1522
# AWS Outbound Security Group Rule
16-
resource "aws_security_group_rule" "security_group_rule" {
17-
type = "egress"
18-
from_port = 0
19-
to_port = 65535
20-
protocol = "-1"
21-
cidr_blocks = ["0.0.0.0/0"]
22-
security_group_id = aws_security_group.security_group.id
23-
}
23+
# resource "aws_security_group_rule" "security_group_rule" {
24+
# type = "egress"
25+
# from_port = 0
26+
# to_port = 65535
27+
# protocol = "-1"
28+
# cidr_blocks = ["0.0.0.0/0"]
29+
# security_group_id = aws_security_group.security_group.id
30+
# }
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
output "security_group_id" {
2-
value = aws_security_group.security_group.id
3-
}
1+
# output "security_group_id" {
2+
# value = aws_security_group.security_group.id
3+
# }

modules/security-groups/resources/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ variable "cluster_environment" {
1313
type = string
1414
}
1515

16+
variable "cluster_architecture" {
17+
description = "To apply generic cluster_environment to AWS VPC Resources"
18+
type = string
19+
}
20+
1621
variable "sg_type" {
1722
description = "Security Group type Eg: public, private and storage"
1823
type = string

modules/security-groups/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ variable "cluster_environment" {
1313
type = string
1414
}
1515

16+
variable "cluster_architecture" {
17+
description = "To apply generic cluster_environment to AWS VPC Resources"
18+
type = string
19+
}
20+

modules/subnets/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ data "aws_availability_zones" "available_zones" {}
55
module "aws_public_subnet" {
66
source = "./resources"
77
create = contains(var.subnet_type, "public") ? 1 : 0
8-
tier = var.cluster_architecture == "1-tier" ? 1 : 0
8+
tier = var.cluster_architecture == "1-tier" || var.cluster_architecture == "2-tier" || var.cluster_architecture == "3-tier" ? 1 : 0
99
cluster_prefix = var.cluster_prefix
1010
cluster_environment = var.cluster_environment
1111
vpc_id = var.vpc_id
@@ -19,7 +19,7 @@ module "aws_public_subnet" {
1919
module "aws_private_subnet" {
2020
source = "./resources"
2121
create = contains(var.subnet_type, "private") ? 1 : 0
22-
tier = var.cluster_architecture == "2-tier" ? 1 : 0
22+
tier = var.cluster_architecture == "2-tier" || var.cluster_architecture == "3-tier" ? 1 : 0
2323
cluster_prefix = var.cluster_prefix
2424
cluster_environment = var.cluster_environment
2525
vpc_id = var.vpc_id
@@ -55,8 +55,8 @@ resource "aws_route" "public_route" {
5555

5656
# AWS Route Tables - Private Route
5757
resource "aws_route" "private_route" {
58-
count = var.cluster_architecture == "2-tier" || var.cluster_architecture == "3-tier" && contains(var.subnet_type, "private") ? length(data.aws_availability_zones.available_zones.names) : 0
58+
count = var.cluster_architecture == "2-tier" || var.cluster_architecture == "3-tier" && contains(var.subnet_type, "private") ? length(data.aws_availability_zones.available_zones.names): 0
5959
route_table_id = module.aws_private_subnet.route_table_ids[count.index]
6060
destination_cidr_block = "0.0.0.0/0"
61-
nat_gateway_id = var.aws_nat_gateway_id[count.index]
61+
nat_gateway_id = var.aws_nat_gateway_id[0]
6262
}

0 commit comments

Comments
 (0)