forked from trussworks/terraform-aws-s3-anti-virus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anti-virus-update.tf
160 lines (125 loc) · 3.68 KB
/
anti-virus-update.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
#
# Lambda Function: Anti-Virus Definitions
#
#
# IAM
#
data "aws_iam_policy_document" "assume_role_update" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "main_update" {
# Allow creating and writing CloudWatch logs for Lambda function.
statement {
sid = "WriteCloudWatchLogs"
effect = "Allow"
actions = [
"logs:CreateLogGroup",
"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/${var.name_update}:*"]
}
statement {
sid = "s3GetAndPutWithTagging"
effect = "Allow"
actions = [
"s3:GetObject",
"s3:GetObjectTagging",
"s3:PutObject",
"s3:PutObjectTagging",
"s3:PutObjectVersionTagging",
]
resources = ["arn:${data.aws_partition.current.partition}:s3:::${var.av_definition_s3_bucket}/${var.av_definition_s3_prefix}/*"]
}
statement {
sid = "s3HeadObject"
effect = "Allow"
actions = [
"s3:ListBucket",
]
resources = [
"arn:${data.aws_partition.current.partition}:s3:::${var.av_definition_s3_bucket}",
"arn:${data.aws_partition.current.partition}:s3:::${var.av_definition_s3_bucket}/*",
]
}
}
resource "aws_iam_role" "main_update" {
name = "lambda-${var.name_update}"
assume_role_policy = data.aws_iam_policy_document.assume_role_update.json
permissions_boundary = var.permissions_boundary
tags = var.tags
}
resource "aws_iam_role_policy" "main_update" {
name = "lambda-${var.name_update}"
role = aws_iam_role.main_update.id
policy = data.aws_iam_policy_document.main_update.json
}
#
# CloudWatch Scheduled Event
#
resource "aws_cloudwatch_event_rule" "main_update" {
name = var.name_update
description = "scheduled trigger for ${var.name_update}"
schedule_expression = "rate(${var.av_update_minutes} minutes)"
tags = var.tags
}
resource "aws_cloudwatch_event_target" "main_update" {
rule = aws_cloudwatch_event_rule.main_update.name
arn = aws_lambda_function.main_update.arn
}
#
# CloudWatch Logs
#
resource "aws_cloudwatch_log_group" "main_update" {
# This name must match the lambda function name and should not be changed
name = "/aws/lambda/${var.name_update}"
retention_in_days = var.cloudwatch_logs_retention_days
kms_key_id = var.cloudwatch_kms_arn
tags = merge(
{
"Name" = var.name_update
},
var.tags
)
}
#
# Lambda Function
#
resource "aws_lambda_function" "main_update" {
depends_on = [aws_cloudwatch_log_group.main_update]
description = "Updates clamav definitions stored in s3."
s3_bucket = var.lambda_s3_bucket
s3_key = local.lambda_package_key
function_name = var.name_update
role = aws_iam_role.main_update.arn
handler = "update.lambda_handler"
runtime = "python3.12"
memory_size = var.memory_size
timeout = var.timeout_seconds
environment {
variables = {
AV_DEFINITION_S3_BUCKET = var.av_definition_s3_bucket
AV_DEFINITION_S3_PREFIX = var.av_definition_s3_prefix
}
}
tags = merge(
{
"Name" = var.name_update
},
var.tags
)
}
resource "aws_lambda_permission" "main_update" {
statement_id = var.name_update
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.main_update.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.main_update.arn
}