Skip to content

Commit e24aa38

Browse files
authored
Merge pull request #3 from arunlalp/gitlab
[TEC-75]
2 parents 940aad1 + a568ce7 commit e24aa38

File tree

7 files changed

+186
-127
lines changed

7 files changed

+186
-127
lines changed

environments/dev/ec2/main.tf

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,26 @@ module "ec2" {
1616
owner = var.owner
1717
cost_center = var.cost_center
1818
application = var.application
19-
security_group_ids = module.ec2-sg.security_group_ids
19+
security_group_ids = module.security-group.security_group_ids
2020
}
2121

22-
module "ec2-sg" {
23-
source = "../../../modules/ec2-sg"
24-
region = var.region
25-
tags = var.tags
26-
name = var.name
27-
environment = var.environment
28-
owner = var.owner
29-
cost_center = var.cost_center
30-
application = var.application
31-
sg_name = var.sg_name
32-
from_port = var.from_port
33-
to_port = var.to_port
34-
protocol = var.protocol
35-
cidr_block = var.cidr_block
22+
module "security-group" {
23+
source = "../../../modules/security-group"
24+
region = var.region
25+
tags = var.tags
26+
name = var.name
27+
environment = var.environment
28+
owner = var.owner
29+
cost_center = var.cost_center
30+
application = var.application
31+
sg_name = var.sg_name
32+
vpc_id = var.vpc_id
33+
ingress_from_port = var.ingress_from_port
34+
ingress_to_port = var.ingress_to_port
35+
ingress_protocol = var.ingress_protocol
36+
ingress_cidr_block = var.ingress_cidr_block
37+
egress_from_port = var.egress_from_port
38+
egress_to_port = var.egress_to_port
39+
egress_protocol = var.egress_protocol
40+
egress_cidr_block = var.egress_cidr_block
3641
}

environments/dev/ec2/variables.tf

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,102 @@
11
variable "region" {
22
type = string
3-
description = "Region of the ec2 instance"
3+
description = "Region of the EC2 instance"
44
}
55

66
variable "instance_name" {
77
type = string
8-
description = "Name of the ec2 instance"
8+
description = "Name of the EC2 instance"
99
}
1010

1111
variable "ami_id" {
1212
type = string
13-
description = "AMI Id of the ec2 instance"
13+
description = "AMI ID of the EC2 instance"
1414
}
1515

1616
variable "instance_type" {
1717
type = string
18-
description = "Instance type of the ec2 instance"
18+
description = "Instance type of the EC2 instance"
1919
}
2020

2121
variable "key_name" {
2222
type = string
23-
description = "Key name of the ec2 instance"
23+
description = "Key name of the EC2 instance"
2424
}
2525

2626
variable "instance_count" {
2727
type = number
28-
description = "Count of the ec2 instances"
28+
description = "Count of the EC2 instances"
2929
}
3030

3131
variable "subnet_ids" {
3232
type = list(string)
33-
description = "Subnet ids of the ec2 instance"
33+
description = "Subnet IDs of the EC2 instance"
3434
}
3535

3636
variable "sg_name" {
3737
type = string
38-
description = "Security group for instance"
38+
description = "Security group name for the instance"
3939
}
4040

41-
variable "cidr_block" {
41+
variable "vpc_id" {
42+
type = string
43+
description = "VPC ID for the security group"
44+
}
45+
46+
variable "ingress_cidr_block" {
4247
type = list(string)
43-
description = "CIDR block for EC2 security group"
48+
description = "CIDR blocks for EC2 security group ingress rules"
4449
}
4550

46-
variable "from_port" {
51+
variable "ingress_from_port" {
4752
description = "The starting port for ingress rules"
4853
type = list(number)
4954
}
5055

51-
variable "to_port" {
56+
variable "ingress_to_port" {
5257
description = "The ending port for ingress rules"
5358
type = list(number)
5459
}
5560

56-
variable "protocol" {
61+
variable "ingress_protocol" {
5762
description = "The protocol for ingress rules"
63+
type = list(any)
64+
}
65+
66+
variable "egress_cidr_block" {
5867
type = list(string)
68+
description = "CIDR blocks for EC2 security group egress rules"
69+
}
70+
71+
variable "egress_from_port" {
72+
description = "The starting port for egress rules"
73+
type = list(number)
74+
}
75+
76+
variable "egress_to_port" {
77+
description = "The ending port for egress rules"
78+
type = list(number)
79+
}
80+
81+
variable "egress_protocol" {
82+
description = "The protocol for egress rules"
83+
type = list(any)
5984
}
6085

6186
variable "tags" {
6287
default = {}
6388
type = map(string)
64-
description = "Extra tags to attach to the ec2-sg resources"
89+
description = "Extra tags to attach to the security group resources"
6590
}
6691

6792
variable "name" {
6893
type = string
69-
description = "The name of the resources."
94+
description = "The name of the resources"
7095
}
7196

7297
variable "environment" {
7398
type = list(string)
74-
description = "The environment name for the resources."
99+
description = "The environment name for the resources"
75100
}
76101

77102
variable "owner" {
@@ -81,10 +106,10 @@ variable "owner" {
81106

82107
variable "cost_center" {
83108
type = string
84-
description = "Cost center identifier for the resource."
109+
description = "Cost center identifier for the resource"
85110
}
86111

87112
variable "application" {
88113
type = string
89-
description = "Name of the application related to the resource."
90-
}
114+
description = "Name of the application related to the resource"
115+
}

modules/ec2-sg/main.tf

Lines changed: 0 additions & 33 deletions
This file was deleted.

modules/ec2-sg/variables.tf

Lines changed: 0 additions & 60 deletions
This file was deleted.

modules/security-group/main.tf

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
resource "aws_security_group" "instance_sg" {
2+
name = var.sg_name
3+
description = "Security Group for Instance"
4+
vpc_id = var.vpc_id
5+
6+
dynamic "ingress" {
7+
for_each = toset(range(length(var.ingress_from_port)))
8+
content {
9+
from_port = var.ingress_from_port[ingress.key]
10+
to_port = var.ingress_to_port[ingress.key]
11+
protocol = var.ingress_protocol[ingress.key]
12+
cidr_blocks = var.ingress_cidr_block
13+
}
14+
}
15+
16+
dynamic "egress" {
17+
for_each = toset(range(length(var.egress_from_port)))
18+
content {
19+
from_port = var.egress_from_port[egress.key]
20+
to_port = var.egress_to_port[egress.key]
21+
protocol = var.egress_protocol[egress.key]
22+
cidr_blocks = var.egress_cidr_block
23+
}
24+
}
25+
26+
tags = merge(
27+
{
28+
"Name" = "${var.name}-sg"
29+
"Environment" = var.environment[0]
30+
"Owner" = var.owner
31+
"CostCenter" = var.cost_center
32+
"Application" = var.application
33+
},
34+
var.tags
35+
)
36+
37+
}
File renamed without changes.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
variable "region" {
2+
type = string
3+
description = "Region of the security group."
4+
}
5+
6+
variable "sg_name" {
7+
type = string
8+
description = "Name of the security group for the instance."
9+
}
10+
11+
variable "vpc_id" {
12+
type = string
13+
description = "ID of the VPC associated with the security group."
14+
}
15+
16+
variable "ingress_cidr_block" {
17+
type = list(string)
18+
description = "List of CIDR blocks for ingress rules of the EC2 security group."
19+
}
20+
21+
variable "ingress_from_port" {
22+
description = "List of starting ports for ingress rules of the EC2 security group."
23+
type = list(number)
24+
}
25+
26+
variable "ingress_to_port" {
27+
description = "List of ending ports for ingress rules of the EC2 security group."
28+
type = list(number)
29+
}
30+
31+
variable "ingress_protocol" {
32+
description = "List of protocols for ingress rules of the EC2 security group."
33+
type = list
34+
}
35+
36+
variable "egress_cidr_block" {
37+
type = list(string)
38+
description = "List of CIDR blocks for egress rules of the EC2 security group."
39+
}
40+
41+
variable "egress_from_port" {
42+
description = "List of starting ports for egress rules of the EC2 security group."
43+
type = list(number)
44+
}
45+
46+
variable "egress_to_port" {
47+
description = "List of ending ports for egress rules of the EC2 security group."
48+
type = list(number)
49+
}
50+
51+
variable "egress_protocol" {
52+
description = "List of protocols for egress rules of the EC2 security group."
53+
type = list
54+
}
55+
56+
variable "tags" {
57+
default = {}
58+
type = map(string)
59+
description = "Extra tags to attach to the EC2 security group resources."
60+
}
61+
62+
variable "name" {
63+
type = string
64+
description = "The name of the resources."
65+
}
66+
67+
variable "environment" {
68+
type = list(string)
69+
description = "The environment name for the resources."
70+
}
71+
72+
variable "owner" {
73+
type = string
74+
description = "Owner's name for the resource."
75+
}
76+
77+
variable "cost_center" {
78+
type = string
79+
description = "Cost center identifier for the resource."
80+
}
81+
82+
variable "application" {
83+
type = string
84+
description = "Name of the application related to the resource."
85+
}

0 commit comments

Comments
 (0)