-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.tf
229 lines (207 loc) · 5.94 KB
/
config.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
#Event rule to direct events to the Lambda Function
resource "aws_cloudwatch_event_rule" "event" {
name = "${var.name_prefix}-${var.unique_name}-rule${var.name_suffix}"
description = "Pattern of events to forward to targets"
event_pattern = <<PATTERN
{
"source": [
"aws.ec2"
],
"detail-type": [
"EBS Volume Notification"
],
"detail": {
"event": [
"deleteVolume",
"createVolume"
]
}
}
PATTERN
}
#Target to direct event at function
resource "aws_cloudwatch_event_target" "function_target" {
rule = aws_cloudwatch_event_rule.event.name
target_id = "${var.name_prefix}-${var.unique_name}-target${var.name_suffix}"
arn = aws_lambda_function.function.arn
}
#Permission to allow event trigger
resource "aws_lambda_permission" "allow_cloudwatch_event_trigger" {
statement_id = "TrustCWEToInvokeMyLambdaFunction"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.function.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.event.arn
}
#Automatic packaging of code
data "archive_file" "function_code" {
type = "zip"
source_dir = "${path.module}/function_code"
output_path = "${path.module}/function_code_zipped/function_code.zip"
}
#Function to process event
resource "aws_lambda_function" "function" {
filename = data.archive_file.function_code.output_path
source_code_hash = filebase64sha256(data.archive_file.function_code.output_path)
function_name = "${var.name_prefix}-${var.unique_name}-function${var.name_suffix}"
role = aws_iam_role.function_role.arn
handler = "main.handler"
runtime = "python3.9"
timeout = "10"
tracing_config {
mode = var.lambda_tracing_option
}
environment {
variables = {
sns_alarm_target = var.sns_alarm_target
alarm_threshold = var.alarm_threshold
alarm_period = var.alarm_period
}
}
lifecycle {
ignore_changes = [last_modified]
}
tags = local.common_tags
}
#Role to attach policy to Function
resource "aws_iam_role" "function_role" {
name = "${var.name_prefix}-${var.unique_name}-role${var.name_suffix}"
tags = local.common_tags
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
#Default policy for Lambda to be executed and put logs in Cloudwatch
resource "aws_iam_role_policy" "function_policy_default" {
name = "${var.name_prefix}-${var.unique_name}-policy-default${var.name_suffix}"
role = aws_iam_role.function_role.id
policy = jsonencode({
Version = "2012-10-17" #tfsec:ignore:aws-iam-no-policy-wildcards Ignoring because log streams names are randomly generated and can't match them
Statement = [
{
Sid = "AllowListCloudWatchLogGroups"
Action = [
"logs:DescribeLogStreams",
]
Effect = "Allow"
Resource = "${aws_cloudwatch_log_group.log_group.arn}:*"
},
]
Statement = [
{
Sid = "AllowCreatePutLogGroupsStreams"
Action = [
"logs:PutLogEvents",
"logs:CreateLogStream",
"logs:CreateLogGroup"
]
Effect = "Allow"
Resource = [
aws_cloudwatch_log_group.log_group.arn,
"${aws_cloudwatch_log_group.log_group.arn}:log-stream:*"
]
},
]
})
}
#Policy for additional Permissions for Lambda Execution
#tfsec:ignore:aws-iam-no-policy-wildcards --Ignores warning on usage of Wildcards as changing them will require a refactor of the policy. An issue has been raised for this.
resource "aws_iam_role_policy" "function_policy" {
name = "${var.name_prefix}-${var.unique_name}-policy${var.name_suffix}"
role = aws_iam_role.function_role.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "AllowCloudwatchActions"
Action = [
"cloudwatch:*",
]
Effect = "Allow"
Resource = "*"
},
]
Statement = [
{
Sid = "AllowEBSVolumeDescription"
Action = [
"ec2:DescribeVolumes",
]
Effect = "Allow"
Resource = "*" # The "DescribeVolumes" action MUST use a wildcard - it is not valid with a specific ARN
},
]
})
}
locals {
log_group_name = "/aws/lambda/${aws_lambda_function.function.function_name}"
}
#Cloudwatch Log Group for Function
resource "aws_cloudwatch_log_group" "log_group" {
name = local.log_group_name
retention_in_days = var.cloudwatch_log_retention_days
kms_key_id = aws_kms_key.log_key.arn
tags = local.common_tags
}
# CloudWatch Logs encryption key
resource "aws_kms_key" "log_key" {
description = "KMS key for encryption of cloudwatch logs generated by EBS burst balance alert Lambda"
enable_key_rotation = true
deletion_window_in_days = var.kms_log_key_deletion_window
policy = data.aws_iam_policy_document.cloudwatch_kms.json
}
data "aws_iam_policy_document" "cloudwatch_kms" {
statement {
actions = [
"kms:*",
]
principals {
identifiers = [
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:root",
]
type = "AWS"
}
resources = [
"*",
]
sid = "Enable IAM User Permissions"
}
statement {
actions = [
"kms:Encrypt*",
"kms:Decrypt*",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Describe*"
]
condition {
test = "ArnEquals"
values = [
"arn:aws:logs:*:${data.aws_caller_identity.current.account_id}:log-group:${local.log_group_name}",
]
variable = "kms:EncryptionContext:aws:logs:arn"
}
principals {
identifiers = [
"logs.amazonaws.com",
]
type = "Service"
}
resources = [
"*",
]
sid = "Allow cloudwatch to encrypt logs"
}
}