-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.tf
319 lines (278 loc) · 11.4 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
data "aws_region" "current" {}
data "aws_availability_zones" "available" {}
locals {
tags = {
Automation = "true"
Environment = var.environment
}
}
module "db" {
source = "terraform-aws-modules/rds/aws"
version = "6.1.0"
identifier = format("%s-%s", var.environment, var.name)
db_name = var.db_name
port = var.port
engine = var.engine
username = var.master_username
password = var.custom_user_password != "" ? var.custom_user_password : var.manage_master_user_password ? null : length(random_password.master) > 0 ? random_password.master[0].result : null
multi_az = var.multi_az
subnet_ids = var.subnet_ids
kms_key_id = var.kms_key_arn
instance_class = var.instance_class
storage_type = var.storage_type
engine_version = var.engine_version
allocated_storage = var.allocated_storage
storage_encrypted = var.storage_encrypted
max_allocated_storage = var.enable_storage_autoscaling && var.max_allocated_storage != "" ? var.max_allocated_storage : null
publicly_accessible = var.publicly_accessible
performance_insights_enabled = var.performance_insights_enabled
performance_insights_retention_period = var.performance_insights_retention_period
create_db_subnet_group = var.create_db_subnet_group
replicate_source_db = var.replicate_source_db
vpc_security_group_ids = split(",", module.security_group_rds.security_group_id)
skip_final_snapshot = var.skip_final_snapshot
snapshot_identifier = var.snapshot_identifier
maintenance_window = var.maintenance_window
backup_window = var.backup_window
apply_immediately = var.apply_immediately
backup_retention_period = var.backup_retention_period
manage_master_user_password = var.manage_master_user_password ? true : false
monitoring_interval = "30"
monitoring_role_name = format("%s-%s-RDSPostgresql", var.name, var.environment)
create_monitoring_role = true
final_snapshot_identifier_prefix = var.final_snapshot_identifier_prefix
enabled_cloudwatch_logs_exports = ["postgresql"]
tags = merge(
{ "Name" = format("%s-%s", var.environment, var.name) },
local.tags,
)
# DB parameter group
family = var.family
# DB option group
major_engine_version = var.major_engine_version
# Database Deletion Protection
deletion_protection = var.deletion_protection
}
module "db_replica" {
source = "terraform-aws-modules/rds/aws"
version = "6.1.0"
count = var.replica_enable ? var.replica_count : 0
identifier = format("%s-%s-%s", var.environment, var.name, "replica")
port = var.port
engine = var.engine
multi_az = var.multi_az
kms_key_id = var.kms_key_arn
instance_class = var.instance_class
storage_type = var.storage_type
engine_version = var.engine_version
storage_encrypted = var.storage_encrypted
publicly_accessible = var.publicly_accessible
replicate_source_db = module.db.db_instance_identifier
vpc_security_group_ids = split(",", module.security_group_rds.security_group_id)
skip_final_snapshot = var.skip_final_snapshot
snapshot_identifier = var.snapshot_identifier
maintenance_window = var.maintenance_window
backup_window = var.backup_window
apply_immediately = var.apply_immediately
backup_retention_period = var.backup_retention_period
monitoring_interval = "30"
monitoring_role_arn = module.db.enhanced_monitoring_iam_role_arn
create_monitoring_role = false
create_cloudwatch_log_group = false
final_snapshot_identifier_prefix = var.final_snapshot_identifier_prefix
enabled_cloudwatch_logs_exports = ["postgresql"]
tags = merge(
{ "Name" = format("%s-%s", var.environment, var.name) },
local.tags,
)
# DB parameter group
family = var.family
# DB option group
major_engine_version = var.major_engine_version
# Database Deletion Protection
deletion_protection = var.deletion_protection
depends_on = [module.db]
}
resource "aws_security_group_rule" "default_ingress" {
count = var.create_security_group && length(var.allowed_security_groups) > 0 ? 1 : 0
description = "From allowed SGs"
type = "ingress"
to_port = var.port
from_port = var.port
protocol = "tcp"
source_security_group_id = element(var.allowed_security_groups, count.index)
security_group_id = module.security_group_rds.security_group_id
}
resource "aws_security_group_rule" "cidr_ingress" {
count = var.create_security_group && length(var.allowed_cidr_blocks) > 0 ? 1 : 0
description = "From allowed CIDRs"
type = "ingress"
to_port = var.port
from_port = var.port
protocol = "tcp"
cidr_blocks = var.allowed_cidr_blocks
security_group_id = module.security_group_rds.security_group_id
}
module "security_group_rds" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 4"
name = format("%s-%s-%s", var.environment, var.name, "rds-sg")
create = var.create_security_group
vpc_id = var.vpc_id
description = "Complete PostgreSQL example 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, "rds-sg") },
local.tags,
)
}
resource "aws_secretsmanager_secret" "secret_master_db" {
name = format("%s/%s/%s", var.environment, var.name, "rds-postgresql-pass")
tags = merge(
{ "Name" = format("%s/%s/%s", var.environment, var.name, "rds-mysql-pass") },
local.tags,
)
}
resource "random_password" "master" {
count = var.manage_master_user_password ? 0 : var.custom_user_password == "" ? 1 : 0
length = var.random_password_length
special = false
}
resource "aws_secretsmanager_secret_version" "rds_credentials" {
count = length(random_password.master) > 0 ? 1 : 0
secret_id = aws_secretsmanager_secret.secret_master_db.id
secret_string = <<EOF
{
"username": "${module.db.db_instance_username}",
"password": length(random_password.master) > 0 ? element(random_password.master, 0).result : var.custom_password,
"engine": "${var.engine}",
"host": "${module.db.db_instance_endpoint}"
}
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/RDS"
period = "300"
statistic = "Average"
threshold = var.alarm_cpu_threshold_percent
dimensions = {
DBInstanceIdentifier = module.db.db_instance_identifier
}
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" "disk_free_storage_space_too_low" {
count = var.cloudwatch_metric_alarms_enabled ? 1 : 0
alarm_name = format("%s-%s-%s", var.environment, var.name, "free-storage-space")
alarm_description = "Redis cluster freeable memory"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
metric_name = "FreeStorageSpace"
namespace = "AWS/RDS"
period = "60"
statistic = "Average"
threshold = var.disk_free_storage_space
dimensions = {
DBInstanceIdentifier = module.db.db_instance_identifier
}
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, "free-storage-space") },
local.tags,
)
}
resource "aws_kms_key" "this" {
count = var.cloudwatch_metric_alarms_enabled ? 1 : 0
description = "KMS key for notify-slack test"
}
resource "aws_kms_ciphertext" "slack_url" {
count = var.cloudwatch_metric_alarms_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 = [module.db]
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" {
count = var.slack_notification_enabled ? 1 : 0
source = "./lambda"
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
}