Skip to content

Commit 8538b2b

Browse files
authored
Merge pull request #29 from techiescamp/develop
Develop
2 parents 7a00001 + 36bc006 commit 8538b2b

File tree

5 files changed

+232
-131
lines changed

5 files changed

+232
-131
lines changed

environments/dev/alb-asg/main.tf

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,36 @@ module "alb-sg" {
1414
source = "../../../modules/security-group"
1515
region = var.region
1616
tags = var.tags
17+
name = "${var.environment}-${var.application}"
1718
environment = var.environment
1819
owner = var.owner
1920
cost_center = var.cost_center
20-
application = var.application
21+
application = "${var.application}-alb"
2122
vpc_id = var.vpc_id
22-
ingress_from_port = var.alb_ingress_from_port
23-
ingress_to_port = var.alb_ingress_to_port
24-
ingress_protocol = var.alb_ingress_protocol
23+
24+
ingress_cidr_from_port = var.alb_ingress_cidr_from_port
25+
ingress_cidr_to_port = var.alb_ingress_cidr_to_port
26+
ingress_cidr_protocol = var.ingress_cidr_protocol
2527
ingress_cidr_block = var.alb_ingress_cidr_block
26-
egress_from_port = var.alb_egress_from_port
27-
egress_to_port = var.alb_egress_to_port
28-
egress_protocol = var.alb_egress_protocol
28+
create_ingress_cidr = var.alb_create_ingress_cidr
29+
30+
ingress_sg_from_port = var.alb_ingress_sg_from_port
31+
ingress_sg_to_port = var.alb_ingress_sg_to_port
32+
ingress_sg_protocol = var.alb_ingress_sg_protocol
33+
ingress_security_group_ids = var.ingress_security_group_ids
34+
create_ingress_sg = var.alb_create_ingress_sg
35+
36+
egress_cidr_from_port = var.alb_egress_cidr_from_port
37+
egress_cidr_to_port = var.alb_egress_cidr_to_port
38+
egress_cidr_protocol = var.alb_egress_cidr_protocol
2939
egress_cidr_block = var.alb_egress_cidr_block
40+
create_egress_cidr = var.alb_create_egress_cidr
41+
42+
egress_sg_from_port = var.alb_egress_sg_from_port
43+
egress_sg_to_port = var.alb_egress_sg_to_port
44+
egress_sg_protocol = var.alb_egress_sg_protocol
45+
egress_security_group_ids = var.egress_security_group_ids
46+
create_egress_sg = var.alb_create_egress_sg
3047
}
3148

3249
module "alb" {
@@ -57,17 +74,45 @@ module "alb" {
5774
security_group_ids = module.alb-sg.security_group_ids
5875
}
5976

77+
module "instance-sg" {
78+
source = "../../../modules/security-group"
79+
region = var.region
80+
tags = var.tags
81+
name = "${var.environment}-${var.application}"
82+
environment = var.environment
83+
owner = var.owner
84+
cost_center = var.cost_center
85+
application = var.application
86+
vpc_id = var.vpc_id
87+
88+
ingress_cidr_from_port = var.ingress_cidr_from_port
89+
ingress_cidr_to_port = var.ingress_cidr_to_port
90+
ingress_cidr_protocol = var.ingress_cidr_protocol
91+
ingress_cidr_block = var.ingress_cidr_block
92+
create_ingress_cidr = var.create_ingress_cidr
93+
94+
ingress_sg_from_port = var.ingress_sg_from_port
95+
ingress_sg_to_port = var.ingress_sg_to_port
96+
ingress_sg_protocol = var.ingress_sg_protocol
97+
ingress_security_group_ids = module.alb-sg.security_group_ids
98+
create_ingress_sg = var.create_ingress_sg
99+
100+
egress_cidr_from_port = var.egress_cidr_from_port
101+
egress_cidr_to_port = var.egress_cidr_to_port
102+
egress_cidr_protocol = var.egress_cidr_protocol
103+
egress_cidr_block = var.egress_cidr_block
104+
create_egress_cidr = var.create_egress_cidr
105+
106+
egress_sg_from_port = var.egress_sg_from_port
107+
egress_sg_to_port = var.egress_sg_to_port
108+
egress_sg_protocol = var.egress_sg_protocol
109+
egress_security_group_ids = module.alb-sg.security_group_ids
110+
create_egress_sg = var.create_egress_sg
111+
}
112+
60113
module "asg" {
61114
source = "../../../modules/asg"
62115
ami_id = var.ami_id
63-
ingress_from_port = var.ingress_from_port
64-
ingress_to_port = var.ingress_to_port
65-
ingress_protocol = var.ingress_protocol
66-
ingress_cidr_block = var.ingress_cidr_block
67-
egress_from_port = var.egress_from_port
68-
egress_to_port = var.egress_to_port
69-
egress_protocol = var.egress_protocol
70-
egress_cidr_block = var.egress_cidr_block
71116
instance_type = var.instance_type
72117
key_name = var.key_name
73118
vpc_id = var.vpc_id
@@ -84,7 +129,7 @@ module "asg" {
84129
application = var.application
85130
alb_target_group_arn = module.alb.alb_target_group_arn
86131
iam_role = module.iam-policy.iam_role
87-
security_group_ids = module.alb-sg.security_group_ids
132+
security_group_ids = module.instance-sg.security_group_ids
88133
tags = {
89134
Owner = "${var.owner}"
90135
Environment = "${var.environment}"

environments/dev/alb-asg/variables.tf

Lines changed: 130 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,6 @@ variable "asg_subnets" {
120120
type = list(string)
121121
}
122122

123-
variable "security_group_ids" {
124-
type = list(string)
125-
description = "Security group id of the ec2 instance"
126-
}
127-
128123
variable "public_access" {
129124
description = "Whether the instance is public or not"
130125
type = bool
@@ -175,83 +170,195 @@ variable "application" {
175170
description = "Name of the application"
176171
}
177172

178-
179173
variable "ingress_cidr_block" {
180174
type = list(string)
181-
description = "CIDR blocks for EC2 security group ingress rules"
175+
description = "CIDR blocks for the security group ingress rules"
182176
}
183177

184-
variable "ingress_from_port" {
178+
variable "ingress_cidr_from_port" {
185179
description = "The starting port for ingress rules"
186180
type = list(number)
187181
}
188182

189-
variable "ingress_to_port" {
183+
variable "ingress_cidr_to_port" {
190184
description = "The ending port for ingress rules"
191185
type = list(number)
192186
}
193187

194-
variable "ingress_protocol" {
188+
variable "ingress_cidr_protocol" {
195189
description = "The protocol for ingress rules"
196190
type = list(any)
197191
}
198192

193+
variable "create_ingress_cidr" {
194+
description = "Whether to create the ingress cidr or not"
195+
type = bool
196+
}
197+
198+
variable "ingress_sg_from_port" {
199+
type = list(number)
200+
description = "List of starting ports for sg ingress rules"
201+
}
202+
203+
variable "ingress_sg_to_port" {
204+
type = list(number)
205+
description = "List of ending ports for sg ingress rules"
206+
}
207+
208+
variable "ingress_sg_protocol" {
209+
type = list(any)
210+
description = "List of protocols for sg ingress rules"
211+
}
212+
213+
variable "ingress_security_group_ids" {
214+
type = list(string)
215+
default = [ "sg-0fe4363da3994c100" ]
216+
description = "List of Security Group ids for sg ingress rules"
217+
}
218+
219+
variable "create_ingress_sg" {
220+
type = bool
221+
description = "Enable or disable Security Groups ingress rules."
222+
}
223+
199224
variable "egress_cidr_block" {
200225
type = list(string)
201-
description = "CIDR blocks for EC2 security group egress rules"
226+
description = "CIDR blocks for group egress rules"
202227
}
203228

204-
variable "egress_from_port" {
229+
variable "egress_cidr_from_port" {
205230
description = "The starting port for egress rules"
206231
type = list(number)
207232
}
208233

209-
variable "egress_to_port" {
234+
variable "egress_cidr_to_port" {
210235
description = "The ending port for egress rules"
211236
type = list(number)
212237
}
213238

214-
variable "egress_protocol" {
239+
variable "egress_cidr_protocol" {
215240
description = "The protocol for egress rules"
216241
type = list(any)
217242
}
218243

244+
variable "create_egress_cidr" {
245+
type = bool
246+
description = "Enable or disable CIDR block egress rules."
247+
}
248+
249+
variable "egress_sg_from_port" {
250+
description = "The starting port for egress rules"
251+
type = list(number)
252+
}
253+
254+
variable "egress_sg_to_port" {
255+
description = "The ending port for egress rules"
256+
type = list(number)
257+
}
258+
259+
variable "egress_sg_protocol" {
260+
description = "The protocol for egress rules"
261+
type = list(any)
262+
}
263+
264+
variable "egress_security_group_ids" {
265+
type = list(string)
266+
default = [ "sg-0fe4363da3994c100" ]
267+
description = "List of Security Group ids for sg egress rules"
268+
}
269+
270+
variable "create_egress_sg" {
271+
type = bool
272+
description = "Enable or disable CIDR block egress rules."
273+
}
274+
275+
219276
variable "alb_ingress_cidr_block" {
220277
type = list(string)
221-
description = "CIDR blocks for EC2 security group ingress rules"
278+
description = "CIDR blocks for the security group ingress rules"
222279
}
223280

224-
variable "alb_ingress_from_port" {
281+
variable "alb_ingress_cidr_from_port" {
225282
description = "The starting port for ingress rules"
226283
type = list(number)
227284
}
228285

229-
variable "alb_ingress_to_port" {
286+
variable "alb_ingress_cidr_to_port" {
230287
description = "The ending port for ingress rules"
231288
type = list(number)
232289
}
233290

234-
variable "alb_ingress_protocol" {
291+
variable "alb_ingress_cidr_protocol" {
235292
description = "The protocol for ingress rules"
236293
type = list(any)
237294
}
238295

296+
variable "alb_create_ingress_cidr" {
297+
description = "Whether to create the ingress cidr or not"
298+
type = bool
299+
}
300+
301+
variable "alb_ingress_sg_from_port" {
302+
type = list(number)
303+
description = "List of starting ports for sg ingress rules"
304+
}
305+
306+
variable "alb_ingress_sg_to_port" {
307+
type = list(number)
308+
description = "List of ending ports for sg ingress rules"
309+
}
310+
311+
variable "alb_ingress_sg_protocol" {
312+
type = list(any)
313+
description = "List of protocols for sg ingress rules"
314+
}
315+
316+
variable "alb_create_ingress_sg" {
317+
type = bool
318+
description = "Enable or disable Security Groups ingress rules."
319+
}
320+
239321
variable "alb_egress_cidr_block" {
240322
type = list(string)
241-
description = "CIDR blocks for EC2 security group egress rules"
323+
description = "CIDR blocks for the security group egress rules"
242324
}
243325

244-
variable "alb_egress_from_port" {
326+
variable "alb_egress_cidr_from_port" {
245327
description = "The starting port for egress rules"
246328
type = list(number)
247329
}
248330

249-
variable "alb_egress_to_port" {
331+
variable "alb_egress_cidr_to_port" {
250332
description = "The ending port for egress rules"
251333
type = list(number)
252334
}
253335

254-
variable "alb_egress_protocol" {
336+
variable "alb_egress_cidr_protocol" {
255337
description = "The protocol for egress rules"
256338
type = list(any)
257-
}
339+
}
340+
341+
variable "alb_create_egress_cidr" {
342+
type = bool
343+
description = "Enable or disable CIDR block egress rules."
344+
}
345+
346+
variable "alb_egress_sg_from_port" {
347+
description = "The starting port for egress rules"
348+
type = list(number)
349+
}
350+
351+
variable "alb_egress_sg_to_port" {
352+
description = "The ending port for egress rules"
353+
type = list(number)
354+
}
355+
356+
variable "alb_egress_sg_protocol" {
357+
description = "The protocol for egress rules"
358+
type = list(any)
359+
}
360+
361+
variable "alb_create_egress_sg" {
362+
type = bool
363+
description = "Enable or disable CIDR block egress rules."
364+
}

modules/asg/main.tf

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,6 @@ locals {
55
)
66
}
77

8-
resource "aws_security_group" "instance_sg" {
9-
name = "${var.environment}-${var.application}-instance-sg"
10-
description = "Security Group for Instance"
11-
vpc_id = var.vpc_id
12-
13-
dynamic "ingress" {
14-
for_each = toset(range(length(var.ingress_from_port)))
15-
content {
16-
from_port = var.ingress_from_port[ingress.key]
17-
to_port = var.ingress_to_port[ingress.key]
18-
protocol = var.ingress_protocol[ingress.key]
19-
security_groups = var.security_group_ids
20-
}
21-
}
22-
23-
dynamic "egress" {
24-
for_each = toset(range(length(var.egress_from_port)))
25-
content {
26-
from_port = var.egress_from_port[egress.key]
27-
to_port = var.egress_to_port[egress.key]
28-
protocol = var.egress_protocol[egress.key]
29-
cidr_blocks = var.egress_cidr_block
30-
}
31-
}
32-
33-
tags = merge(
34-
{
35-
"Name" = "${var.environment}-${var.application}-sg"
36-
"Environment" = var.environment
37-
"Owner" = var.owner
38-
"CostCenter" = var.cost_center
39-
"Application" = var.application
40-
},
41-
var.tags
42-
)
43-
44-
}
45-
468
resource "aws_iam_instance_profile" "instance_profile" {
479
name = "${var.environment}-${var.application}-instance_profile"
4810

@@ -61,7 +23,7 @@ resource "aws_launch_template" "application_lt" {
6123

6224
network_interfaces {
6325
associate_public_ip_address = var.public_access
64-
security_groups = [aws_security_group.instance_sg.id]
26+
security_groups = var.security_group_ids
6527
}
6628

6729
user_data = base64encode(var.user_data)

0 commit comments

Comments
 (0)