-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsg.tf
59 lines (53 loc) · 1.82 KB
/
sg.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
locals {
security_group = {
ingress = flatten([
[
for range in var.public_allowlist : [
for port in var.ports : {
from = port
to = port
proto = port == 0 ? "-1" : "tcp"
cidrs = [range]
desc = "Allow ${port} from ${range} for incoming traffic"
}
]
]
])
egress = [{
from = 0
to = 0
proto = -1
cidrs = ["0.0.0.0/0"]
desc = "Allow any outgoing traffic"
}]
}
}
resource "aws_security_group" "this" {
name = "${var.name}-sg"
description = "Controls access to ${var.fullname} instance"
vpc_id = local.vpc_id
tags = merge(local.tags, {
Name = "${var.name}-sg"
Description = "Controls access to ${var.fullname} instance",
})
}
resource "aws_security_group_rule" "ingress" {
count = length(local.security_group.ingress)
type = "ingress"
from_port = local.security_group.ingress[count.index].from
to_port = local.security_group.ingress[count.index].to
protocol = local.security_group.ingress[count.index].proto
cidr_blocks = local.security_group.ingress[count.index].cidrs
description = local.security_group.ingress[count.index].desc
security_group_id = aws_security_group.this.id
}
resource "aws_security_group_rule" "egress" {
count = length(local.security_group.egress)
type = "egress"
from_port = local.security_group.egress[count.index].from
to_port = local.security_group.egress[count.index].to
protocol = local.security_group.egress[count.index].proto
cidr_blocks = local.security_group.egress[count.index].cidrs
description = local.security_group.egress[count.index].desc
security_group_id = aws_security_group.this.id
}