Skip to content

Commit 9ec969f

Browse files
Added Security Group Module
1 parent 4ea7b11 commit 9ec969f

File tree

10 files changed

+163
-13
lines changed

10 files changed

+163
-13
lines changed

main.tf

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,21 @@ module "nat_gateway" {
4444
}
4545

4646
# AWS VPC Subnets Module - Private Subnet
47-
# module "private_subnet" {
48-
# source = "./modules/subnets"
49-
# vpc_id = aws_vpc.vpc.id
50-
# aws_nat_gateway_id = module.nat_gateway.nat_gateway_ids
51-
# cidr = var.cidr
52-
# prefix = var.prefix
53-
# environment = var.environment
54-
# subnet_bits = var.subnet_bits
55-
# subnet_type = ["private", "storage"]
56-
# }
47+
module "private_subnet" {
48+
source = "./modules/subnets"
49+
vpc_id = aws_vpc.vpc.id
50+
aws_nat_gateway_id = module.nat_gateway.nat_gateway_ids
51+
cidr = var.cidr
52+
prefix = var.prefix
53+
environment = var.environment
54+
subnet_bits = var.subnet_bits
55+
subnet_type = ["private", "storage"]
56+
}
57+
58+
# AWS VPC Security Groups Module
59+
module "security_group" {
60+
source = "./modules/security-groups"
61+
vpc_id = aws_vpc.vpc.id
62+
prefix = var.prefix
63+
environment = var.environment
64+
}

modules/security-groups/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Security Group Module

modules/security-groups/main.tf

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# AWS Public Security Group
2+
module "public_security_group" {
3+
source = "./resources"
4+
vpc_id = var.vpc_id
5+
prefix = var.prefix
6+
environment = var.environment
7+
sg_type = "public"
8+
sg_description = "Allow connections from internet"
9+
}
10+
11+
# 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+
}
20+
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+
}
29+
30+
# AWS Private Security Group
31+
module "private_security_group" {
32+
source = "./resources"
33+
vpc_id = var.vpc_id
34+
prefix = var.prefix
35+
environment = var.environment
36+
sg_type = "private"
37+
sg_description = "The private security group to allows inbound traffic from public group"
38+
}
39+
40+
# AWS Private Security Group Rules
41+
resource "aws_security_group_rule" "allow_inbound_private" {
42+
type = "ingress"
43+
from_port = 0
44+
to_port = 65535
45+
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
48+
}
49+
50+
# AWS Storage Security Group
51+
module "storage_security_group" {
52+
source = "./resources"
53+
vpc_id = var.vpc_id
54+
prefix = var.prefix
55+
environment = var.environment
56+
sg_type = "storage"
57+
sg_description = "The storage security group to allows inbound traffic from private group"
58+
}
59+
60+
# AWS Storage Security Group Rules
61+
resource "aws_security_group_rule" "allow_inbound_storage" {
62+
type = "ingress"
63+
from_port = 0
64+
to_port = 65535
65+
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
68+
}

modules/security-groups/output.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "public_security_group_id" {
2+
value = module.public_security_group.security_group_id
3+
}
4+
5+
output "private_security_group_id" {
6+
value = module.private_security_group.security_group_id
7+
}
8+
9+
output "storage_security_group_id" {
10+
value = module.storage_security_group.security_group_id
11+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# AWS Security Group
2+
resource "aws_security_group" "security_group" {
3+
name = "${var.prefix}-${var.sg_type}"
4+
description = var.sg_description
5+
vpc_id = var.vpc_id
6+
revoke_rules_on_delete = true
7+
8+
tags = {
9+
Name = "${var.prefix}-${var.sg_type}"
10+
Type = var.sg_type
11+
Environment = var.environment
12+
}
13+
}
14+
15+
# 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+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "security_group_id" {
2+
value = aws_security_group.security_group.id
3+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
variable "vpc_id" {
2+
description = "VPC ID to which subnet mask will bind to"
3+
type = string
4+
}
5+
6+
variable "prefix" {
7+
description = "generic naming resources"
8+
type = string
9+
}
10+
11+
variable "environment" {
12+
description = "To apply generic environment to AWS VPC Resources"
13+
type = string
14+
}
15+
16+
variable "sg_type" {
17+
description = "Security Group type Eg: public, private and storage"
18+
type = string
19+
}
20+
21+
variable "sg_description" {
22+
description = "Generic security group description"
23+
type = string
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
variable "vpc_id" {
2+
description = "VPC ID to which subnet mask will bind to"
3+
type = string
4+
}
5+
6+
variable "prefix" {
7+
description = "generic naming resources"
8+
type = string
9+
}
10+
11+
variable "environment" {
12+
description = "To apply generic environment to AWS VPC Resources"
13+
type = string
14+
}
15+

modules/vpc-endpoints/.gitkeep

Whitespace-only changes.

variables.tf

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22
variable "aws_region" {
33
description = "AWS Default Region"
44
type = string
5-
default = "us-east-1"
65
}
76

87
variable "prefix" {
98
description = "To apply generic naming to AWS VPC Resources"
109
type = string
11-
default = "copper"
1210
}
1311

1412
variable "environment" {
1513
description = "To apply generic environment to AWS VPC Resources"
1614
type = string
17-
default = "devops"
1815
}
1916

2017
variable "cidr" {

0 commit comments

Comments
 (0)