forked from umotif-public/terraform-aws-elasticache-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
128 lines (102 loc) · 4.06 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
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"
num_cache_clusters = var.cluster_mode_enabled ? null : var.num_cache_clusters
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 != "" ? var.auth_token : null
kms_key_id = var.kms_key_id
global_replication_group_id = var.global_replication_group_id
apply_immediately = var.apply_immediately
description = var.description
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
tags = merge(
{
"Name" = "${var.name_prefix}-redis"
},
var.tags,
)
}
resource "random_id" "redis_pg" {
keepers = {
family = var.family
}
byte_length = 2
}
resource "aws_elasticache_parameter_group" "redis" {
name = "${var.name_prefix}-redis-${random_id.redis_pg.hex}"
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 = parameter.value.value
}
}
lifecycle {
create_before_destroy = true
}
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-"
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
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
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" {
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
}