-
Notifications
You must be signed in to change notification settings - Fork 68
/
main.tf
190 lines (166 loc) · 6.46 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
#
# IAM resources
#
data "aws_iam_policy_document" "enhanced_monitoring" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["monitoring.rds.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
resource "aws_iam_role" "enhanced_monitoring" {
name = "rds${var.environment}EnhancedMonitoringRole"
assume_role_policy = data.aws_iam_policy_document.enhanced_monitoring.json
}
resource "aws_iam_role_policy_attachment" "enhanced_monitoring" {
role = aws_iam_role.enhanced_monitoring.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole"
}
#
# Security group resources
#
resource "aws_security_group" "postgresql" {
vpc_id = var.vpc_id
tags = merge(
{
Name = "sgDatabaseServer",
Project = var.project,
Environment = var.environment
},
var.tags
)
}
#
# RDS resources
#
resource "aws_db_instance" "postgresql" {
allocated_storage = var.allocated_storage
engine = "postgres"
engine_version = var.engine_version
identifier = var.database_identifier
snapshot_identifier = var.snapshot_identifier
instance_class = var.instance_type
storage_type = var.storage_type
iops = var.iops
name = var.database_name
password = var.database_password
username = var.database_username
backup_retention_period = var.backup_retention_period
backup_window = var.backup_window
maintenance_window = var.maintenance_window
auto_minor_version_upgrade = var.auto_minor_version_upgrade
final_snapshot_identifier = var.final_snapshot_identifier
skip_final_snapshot = var.skip_final_snapshot
copy_tags_to_snapshot = var.copy_tags_to_snapshot
multi_az = var.multi_availability_zone
port = var.database_port
vpc_security_group_ids = [aws_security_group.postgresql.id]
db_subnet_group_name = var.subnet_group
parameter_group_name = var.parameter_group
storage_encrypted = var.storage_encrypted
monitoring_interval = var.monitoring_interval
monitoring_role_arn = var.monitoring_interval > 0 ? aws_iam_role.enhanced_monitoring.arn : ""
deletion_protection = var.deletion_protection
enabled_cloudwatch_logs_exports = var.cloudwatch_logs_exports
tags = merge(
{
Name = "DatabaseServer",
Project = var.project,
Environment = var.environment
},
var.tags
)
}
#
# CloudWatch resources
#
resource "aws_cloudwatch_metric_alarm" "database_cpu" {
alarm_name = "alarm${var.environment}DatabaseServerCPUUtilization-${var.database_identifier}"
alarm_description = "Database server CPU utilization"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = "CPUUtilization"
namespace = "AWS/RDS"
period = "300"
statistic = "Average"
threshold = var.alarm_cpu_threshold
dimensions = {
DBInstanceIdentifier = aws_db_instance.postgresql.id
}
alarm_actions = var.alarm_actions
ok_actions = var.ok_actions
insufficient_data_actions = var.insufficient_data_actions
}
resource "aws_cloudwatch_metric_alarm" "database_disk_queue" {
alarm_name = "alarm${var.environment}DatabaseServerDiskQueueDepth-${var.database_identifier}"
alarm_description = "Database server disk queue depth"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = "DiskQueueDepth"
namespace = "AWS/RDS"
period = "60"
statistic = "Average"
threshold = var.alarm_disk_queue_threshold
dimensions = {
DBInstanceIdentifier = aws_db_instance.postgresql.id
}
alarm_actions = var.alarm_actions
ok_actions = var.ok_actions
insufficient_data_actions = var.insufficient_data_actions
}
resource "aws_cloudwatch_metric_alarm" "database_disk_free" {
alarm_name = "alarm${var.environment}DatabaseServerFreeStorageSpace-${var.database_identifier}"
alarm_description = "Database server free storage space"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
metric_name = "FreeStorageSpace"
namespace = "AWS/RDS"
period = "60"
statistic = "Average"
threshold = var.alarm_free_disk_threshold
dimensions = {
DBInstanceIdentifier = aws_db_instance.postgresql.id
}
alarm_actions = var.alarm_actions
ok_actions = var.ok_actions
insufficient_data_actions = var.insufficient_data_actions
}
resource "aws_cloudwatch_metric_alarm" "database_memory_free" {
alarm_name = "alarm${var.environment}DatabaseServerFreeableMemory-${var.database_identifier}"
alarm_description = "Database server freeable memory"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
metric_name = "FreeableMemory"
namespace = "AWS/RDS"
period = "60"
statistic = "Average"
threshold = var.alarm_free_memory_threshold
dimensions = {
DBInstanceIdentifier = aws_db_instance.postgresql.id
}
alarm_actions = var.alarm_actions
ok_actions = var.ok_actions
insufficient_data_actions = var.insufficient_data_actions
}
resource "aws_cloudwatch_metric_alarm" "database_cpu_credits" {
// This results in 1 if instance_type starts with "db.t", 0 otherwise.
count = substr(var.instance_type, 0, 3) == "db.t" ? 1 : 0
alarm_name = "alarm${var.environment}DatabaseCPUCreditBalance-${var.database_identifier}"
alarm_description = "Database CPU credit balance"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
metric_name = "CPUCreditBalance"
namespace = "AWS/RDS"
period = "60"
statistic = "Average"
threshold = var.alarm_cpu_credit_balance_threshold
dimensions = {
DBInstanceIdentifier = aws_db_instance.postgresql.id
}
alarm_actions = var.alarm_actions
ok_actions = var.ok_actions
insufficient_data_actions = var.insufficient_data_actions
}