-
Notifications
You must be signed in to change notification settings - Fork 1
/
rds.tf
275 lines (230 loc) · 7.53 KB
/
rds.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
data "aws_subnets" "filtered_subnets" {
filter {
name = "vpc-id"
values = [aws_vpc.main.id]
}
tags = {
Tier = var.rds_subnet_tier
AZ = var.rds_subnet_az
}
}
# Get general account password secret for SA password
data "aws_secretsmanager_secret" "password" {
arn = var.rds_sysadmin_user_password_secret_arn
}
data "aws_secretsmanager_secret_version" "password" {
secret_id = data.aws_secretsmanager_secret.password.id
}
################################################################################
# IAM Role for Windows Authentication
################################################################################
data "aws_iam_policy_document" "rds_assume_role" {
statement {
sid = "AssumeRole"
actions = [
"sts:AssumeRole",
]
principals {
type = "Service"
identifiers = ["rds.amazonaws.com"]
}
}
}
resource "aws_iam_role" "rds_ad_auth" {
name = "demo-rds-ad-auth"
description = "Role used by RDS for Active Directory authentication and authorization"
force_detach_policies = true
assume_role_policy = data.aws_iam_policy_document.rds_assume_role.json
tags = {
App = var.app_name
CUSTOMER = var.customer_tag
Env = var.environment_name
Terraform = true
}
#This stops any custom tags applied outside of Terraform from getting removed
lifecycle {
ignore_changes = [tags]
}
}
resource "aws_iam_role_policy_attachment" "rds_directory_services" {
role = aws_iam_role.rds_ad_auth.id
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonRDSDirectoryServiceAccess"
}
resource "aws_db_subnet_group" "mssql" {
description = "The ${var.app_name}-${var.environment_name} RDS ${var.rds_instance_name_prefix} instance ${var.rds_subnet_tier} subnet group."
subnet_ids = data.aws_subnets.filtered_subnets.ids
tags = {
App = var.app_name
CUSTOMER = var.customer_tag
Env = var.environment_name
Terraform = true
}
#This stops any custom tags applied outside of Terraform from getting removed
lifecycle {
ignore_changes = [tags]
}
}
resource "aws_security_group" "rds_mssql_security_group" {
name = "Internal-MSSQL-Traffic-To-RDS-${var.rds_instance_name_prefix}-${var.app_name}-${var.environment_name}"
description = "Allows all VPC traffic to RDS MSSQL default port in ${var.app_name}-${var.environment_name}"
vpc_id = aws_vpc.main.id
ingress {
from_port = 1433
to_port = 1433
protocol = "tcp"
cidr_blocks = [var.cidr_block]
}
# outbound internet access
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
App = var.app_name
CUSTOMER = var.customer_tag
Env = var.environment_name
Terraform = true
}
#This stops any custom tags applied outside of Terraform from getting removed
lifecycle {
ignore_changes = [tags]
}
}
data "aws_security_groups" "filtered_domain_controller_group" {
count = var.domain_join == true ? 1 : 0
filter {
name = "group-name"
values = ["${aws_directory_service_directory.ad[count.index].directory_service_id}_controllers"]
}
filter {
name = "vpc-id"
values = [aws_vpc.main.id]
}
}
resource "aws_db_parameter_group" "clr_enabled" {
name = lower("clr-enabled-parameters-${var.rds_instance_name_prefix}-${var.app_name}-${var.environment_name}")
family = var.mssql_engine_family
parameter {
name = "clr enabled"
value = "1"
}
}
#Create backup bucket
locals {
s3_rdsbackup_bucket = "${lower(var.app_name)}-rdsbackup-${var.environment_name}"
}
resource "aws_s3_bucket" "rdsbackup" {
bucket = local.s3_rdsbackup_bucket
tags = {
App = var.app_name
CUSTOMER = var.customer_tag
Env = var.environment_name
Terraform = true
}
#This stops any custom tags applied outside of Terraform from getting removed
lifecycle {
ignore_changes = [tags]
}
}
resource "aws_s3_bucket_acl" "rdsbackup_acl" {
bucket = aws_s3_bucket.rdsbackup.id
acl = "private"
}
resource "aws_iam_role" "iam_role_sql_backup_restore" {
name = "IAM_ROLE_SQL_BACKUP_RESTORE-${var.rds_instance_name_prefix}-${var.app_name}-${var.environment_name}"
path = "/"
description = "RDS SQL Server Native Backup Without Encryption "
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "rds.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
#Create IAM policy to allow access to bucket
resource "aws_iam_role_policy" "iam_role_sql_backup_s3_access" {
name = "IAM_POLICY_SQL_BACKUP_S3_ACCESS-${local.s3_rdsbackup_bucket}"
role = aws_iam_role.iam_role_sql_backup_restore.id
policy = jsonencode(
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:GetBucketLocation"
],
"Resource": [
"arn:aws:s3:::${local.s3_rdsbackup_bucket}"
]
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:ListMultipartUploadParts",
"s3:AbortMultipartUpload"
],
"Resource": [
"arn:aws:s3:::${local.s3_rdsbackup_bucket}/*"
]
}
]
}
)
}
resource "aws_db_option_group" "sqlexpress-native-backup-restore" {
name = lower("sqlexpress-native-backup-restore-${var.rds_instance_name_prefix}-${var.app_name}-${var.environment_name}")
option_group_description = "DB Option Group for backup and restore on ${var.mssql_engine_family}"
engine_name = var.mssql_engine
major_engine_version = var.engine_major_version
option {
option_name = "SQLSERVER_BACKUP_RESTORE"
option_settings {
name = "IAM_ROLE_ARN"
value = aws_iam_role.iam_role_sql_backup_restore.arn
}
}
}
locals {
domain_id = element(concat(aws_directory_service_directory.ad.*.id, tolist([""])), 0)
domain_iam_role_name = aws_iam_role.rds_ad_auth.name
}
resource "aws_db_instance" "mssql" {
identifier = lower("${var.rds_instance_name_prefix}-${var.app_name}-${var.environment_name}")
allocated_storage = var.rds_allocated_storage
license_model = "license-included"
storage_type = "gp2"
engine = var.mssql_engine
engine_version = var.engine_specific_version
instance_class = var.rds_instance_class
multi_az = var.rds_multi_az
username = var.mssql_admin_username
password = data.aws_secretsmanager_secret_version.password.secret_string
publicly_accessible = var.rds_public_accessible
vpc_security_group_ids = [aws_security_group.rds_mssql_security_group.id ]
db_subnet_group_name = aws_db_subnet_group.mssql.id
backup_retention_period = 3
skip_final_snapshot = var.rds_skip_final_snapshot
final_snapshot_identifier = "${var.rds_instance_name_prefix}-${var.app_name}-${var.environment_name}-final-snapshot"
parameter_group_name = aws_db_parameter_group.clr_enabled.name
option_group_name = aws_db_option_group.sqlexpress-native-backup-restore.name
domain = var.domain_join ? local.domain_id : ""
domain_iam_role_name = var.domain_join ? local.domain_iam_role_name : ""
tags = {
Env = var.environment_name
Terraform = true
}
}