-
Notifications
You must be signed in to change notification settings - Fork 2
/
lambda-dmr-initiator.tf
166 lines (151 loc) · 4.28 KB
/
lambda-dmr-initiator.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
variable dmr_initiator {
description = "Name for the DMR Initiator. This will be used to name the Lambda and IAM Role"
default = "dmr-initiator"
}
# Generate a placeholder VirusTotal API key to be updated manually
resource "aws_ssm_parameter" "virustotal_apikey" {
name = "/dmr/virustotal"
type = "SecureString"
key_id = CHANGEME
value = "PLACEHOLDER_REPLACEBYHAND"
overwrite = false
lifecycle {
ignore_changes = [value, key_id]
}
}
# Lookup value post creation
data "aws_ssm_parameter" "virustotal_apikey" {
name = "/dmr/virustotal"
depends_on = [aws_ssm_parameter.virustotal_apikey]
}
resource "aws_lambda_alias" "dmr_initiator" {
name = var.dmr_initiator
description = "Handle DMR requests"
function_name = aws_lambda_function.dmr_initiator.function_name
function_version = "$LATEST"
}
resource "aws_lambda_function" "dmr_initiator" {
filename = "functions/dmr-initiator.zip"
function_name = var.dmr_initiator
role = aws_iam_role.dmr_initiator.arn
handler = "dmr_initiator.handler"
runtime = "python3.7"
source_code_hash = filebase64sha256("functions/dmr-initiator.zip")
timeout = "900"
memory_size = "1024"
environment {
variables = {
LOG_LEVEL = var.lambda_loglevel
VT_API_KEY = data.aws_ssm_parameter.virustotal_apikey.value
S3_STAGING_BUCKET = aws_s3_bucket.dmr_staging_bucket.id
SEND_JIRA_COMMENT = var.create_jira
DMR_JIRA_ARN = var.create_jira ? aws_lambda_function.dmr_jira[0].arn : ""
}
}
}
resource "aws_lambda_event_source_mapping" "dmr_initiator" {
batch_size = 1
enabled = true
event_source_arn = aws_sqs_queue.dmr_queue.arn
function_name = aws_lambda_function.dmr_initiator.arn
depends_on = [
aws_lambda_function.dmr_initiator,
aws_iam_policy.iam_dmr_initiator,
aws_sqs_queue.dmr_queue
]
}
resource "aws_cloudwatch_log_group" "dmr_initiator" {
name = "/aws/lambda/${aws_lambda_function.dmr_initiator.function_name}"
retention_in_days = "14"
}
resource "aws_iam_role" "dmr_initiator" {
name = var.dmr_initiator
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service":
["lambda.amazonaws.com"]
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
tags = {
Name = var.dmr_initiator
}
}
resource "aws_iam_policy" "iam_dmr_initiator" {
name = "iam-${var.dmr_initiator}"
path = "/"
description = ""
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*",
"Effect": "Allow"
},
{
"Action": [
"sqs:ReceiveMessage",
"sqs:DeleteMessage",
"sqs:GetQueueAttributes"
],
"Resource": "*",
"Effect": "Allow"
},
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:PutObjectTagging"
],
"Resource": "arn:aws:s3:::${aws_s3_bucket.dmr_staging_bucket.id}/*"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "dmr_initiator" {
role = aws_iam_role.dmr_initiator.name
policy_arn = aws_iam_policy.iam_dmr_initiator.arn
}
resource "aws_iam_policy" "iam_dmr_initiator_invoke_jira" {
count = var.create_jira ? 1 : 0
name = "iam-${var.dmr_initiator}-invoke-dmr-jira"
path = "/"
description = ""
policy = <<EOF
{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"InvokeDMRJira",
"Effect":"Allow",
"Action":[
"lambda:InvokeFunction",
"lambda:InvokeAsync"
],
"Resource":"${aws_lambda_function.dmr_jira[0].arn}"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "dmr_initiator_invoke_jira" {
count = var.create_jira ? 1 : 0
role = aws_iam_role.dmr_initiator.name
policy_arn = aws_iam_policy.iam_dmr_initiator_invoke_jira[0].arn
}