-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
405 lines (333 loc) · 11.6 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
data "aws_caller_identity" "current" {}
locals {
id = "${var.stage}-${var.project_name}"
}
###############################################################################
# VPC
###############################################################################
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.14.0"
name = "${local.id}-vpc"
# base CIDR address range
cidr = "10.10.0.0/16"
# Configure availability zones and subnets
# Three subnets are created:
# - private
# - public
# - database
azs = ["${var.aws_region}a", "${var.aws_region}b", "${var.aws_region}c"]
private_subnets = ["10.10.1.0/24", "10.10.2.0/24", "10.10.3.0/24"]
public_subnets = ["10.10.11.0/24", "10.10.12.0/24", "10.10.13.0/24"]
database_subnets = ["10.10.21.0/24", "10.10.22.0/24", "10.10.23.0/24"]
# enable DNS - this is required so that we can use private DNS
# hostnames when using a VPC Endpoint
enable_dns_hostnames = true
enable_dns_support = true
# database subnet group
database_subnet_group_name = "${local.id}-database-subnet-group"
create_database_subnet_group = true
create_database_subnet_route_table = true
# set Name tags so that the resources are easier
# to identify in the console
default_security_group_tags = {
Name = "${local.id}-default-sg"
}
public_subnet_tags = {
Name = "${local.id}-public-subnet"
}
public_route_table_tags = {
Name = "${local.id}-public-route-table"
}
private_subnet_tags = {
Name = "${local.id}-private-subnet"
}
private_route_table_tags = {
Name = "${local.id}-private-route-table"
}
database_subnet_tags = {
Name = "${local.id}-database-subnet"
}
database_route_table_tags = {
Name = "${local.id}-database-route-table"
}
igw_tags = {
Name = "${local.id}-internet-gateway"
}
vpc_tags = {
Name = "${local.id}-vpc"
}
}
resource "aws_ssm_parameter" "vpc_id" {
name = "/${var.stage}/${var.project_name}/vpc/id"
description = "The VPC ID"
type = "String"
value = module.vpc.vpc_id
}
resource "aws_ssm_parameter" "private_subnet_ids" {
name = "/${var.stage}/${var.project_name}/vpc/subnet/private/ids"
description = "The private subnet IDs"
type = "String"
value = join(",", module.vpc.private_subnets)
}
resource "aws_ssm_parameter" "database_subnet_ids" {
name = "/${var.stage}/${var.project_name}/vpc/subnet/database/ids"
description = "The database subnet IDs"
type = "String"
value = join(",", module.vpc.database_subnets)
}
resource "aws_ssm_parameter" "public_subnet_ids" {
name = "/${var.stage}/${var.project_name}/vpc/subnet/public/ids"
description = "The public subnet IDs"
type = "String"
value = join(",", module.vpc.public_subnets)
}
###############################################################################
# Security groups
###############################################################################
resource "aws_security_group" "secret_rotator_lambda_sg" {
name = "${local.id}-rotator-lambda-sg"
description = "Lambda Rotation Function SG"
vpc_id = module.vpc.vpc_id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "rds_sg" {
name = "${local.id}-rds-sg"
description = "RDS SG"
vpc_id = module.vpc.vpc_id
}
resource "aws_ssm_parameter" "db_security_group_id" {
name = "/${var.stage}/${var.project_name}/database/security_group_id"
description = "The RDS security group ID"
type = "String"
value = aws_security_group.rds_sg.id
}
resource "aws_security_group_rule" "rds_rotator_lambda_rule" {
type = "ingress"
from_port = 3306
to_port = 3306
protocol = "tcp"
security_group_id = aws_security_group.rds_sg.id
source_security_group_id = aws_security_group.secret_rotator_lambda_sg.id
}
resource "aws_security_group_rule" "rds_rotator_egress_rule" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [module.vpc.vpc_cidr_block]
security_group_id = aws_security_group.rds_sg.id
}
resource "aws_security_group" "secrets_manager_endpoint_sg" {
name = "${local.id}-sm-endpoint-sg"
description = "Secrets Manager VPC endpoint SG"
vpc_id = module.vpc.vpc_id
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
###############################################################################
# RDS cluster
###############################################################################
resource "random_password" "initial_master_password" {
length = 12
special = false
}
resource "aws_rds_cluster" "this" {
cluster_identifier = local.id
availability_zones = module.vpc.azs
db_subnet_group_name = module.vpc.database_subnet_group_name
vpc_security_group_ids = [aws_security_group.rds_sg.id]
engine = "aurora-mysql"
engine_version = var.database_engine_version
database_name = var.database_name
master_username = var.database_master_username
master_password = random_password.initial_master_password.result
iam_database_authentication_enabled = true
skip_final_snapshot = true
}
resource "aws_rds_cluster_instance" "this" {
count = 1
identifier = "${local.id}-instance-${count.index + 1}"
cluster_identifier = aws_rds_cluster.this.id
db_subnet_group_name = aws_rds_cluster.this.db_subnet_group_name
instance_class = var.database_instance_type
engine = aws_rds_cluster.this.engine
engine_version = aws_rds_cluster.this.engine_version
}
###############################################################################
# Secrets Manager VPC endpoint
###############################################################################
module "vpc_endpoints" {
source = "terraform-aws-modules/vpc/aws//modules/vpc-endpoints"
version = "3.14.0"
vpc_id = module.vpc.vpc_id
security_group_ids = [aws_security_group.secrets_manager_endpoint_sg.id]
# Endpoints are used to allow a particular subnet to be connected to AWS services
# without having to go via the internet. All traffic is routed internally to AWS.
endpoints = {
# The Secrets Manager endpoint allows Lambda functions to call the secrets manager
# API for the purpose of RDS key rotation from within the private subnet where the
# Lambda function resides.
secrets_manager = {
service = "secretsmanager"
subnet_ids = module.vpc.database_subnets
tags = { Name = "${local.id}-endpoint" }
// enable private DNS to ensure the VPC endpoint is exposed
// using the standard service Uri
private_dns_enabled = true
}
}
}
###############################################################################
# Lambda rotator function and role
###############################################################################
resource "aws_lambda_function" "secret_rotator_lambda" {
function_name = "${local.id}-rotator-lambda"
role = aws_iam_role.secret_rotator_lambda_role.arn
handler = "handler.lambda_handler"
filename = "./function/function.zip"
source_code_hash = filebase64sha256("./function/function.zip")
runtime = "python3.9"
timeout = 30
environment {
variables = {
SECRETS_MANAGER_ENDPOINT = "https://secretsmanager.${var.aws_region}.amazonaws.com",
API_CONNECT_TIMEOUT = 2,
API_READ_TIMEOUT = 2,
API_RETRIES = 10,
LOG_LEVEL = "DEBUG"
}
}
vpc_config {
security_group_ids = [aws_security_group.secret_rotator_lambda_sg.id]
subnet_ids = module.vpc.database_subnets
}
depends_on = [
aws_iam_role_policy_attachment.secret_rotator_lambda_role_policy_attachment
]
}
resource "aws_lambda_permission" "allow_secret_manager_call_lambda" {
function_name = aws_lambda_function.secret_rotator_lambda.function_name
statement_id = "AllowExecutionSecretManager"
action = "lambda:InvokeFunction"
principal = "secretsmanager.amazonaws.com"
source_account = data.aws_caller_identity.current.account_id
}
resource "aws_iam_role" "secret_rotator_lambda_role" {
name = "${local.id}-rotator-lambda-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "lambda.amazonaws.com"
}
},
]
})
}
resource "aws_iam_policy" "secret_rotator_lambda_policy" {
name = "${local.id}-rotator-lambda-policy"
policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Effect = "Allow",
Action = [
"secretsmanager:DescribeSecret",
"secretsmanager:GetSecretValue",
"secretsmanager:PutSecretValue",
"secretsmanager:UpdateSecretVersionStage"
],
Resource = aws_secretsmanager_secret.db_master_password.arn,
},
{
Effect = "Allow",
Action = [
"secretsmanager:GetRandomPassword"
],
Resource = "*"
},
{
Action = [
"ec2:CreateNetworkInterface",
"ec2:DescribeNetworkInterfaces",
"ec2:DeleteNetworkInterface",
"ec2:AssignPrivateIpAddresses",
"ec2:UnassignPrivateIpAddresses"
],
Resource = "*",
Effect = "Allow"
},
{
Effect = "Allow",
Action = "logs:CreateLogGroup",
Resource = "arn:aws:logs:${var.aws_region}:${data.aws_caller_identity.current.account_id}:*"
},
{
Effect = "Allow",
Action = [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
Resource = [
"arn:aws:logs:${var.aws_region}:${data.aws_caller_identity.current.account_id}:log-group:/aws/lambda/${local.id}-rotator-lambda:*"
]
}
]
})
}
resource "aws_iam_role_policy_attachment" "secret_rotator_lambda_role_policy_attachment" {
role = aws_iam_role.secret_rotator_lambda_role.id
policy_arn = aws_iam_policy.secret_rotator_lambda_policy.arn
}
###############################################################################
# Store secret in Secrets Manager and configure rotation
###############################################################################
resource "aws_secretsmanager_secret" "db_master_password" {
name = "/${var.stage}/${var.project_name}/database/secret"
description = "RDS master database secret"
}
resource "aws_secretsmanager_secret_version" "db_master_password" {
secret_id = aws_secretsmanager_secret.db_master_password.id
secret_string = jsonencode(
{
username = aws_rds_cluster.this.master_username
password = aws_rds_cluster.this.master_password
engine = "mysql"
host = aws_rds_cluster.this.endpoint
dbClusterIdentifier = aws_rds_cluster.this.id
}
)
}
resource "aws_ssm_parameter" "db_secret" {
name = "/${var.stage}/${var.project_name}/database/secret"
description = "The secrets manager secret ARN for RDS secret"
type = "String"
value = aws_secretsmanager_secret.db_master_password.arn
}
resource "aws_secretsmanager_secret_rotation" "db_master_password_rotation" {
secret_id = aws_secretsmanager_secret.db_master_password.id
rotation_lambda_arn = aws_lambda_function.secret_rotator_lambda.arn
rotation_rules {
automatically_after_days = var.rotation_interval
}
}