-
Notifications
You must be signed in to change notification settings - Fork 0
/
security-group.tf
119 lines (102 loc) · 4.32 KB
/
security-group.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
######################################################################
# Copyright (c) 2021 Claudio André <claudioandre.br at gmail.com>
#
# This program comes with ABSOLUTELY NO WARRANTY; express or implied.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, as expressed in version 2, seen at
# http://www.gnu.org/licenses/gpl-2.0.html
######################################################################
# Access Security Group Rule #########################################
variable "web_ingress_data" {
description = "The security groups inbound rules."
type = map(object({ port = string, description = string, cidr_blocks = list(string) }))
default = {
80 = { port = "80", description = "Inbound HTTP rule.", cidr_blocks = ["0.0.0.0/0"] }
}
}
variable "web_egress_data" {
description = "The security groups outbound rules."
type = map(object({ port = string, description = string, cidr_blocks = list(string), ipv6_cidr_blocks = list(string) }))
default = {
443 = { port = "443", description = "Outbound HTTPS rule.", cidr_blocks = ["0.0.0.0/0"], ipv6_cidr_blocks = ["::/0"] }
}
}
variable "internal_ingress_data" {
description = "The security groups inbound rules."
type = map(object({ port = string, description = string, cidr_blocks = list(string) }))
default = {
80 = { port = "80", description = "From ALB inbound HTTP rule.", cidr_blocks = ["Not used"] }
}
}
variable "internal_egress_data" {
description = "The security groups inbound rules."
type = map(object({ port = string, description = string, cidr_blocks = list(string) }))
default = {
80 = { port = "80", description = "ALB outbound HTTP rule.", cidr_blocks = ["Not used"] }
}
}
resource "aws_security_group_rule" "webserver-in" {
security_group_id = aws_security_group.webserver-sg.id
type = "ingress"
description = var.internal_ingress_data[80].description
from_port = var.internal_ingress_data[80].port
to_port = var.internal_ingress_data[80].port
protocol = "tcp"
source_security_group_id = aws_security_group.alb-sg.id
}
resource "aws_security_group_rule" "webserver-out" {
security_group_id = aws_security_group.webserver-sg.id
type = "egress"
description = var.web_egress_data[443].description
from_port = var.web_egress_data[443].port
to_port = var.web_egress_data[443].port
protocol = "tcp"
cidr_blocks = var.web_egress_data[443].cidr_blocks
ipv6_cidr_blocks = var.web_egress_data[443].ipv6_cidr_blocks
}
resource "aws_security_group_rule" "alb-in" {
security_group_id = aws_security_group.alb-sg.id
type = "ingress"
description = var.web_ingress_data[80].description
from_port = var.web_ingress_data[80].port
to_port = var.web_ingress_data[80].port
protocol = "tcp"
cidr_blocks = var.web_ingress_data[80].cidr_blocks
}
resource "aws_security_group_rule" "alb-out" {
security_group_id = aws_security_group.alb-sg.id
type = "egress"
description = var.internal_egress_data[80].description
from_port = var.internal_egress_data[80].port
to_port = var.internal_egress_data[80].port
protocol = "tcp"
source_security_group_id = aws_security_group.webserver-sg.id
}
resource "aws_security_group" "webserver-sg" {
name = "web-server-sg"
description = "Enable HTTP access from ALB. Allow access to obtain software updates and to DynamoDB."
vpc_id = module.vpc.vpc_id
tags = {
Name = "sg-web-server"
Environment = var.domain
"Application Role" = var.role
Owner = var.owner
Customer = var.customer
Confidentiality = var.confidentiality
}
}
resource "aws_security_group" "alb-sg" {
name = "elastic-balancer-sg"
description = "Enable HTTP access from Internet."
vpc_id = module.vpc.vpc_id
tags = {
Name = "sg-elastic-balancer"
Environment = var.domain
"Application Role" = var.role
Owner = var.owner
Customer = var.customer
Confidentiality = var.confidentiality
}
}