-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.tf
305 lines (269 loc) · 10.3 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
locals {
tags = {
Environment = var.environment
}
slow_log = var.slow_log_destination == null ? [] : [1]
engine_log = var.engine_log_destination == null ? [] : [1]
}
resource "random_password" "password" {
count = var.transit_encryption_enabled ? 1 : 0
length = 16
special = false
}
resource "aws_elasticache_parameter_group" "default" {
name = "${var.environment}-${var.name}-redis-parameter-group"
family = var.family
description = var.parameter_group_description != null ? var.parameter_group_description : "Elasticache parameter group for ${var.environment}-${var.name}"
tags = {
Name = "${var.environment}-${var.name}-redis-parameter-group"
Environment = var.environment
}
# Ignore changes to the description since it will try to recreate the resource
lifecycle {
ignore_changes = [
description,
]
}
dynamic "parameter" {
for_each = var.cluster_mode_enabled ? concat([{ name = "cluster-enabled", value = "yes" }], var.parameter) : var.parameter
content {
name = parameter.value.name
value = tostring(parameter.value.value)
}
}
}
resource "aws_elasticache_replication_group" "redis" {
replication_group_id = "${var.environment}-${var.name}-redis"
port = var.port
engine = "redis"
node_type = var.node_type
description = "Redis cluster for ${var.environment}-${var.name}-redis"
engine_version = var.engine_version
num_cache_clusters = var.cluster_mode_enabled ? null : var.num_cache_nodes
parameter_group_name = join("", aws_elasticache_parameter_group.default.*.name) #var.parameter_group_name
security_group_ids = [module.security_group_redis.security_group_id]
subnet_group_name = aws_elasticache_subnet_group.elasticache.id
preferred_cache_cluster_azs = length(var.availability_zones) == 0 ? null : [for n in range(0, var.num_cache_nodes) : element(var.availability_zones, n)]
snapshot_arns = var.snapshot_arns
snapshot_window = var.snapshot_window
snapshot_retention_limit = var.snapshot_retention_limit
automatic_failover_enabled = var.cluster_mode_enabled ? true : var.automatic_failover_enabled
multi_az_enabled = var.multi_az_enabled
at_rest_encryption_enabled = var.at_rest_encryption_enabled
kms_key_id = var.at_rest_encryption_enabled ? var.kms_key_arn : null
auth_token = var.transit_encryption_enabled ? random_password.password[0].result : null
transit_encryption_enabled = var.transit_encryption_enabled ? var.transit_encryption_enabled || random_password.password[0].result != null : false
notification_topic_arn = var.notification_topic_arn
maintenance_window = var.maintenance_window
final_snapshot_identifier = var.final_snapshot_identifier
num_node_groups = var.cluster_mode_enabled ? var.cluster_mode_num_node_groups : null
replicas_per_node_group = var.cluster_mode_enabled ? var.cluster_mode_replicas_per_node_group : null
dynamic "log_delivery_configuration" {
for_each = local.slow_log
content {
log_type = "slow-log"
log_format = var.slow_log_format
destination = var.slow_log_destination
destination_type = var.slow_log_destination_type
}
}
dynamic "log_delivery_configuration" {
for_each = local.engine_log
content {
log_type = "engine-log"
log_format = var.engine_log_format
destination = var.engine_log_destination
destination_type = var.engine_log_destination_type
}
}
tags = {
Name = "${var.environment}-${var.name}"
Environment = var.environment
}
}
resource "aws_elasticache_subnet_group" "elasticache" {
name = "${var.environment}-${var.name}-redis"
subnet_ids = var.subnets
description = "Elastic-cache Redis subnet-group"
tags = {
Name = "${var.environment}-${var.name}-redis"
Environment = var.environment
}
}
resource "aws_security_group_rule" "default_ingress" {
count = length(var.allowed_security_groups) > 0 ? length(var.allowed_security_groups) : 0
description = "From allowed SGs"
type = "ingress"
to_port = var.port
from_port = var.port
protocol = "tcp"
security_group_id = module.security_group_redis.security_group_id
source_security_group_id = element(var.allowed_security_groups, count.index)
}
resource "aws_security_group_rule" "cidr_ingress" {
count = length(var.allowed_cidr_blocks) > 0 ? length(var.allowed_cidr_blocks) : 0
description = "From allowed CIDRs"
type = "ingress"
to_port = var.port
from_port = var.port
protocol = "tcp"
cidr_blocks = element(var.allowed_cidr_blocks, count.index)
security_group_id = module.security_group_redis.security_group_id
}
module "security_group_redis" {
source = "terraform-aws-modules/security-group/aws"
version = "4.13.0"
name = format("%s-%s-%s", var.environment, var.name, "redis-sg")
create = true
vpc_id = var.vpc_id
description = "Elastic-cache Redis security group"
egress_with_cidr_blocks = [
{
to_port = 0
from_port = 0
protocol = "-1"
cidr_blocks = "0.0.0.0/0"
},
]
tags = merge(
{ "Name" = format("%s-%s-%s", var.environment, var.name, "redis-sg") },
local.tags,
)
}
resource "aws_secretsmanager_secret" "secret_redis" {
count = var.transit_encryption_enabled ? 1 : 0
name = format("%s/%s/%s", var.environment, var.name, "redis-auth-token")
tags = merge(
{ "Name" = format("%s/%s/%s", var.environment, var.name, "redis-auth-token") },
local.tags,
)
recovery_window_in_days = var.recovery_window_aws_secret
}
resource "aws_secretsmanager_secret_version" "redis_credentials" {
count = var.transit_encryption_enabled ? 1 : 0
secret_id = aws_secretsmanager_secret.secret_redis[0].id
secret_string = <<EOF
{
"password": "${random_password.password[0].result}"
}
EOF
}
# Cloudwatch alarms
resource "aws_cloudwatch_metric_alarm" "cache_cpu" {
count = var.cloudwatch_metric_alarms_enabled ? 1 : 0
alarm_name = format("%s-%s-%s", var.environment, var.name, "cpu-utilization")
alarm_description = "Redis cluster CPU utilization"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = "CPUUtilization"
namespace = "AWS/ElastiCache"
period = "300"
statistic = "Average"
threshold = var.alarm_cpu_threshold_percent
dimensions = {
CacheClusterId = aws_elasticache_replication_group.redis.id
}
alarm_actions = [aws_sns_topic.slack_topic[0].arn]
ok_actions = [aws_sns_topic.slack_topic[0].arn]
depends_on = [aws_sns_topic.slack_topic]
tags = merge(
{ "Name" = format("%s-%s-%s", var.environment, var.name, "cpu_metric") },
local.tags,
)
}
resource "aws_cloudwatch_metric_alarm" "cache_memory" {
count = var.cloudwatch_metric_alarms_enabled ? 1 : 0
alarm_name = format("%s-%s-%s", var.environment, var.name, "used-memory")
alarm_description = "Redis cluster freeable memory"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
metric_name = "FreeableMemory"
namespace = "AWS/ElastiCache"
period = "60"
statistic = "Average"
threshold = var.alarm_memory_threshold_bytes
dimensions = {
CacheClusterId = aws_elasticache_replication_group.redis.id
}
alarm_actions = [aws_sns_topic.slack_topic[0].arn]
ok_actions = [aws_sns_topic.slack_topic[0].arn]
depends_on = [aws_sns_topic.slack_topic]
tags = merge(
{ "Name" = format("%s-%s-%s", var.environment, var.name, "memory-metric") },
local.tags,
)
}
resource "aws_kms_key" "this" {
count = var.slack_notification_enabled ? 1 : 0
description = "KMS key for notify-slack test"
}
resource "aws_kms_ciphertext" "slack_url" {
count = var.slack_notification_enabled ? 1 : 0
plaintext = var.slack_webhook_url
key_id = aws_kms_key.this[0].arn
}
resource "aws_sns_topic" "slack_topic" {
count = var.cloudwatch_metric_alarms_enabled ? 1 : 0
depends_on = [aws_elasticache_replication_group.redis]
name = format("%s-%s-%s", var.environment, var.name, "slack-topic")
delivery_policy = <<EOF
{
"http": {
"defaultHealthyRetryPolicy": {
"minDelayTarget": 20,
"maxDelayTarget": 20,
"numRetries": 3,
"numMaxDelayRetries": 0,
"numNoDelayRetries": 0,
"numMinDelayRetries": 0,
"backoffFunction": "linear"
},
"disableSubscriptionOverrides": false,
"defaultThrottlePolicy": {
"maxReceivesPerSecond": 1
}
}
}
EOF
}
data "archive_file" "lambdazip" {
count = var.slack_notification_enabled ? 1 : 0
type = "zip"
output_path = "${path.module}/lambda/sns_slack.zip"
source_dir = "${path.module}/lambda/"
}
module "cw_sns_slack" {
source = "./lambda"
count = var.slack_notification_enabled ? 1 : 0
name = format("%s-%s-%s", var.environment, var.name, "sns-slack")
description = "notify slack channel on sns topic"
artifact_file = "${path.module}/lambda/sns_slack.zip"
handler = "sns_slack.lambda_handler"
runtime = "python3.8"
memory_size = 128
timeout = 30
environment = {
"SLACK_URL" = var.slack_webhook_url
"SLACK_CHANNEL" = var.slack_channel
"SLACK_USER" = var.slack_username
}
tags = merge(
{ "Name" = format("%s-%s-%s", var.environment, var.name, "lambda") },
local.tags,
)
}
resource "aws_sns_topic_subscription" "slack-endpoint" {
count = var.slack_notification_enabled ? 1 : 0
endpoint = module.cw_sns_slack[0].arn
protocol = "lambda"
endpoint_auto_confirms = true
topic_arn = aws_sns_topic.slack_topic[0].arn
}
resource "aws_lambda_permission" "sns_lambda_slack_invoke" {
count = var.slack_notification_enabled ? 1 : 0
statement_id = "sns_slackAllowExecutionFromSNS"
action = "lambda:InvokeFunction"
function_name = module.cw_sns_slack[0].arn
principal = "sns.amazonaws.com"
source_arn = aws_sns_topic.slack_topic[0].arn
}