generated from GSA/grace-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambda.tf
177 lines (162 loc) · 4.1 KB
/
lambda.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
resource "aws_sns_topic" "lambda" {
name = var.lambda_sns_topic_name
}
resource "aws_sns_topic_policy" "lambda" {
arn = aws_sns_topic.lambda.arn
policy = <<EOF
{
"Version": "2012-10-17",
"Id": "__default_policy_ID",
"Statement": [
{
"Sid": "__default_statement_ID",
"Effect": "Allow",
"Principal": "*",
"Action": [
"SNS:Publish"
],
"Resource": "${aws_sns_topic.lambda.arn}",
"Condition": {
"StringEquals": {
"AWS:SourceOwner": "${local.account_id}"
}
}
}
]
}
EOF
}
resource "aws_sns_topic_subscription" "lambda" {
topic_arn = aws_sns_topic.lambda.arn
protocol = "lambda"
endpoint = aws_lambda_function.lambda.arn
}
resource "aws_lambda_function" "lambda" {
filename = var.lambda_source_file
function_name = var.lambda_name
description = "A Lambda for converting events to SecurityHub Findings"
role = aws_iam_role.lambda.arn
handler = "grace-securityhub"
source_code_hash = filesha256(var.lambda_source_file)
kms_key_arn = aws_kms_key.lambda.arn
runtime = "go1.x"
timeout = 900
}
resource "aws_lambda_permission" "sns" {
statement_id = "AllowExecutionFromSNS"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda.function_name
principal = "sns.amazonaws.com"
source_arn = aws_sns_topic.lambda.arn
}
resource "aws_lambda_permission" "cloudwatch_logs" {
statement_id = "AllowInvocationFromCloudWatchLogs"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda.function_name
principal = "logs.${local.region}.amazonaws.com"
source_arn = "arn:aws:logs:${local.region}:${local.account_id}:*"
}
resource "aws_iam_role" "lambda" {
name = var.lambda_iam_role_name
description = "Role for GRACE SecurityHub Lambda function"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow"
}
]
}
EOF
}
resource "aws_iam_policy" "lambda" {
name = var.lambda_iam_policy_name
description = "Policy to allow creating new SecurityHub findings"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"securityhub:BatchImportFindings",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Resource": [
"${aws_iam_role.lambda.arn}"
]
},
{
"Effect": "Allow",
"Action": [
"kms:Encrypt"
],
"Resource": [
"${aws_kms_key.lambda.arn}"
]
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "iam_role_policy_attachment" {
role = aws_iam_role.lambda.name
policy_arn = aws_iam_policy.lambda.arn
}
resource "aws_kms_key" "lambda" {
description = "KMS Key for encrypting the lambda at rest"
deletion_window_in_days = 7
enable_key_rotation = "true"
depends_on = ["aws_iam_role.lambda"]
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::${local.account_id}:root"
},
"Action": "kms:*",
"Resource": "*"
},
{
"Sid": "Allow use of the key",
"Effect": "Allow",
"Principal": {
"AWS": [
"${aws_iam_role.lambda.arn}"
]
},
"Action": [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_kms_alias" "lambda" {
name = "alias/${var.lambda_kms_key_alias_prefix}-${local.account_id}" # Key Alias must be unique to account and region
target_key_id = aws_kms_key.lambda.key_id
}