generated from DNXLabs/terraform-aws-template
-
Notifications
You must be signed in to change notification settings - Fork 9
/
sg.tf
82 lines (71 loc) · 2.8 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
resource "aws_security_group" "rds_db" {
name = "rds-${var.environment_name}-${var.name}"
vpc_id = var.vpc_id
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group" "rds_db_replica" {
name = "rds-${var.environment_name}-${var.name}-replica"
vpc_id = var.vpc_id
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group_rule" "rds_db_inbound_cidrs" {
count = length(var.allow_cidrs) != 0 ? 1 : 0
type = "ingress"
from_port = var.port
to_port = var.port
protocol = "tcp"
cidr_blocks = var.allow_cidrs
security_group_id = aws_security_group.rds_db.id
description = "From CIDR ${join(", ", var.allow_cidrs)}"
}
resource "aws_security_group_rule" "rds_db_inbound_from_sg" {
for_each = { for security_group_id in var.allow_security_group_ids : security_group_id.name => security_group_id }
type = "ingress"
from_port = var.port
to_port = var.port
protocol = "tcp"
source_security_group_id = each.value.security_group_id
security_group_id = aws_security_group.rds_db.id
description = try(each.value.description, "From ${each.value.security_group_id}")
}
resource "aws_security_group_rule" "egress_rule" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
security_group_id = aws_security_group.rds_db.id
cidr_blocks = ["0.0.0.0/0"]
}
##SG for Replica
resource "aws_security_group_rule" "rds_db_inbound_cidrs_replica" {
count = length(var.allow_cidrs_replica) != 0 ? 1 : 0
type = "ingress"
from_port = var.port
to_port = var.port
protocol = "tcp"
cidr_blocks = var.allow_cidrs_replica
security_group_id = aws_security_group.rds_db_replica.id
description = "From CIDR ${join(", ", var.allow_cidrs_replica)}"
}
resource "aws_security_group_rule" "rds_db_inbound_from_sg_replica" {
for_each = { for security_group_id in var.allow_security_group_ids_replica : security_group_id.name => security_group_id }
type = "ingress"
from_port = var.port
to_port = var.port
protocol = "tcp"
source_security_group_id = each.value.security_group_id
security_group_id = aws_security_group.rds_db_replica.id
description = try(each.value.description, "From ${each.value.security_group_id}")
}
resource "aws_security_group_rule" "egress_rule_replica" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
security_group_id = aws_security_group.rds_db_replica.id
cidr_blocks = ["0.0.0.0/0"]
}