Skip to content

Commit 9ebbc72

Browse files
authored
Merge pull request #22 from Aswin-Vijayan/TEC-56
[TEC-56][Changes]-made chnages in security group
2 parents 4f55c0c + 0c4684f commit 9ebbc72

File tree

4 files changed

+86
-19
lines changed

4 files changed

+86
-19
lines changed

environments/dev/alb-asg/main.tf

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module "alb-sg" {
1717
environment = var.environment
1818
owner = var.owner
1919
cost_center = var.cost_center
20-
application ="${var.application}-alb"
20+
application = var.application
2121
vpc_id = var.vpc_id
2222
ingress_from_port = var.alb_ingress_from_port
2323
ingress_to_port = var.alb_ingress_to_port
@@ -57,15 +57,9 @@ module "alb" {
5757
security_group_ids = module.alb-sg.security_group_ids
5858
}
5959

60-
module "instance-sg" {
61-
source = "../../../modules/security-group"
62-
region = var.region
63-
tags = var.tags
64-
environment = var.environment
65-
owner = var.owner
66-
cost_center = var.cost_center
67-
application = var.application
68-
vpc_id = var.vpc_id
60+
module "asg" {
61+
source = "../../../modules/asg"
62+
ami_id = var.ami_id
6963
ingress_from_port = var.ingress_from_port
7064
ingress_to_port = var.ingress_to_port
7165
ingress_protocol = var.ingress_protocol
@@ -74,11 +68,6 @@ module "instance-sg" {
7468
egress_to_port = var.egress_to_port
7569
egress_protocol = var.egress_protocol
7670
egress_cidr_block = var.egress_cidr_block
77-
}
78-
79-
module "asg" {
80-
source = "../../../modules/asg"
81-
ami_id = var.ami_id
8271
instance_type = var.instance_type
8372
key_name = var.key_name
8473
vpc_id = var.vpc_id
@@ -95,7 +84,7 @@ module "asg" {
9584
application = var.application
9685
alb_target_group_arn = module.alb.alb_target_group_arn
9786
iam_role = module.iam-policy.iam_role
98-
security_group_ids = module.instance-sg.security_group_ids
87+
security_group_ids = module.alb-sg.security_group_ids
9988
tags = {
10089
Owner = "${var.owner}"
10190
Environment = "${var.environment}"

modules/asg/main.tf

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,44 @@ 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+
846
resource "aws_iam_instance_profile" "instance_profile" {
947
name = "${var.environment}-${var.application}-instance_profile"
1048

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

2462
network_interfaces {
2563
associate_public_ip_address = var.public_access
26-
security_groups = var.security_group_ids
64+
security_groups = [aws_security_group.instance_sg.id]
2765
}
2866

2967
user_data = base64encode(var.user_data)

modules/asg/variables.tf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,43 @@ variable "alb_target_group_arn" {
9393
description = "load balancer target group arn"
9494
type = string
9595
}
96+
97+
variable "ingress_cidr_block" {
98+
type = list(string)
99+
description = "List of CIDR blocks for ingress rules of the EC2 security group."
100+
}
101+
102+
variable "ingress_from_port" {
103+
description = "List of starting ports for ingress rules of the EC2 security group."
104+
type = list(number)
105+
}
106+
107+
variable "ingress_to_port" {
108+
description = "List of ending ports for ingress rules of the EC2 security group."
109+
type = list(number)
110+
}
111+
112+
variable "ingress_protocol" {
113+
description = "List of protocols for ingress rules of the EC2 security group."
114+
type = list
115+
}
116+
117+
variable "egress_cidr_block" {
118+
type = list(string)
119+
description = "List of CIDR blocks for egress rules of the EC2 security group."
120+
}
121+
122+
variable "egress_from_port" {
123+
description = "List of starting ports for egress rules of the EC2 security group."
124+
type = list(number)
125+
}
126+
127+
variable "egress_to_port" {
128+
description = "List of ending ports for egress rules of the EC2 security group."
129+
type = list(number)
130+
}
131+
132+
variable "egress_protocol" {
133+
description = "List of protocols for egress rules of the EC2 security group."
134+
type = list
135+
}

vars/dev/alb-asg.tfvars

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ loadbalancer_type = "application"
44
alb_subnets = ["subnet-058a7514ba8adbb07", "subnet-0dbcd1ac168414927", "subnet-032f5077729435858"]
55

66
#alb-sg
7-
alb_ingress_from_port = [80, 8080]
8-
alb_ingress_to_port = [80, 8080]
7+
alb_ingress_from_port = [80, 22]
8+
alb_ingress_to_port = [80, 22]
99
alb_ingress_protocol = ["tcp", "tcp"]
1010
alb_ingress_cidr_block = ["0.0.0.0/0"]
1111
alb_egress_from_port = [0]

0 commit comments

Comments
 (0)