forked from trussworks/terraform-aws-rds-snapshot-cleaner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
162 lines (126 loc) · 4.44 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
locals {
pkg = "milmove-aws-tools"
name = "rds-snapshot-cleaner"
}
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
#
# IAM
#
data "aws_iam_policy_document" "assume_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
# Allow creating and writing CloudWatch logs for Lambda function.
data "aws_iam_policy_document" "main" {
statement {
sid = "WriteCloudWatchLogs"
effect = "Allow"
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents",
]
resources = ["arn:${data.aws_partition.current.partition}:logs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:log-group:/aws/lambda/${local.name}-${var.cleaner_db_instance_identifier}:*"]
}
# Allow describing RDS snapshots for a particular RDS instance and RDS
# matching the name "$db_instance_identifier-*" (e.g., app-staging-12345676).
statement {
sid = "DescribeDBSnapshots"
effect = "Allow"
actions = [
"rds:DescribeDBSnapshots",
]
resources = [
"arn:${data.aws_partition.current.partition}:rds:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:db:${var.cleaner_db_instance_identifier}",
"arn:${data.aws_partition.current.partition}:rds:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:snapshot:${var.cleaner_db_instance_identifier}-*",
]
}
# Allow deleting RDS snapshots matching the name "$db_instance_identifier-*" (e.g., app-staging-12345676).
statement {
sid = "DeleteDBSnapshots"
effect = "Allow"
actions = [
"rds:DeleteDBSnapshot",
]
resources = [
"arn:${data.aws_partition.current.partition}:rds:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:snapshot:${var.cleaner_db_instance_identifier}-*",
]
}
}
resource "aws_iam_role" "main" {
name = "lambda-${local.name}-${var.cleaner_db_instance_identifier}"
assume_role_policy = data.aws_iam_policy_document.assume_role.json
}
resource "aws_iam_role_policy" "main" {
name = "lambda-${local.name}-${var.cleaner_db_instance_identifier}"
role = aws_iam_role.main.id
policy = data.aws_iam_policy_document.main.json
}
#
# CloudWatch Scheduled Event
#
resource "aws_cloudwatch_event_rule" "main" {
name = "${local.name}-${var.cleaner_db_instance_identifier}"
description = "scheduled trigger for ${local.name}"
schedule_expression = "rate(${var.interval_minutes} minutes)"
}
resource "aws_cloudwatch_event_target" "main" {
rule = aws_cloudwatch_event_rule.main.name
arn = aws_lambda_function.main.arn
}
#
# CloudWatch Logs
#
resource "aws_cloudwatch_log_group" "main" {
# This name must match the lambda function name and should not be changed
name = "/aws/lambda/${local.name}-${var.cleaner_db_instance_identifier}"
retention_in_days = var.cloudwatch_logs_retention_days
kms_key_id = var.cloudwatch_kms_key_arn
tags = {
Name = "${local.name}-${var.cleaner_db_instance_identifier}"
Environment = var.environment
}
}
#
# Lambda Function
#
resource "aws_lambda_permission" "main" {
statement_id = "${local.name}-${var.cleaner_db_instance_identifier}"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.main.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.main.arn
}
resource "aws_lambda_function" "main" {
depends_on = [aws_cloudwatch_log_group.main]
s3_bucket = var.s3_bucket
s3_key = "${local.pkg}/${var.version_to_deploy}/${local.pkg}.zip"
function_name = "${local.name}-${var.cleaner_db_instance_identifier}"
role = aws_iam_role.main.arn
handler = local.name
runtime = "go1.x"
memory_size = "128"
timeout = "60"
# Default AWS managed key for lambda functions
kms_key_arn = var.kms_key_arn
environment {
variables = {
DB_INSTANCE_IDENTIFIER = var.cleaner_db_instance_identifier
DRY_RUN = var.cleaner_dry_run
LAMBDA = "true"
MAX_DB_SNAPSHOT_COUNT = var.cleaner_max_db_snapshot_count
RETENTION_DAYS = var.cleaner_retention_days
}
}
tags = {
Name = "${local.name}-${var.cleaner_db_instance_identifier}"
Environment = var.environment
}
}