Skip to content

Commit f7e56a1

Browse files
authored
Merge pull request #13 from arunlalp/master
[TEC-75] Refactor Security Group Module: Module Rename, VPC ID Parameter, and Egress Variables
2 parents 4708cb9 + ea15764 commit f7e56a1

File tree

9 files changed

+252
-149
lines changed

9 files changed

+252
-149
lines changed

README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,44 @@ terraform apply -var-file=../../../vars/dev/rds.tfvars
2727

2828
terraform destroy -var-file=../../../vars/dev/rds.tfvars
2929

30+
## Terraform EC2 Instance Deployment
31+
32+
1. Navigate to the `environment/dev` folder:
33+
34+
```bash
35+
cd environment/dev
36+
```
37+
38+
2. Open the `ec2.tfvars` file and modify it with your desired details. This file contains variables used in the Terraform configuration.
39+
40+
## Deployment
41+
42+
1. Initialize Terraform in the working directory:
43+
44+
```bash
45+
terraform init
46+
```
47+
48+
2. Create an execution plan:
49+
50+
```bash
51+
terraform plan -var-file=../../../vars/dev/ec2.tfvars
52+
```
53+
54+
3. Apply the changes to create the EC2 instance:
55+
56+
```bash
57+
terraform apply -var-file=../../../vars/dev/ec2.tfvars
58+
```
59+
60+
4. To destroy the EC2 instance and associated resources:
61+
62+
```bash
63+
terraform destroy -var-file=../../../vars/dev/ec2.tfvars
64+
```
65+
66+
**Note**: Always review the execution plan (`terraform plan`) before applying changes to avoid unintended modifications.
67+
3068
## Command Reference
3169

3270
Update all outputs:
@@ -39,5 +77,3 @@ Show all outputs:
3977

4078

4179

42-
43-

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.

0 commit comments

Comments
 (0)