-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
174 lines (132 loc) · 5.34 KB
/
main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
resource "aws_elasticache_replication_group" "redis" {
engine = var.global_replication_group_id == null ? "redis" : null
parameter_group_name = var.global_replication_group_id == null ? aws_elasticache_parameter_group.redis.name : null
subnet_group_name = aws_elasticache_subnet_group.redis.name
security_group_ids = concat(var.security_group_ids, [aws_security_group.redis.id])
preferred_cache_cluster_azs = var.preferred_cache_cluster_azs
replication_group_id = var.global_replication_group_id == null ? "${var.name_prefix}-redis" : "${var.name_prefix}-redis-replica"
node_type = var.global_replication_group_id == null ? var.node_type : null
engine_version = var.global_replication_group_id == null ? var.engine_version : null
port = var.port
maintenance_window = var.maintenance_window
snapshot_window = var.snapshot_window
snapshot_retention_limit = var.snapshot_retention_limit
final_snapshot_identifier = var.final_snapshot_identifier
automatic_failover_enabled = var.automatic_failover_enabled && var.num_cache_clusters >= 2 ? true : false
auto_minor_version_upgrade = var.auto_minor_version_upgrade
multi_az_enabled = var.multi_az_enabled
at_rest_encryption_enabled = var.global_replication_group_id == null ? var.at_rest_encryption_enabled : null
transit_encryption_enabled = var.global_replication_group_id == null ? var.transit_encryption_enabled : null
auth_token = var.auth_token
auth_token_update_strategy = var.auth_token_update_strategy
kms_key_id = var.at_rest_encryption_enabled ? var.kms_key_id : null
global_replication_group_id = var.global_replication_group_id
apply_immediately = var.apply_immediately
description = var.description
data_tiering_enabled = var.data_tiering_enabled
notification_topic_arn = var.notification_topic_arn
replicas_per_node_group = var.cluster_mode_enabled ? var.replicas_per_node_group : null
num_node_groups = var.cluster_mode_enabled ? var.num_node_groups : null
num_cache_clusters = var.cluster_mode_enabled ? null : var.num_cache_clusters
user_group_ids = var.user_group_ids
dynamic "log_delivery_configuration" {
for_each = var.log_delivery_configuration
content {
destination_type = log_delivery_configuration.value.destination_type
destination = log_delivery_configuration.value.destination
log_format = log_delivery_configuration.value.log_format
log_type = log_delivery_configuration.value.log_type
}
}
tags = merge(
{
"Name" = "${var.name_prefix}-redis"
},
var.tags,
)
}
resource "random_id" "redis_pg" {
keepers = {
family = var.family
description = var.description
}
byte_length = 2
}
resource "aws_elasticache_parameter_group" "redis" {
name = var.parameter_group_name == null ? "${var.name_prefix}-redis-${random_id.redis_pg.hex}" : var.parameter_group_name
family = var.family
description = var.description
dynamic "parameter" {
for_each = var.num_node_groups > 0 ? concat([{ name = "cluster-enabled", value = "yes" }], var.parameter) : var.parameter
content {
name = parameter.value.name
value = tostring(parameter.value.value)
}
}
lifecycle {
create_before_destroy = true
ignore_changes = [
description,
]
}
tags = var.tags
}
resource "aws_elasticache_subnet_group" "redis" {
name = var.global_replication_group_id == null ? "${var.name_prefix}-redis-sg" : "${var.name_prefix}-redis-sg-replica"
subnet_ids = var.subnet_ids
description = var.description
tags = var.tags
}
resource "aws_security_group" "redis" {
name_prefix = "${var.name_prefix}-redis-"
description = "Elasticache Security Group ${var.name_prefix}"
vpc_id = var.vpc_id
tags = merge(
{
"Name" = "${var.name_prefix}-redis"
},
var.tags
)
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group_rule" "redis_ingress_self" {
count = var.ingress_self ? 1 : 0
description = "Self reference ingress."
type = "ingress"
from_port = var.port
to_port = var.port
protocol = "tcp"
self = true
security_group_id = aws_security_group.redis.id
}
resource "aws_security_group_rule" "redis_ingress_cidr_blocks" {
count = length(var.ingress_cidr_blocks) != 0 ? 1 : 0
description = "Allow Ingress CIDRs."
type = "ingress"
from_port = var.port
to_port = var.port
protocol = "tcp"
cidr_blocks = var.ingress_cidr_blocks
security_group_id = aws_security_group.redis.id
}
resource "aws_security_group_rule" "redis_egress" {
description = "Allow all egress."
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.redis.id
}
resource "aws_security_group_rule" "other_sg_ingress" {
count = length(var.allowed_security_groups)
description = "Allow additional Ingress SGs."
type = "ingress"
from_port = var.port
to_port = var.port
protocol = "tcp"
source_security_group_id = element(var.allowed_security_groups, count.index)
security_group_id = aws_security_group.redis.id
}