generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.tf
166 lines (139 loc) · 4.96 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
163
164
165
166
/*
* # Sentinel forwarder
*
* This module sets up a lambda that will forward AWS logs to Azure Sentinel.
* It is a light wrapper on the code found here (https://github.com/cds-snc/aws-sentinel-connector-layer) and
* just stitches together the code with the triggers.
*
* Triggers can be EventHub rules, S3 ObjectCreated events, or CloudWatch Log Subscriptions. The following log types are supported:
* - CloudTrail (.json.gz)
* - Load balancer (.log.gz)
* - VPC flow logs (.log.gz)
* - WAF ACL (.gz)
* - GuardDuty
* - SecurityHub (via EventHub)
* - Generic application json logs
*
* You will need to add your Log Analytics Workspace Customer ID and Shared Key. AWS logs are automatically assigned a LogType.
* Custom application logs are given the log type defined through the `var.log_type`. They also need to be nested inside a json
* object with the key, `application_log`. ex: `{'application_log': {'foo': 'bar'}}` for the layer code to forward it to Azure Sentinel.
*/
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.46.0"
}
}
}
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
resource "aws_lambda_function" "sentinel_forwarder" {
function_name = var.function_name
description = "Lambda function to forward AWS logs to Azure Sentinel"
filename = data.archive_file.sentinel_forwarder.output_path
handler = "sentinel_forwarder.lambda_handler"
runtime = "python3.9"
timeout = 30
memory_size = 128
role = aws_iam_role.sentinel_forwarder_lambda.arn
source_code_hash = filebase64sha256(data.archive_file.sentinel_forwarder.output_path)
environment {
variables = {
LOG_TYPE = var.log_type
SENTINEL_AUTH_PARAMS_ARN = aws_ssm_parameter.sentinel_forwarder_auth.arn
}
}
tracing_config {
mode = "Active"
}
layers = [var.layer_arn]
depends_on = [
aws_iam_role_policy_attachment.sentinel_forwarder_lambda,
aws_cloudwatch_log_group.sentinel_forwarder_lambda,
]
tags = {
(var.billing_tag_key) = var.billing_tag_value
}
}
data "archive_file" "sentinel_forwarder" {
type = "zip"
source_file = "${path.module}/wrapper/sentinel_forwarder.py"
output_path = "/tmp/sentinel_forwarder.py.zip"
}
#
# CloudWatch Log Subscriptions
#
resource "aws_lambda_permission" "sentinel_forwarder_cloudwatch_log_subscription" {
count = length(var.cloudwatch_log_arns)
statement_id = "AllowExecutionFromCloudWatchLogs-${var.function_name}-${count.index}"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.sentinel_forwarder.function_name
principal = "logs.${data.aws_region.current.name}.amazonaws.com"
source_arn = format("%s:*", var.cloudwatch_log_arns[count.index])
}
#
# Event triggers
#
resource "aws_lambda_permission" "sentinel_forwarder_events" {
count = length(var.event_rule_names)
statement_id = "AllowExecutionFromEvents-${var.function_name}-${count.index}"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.sentinel_forwarder.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_target.sentinel_forwarder[count.index].arn
source_account = data.aws_caller_identity.current.account_id
}
resource "aws_cloudwatch_event_target" "sentinel_forwarder" {
count = length(var.event_rule_names)
target_id = "SentinelForwarderEventTarget-${count.index}"
rule = var.event_rule_names[count.index]
arn = aws_lambda_function.sentinel_forwarder.arn
}
#
# S3 triggers
#
resource "aws_lambda_permission" "sentinel_forwarder_s3_triggers" {
count = length(var.s3_sources)
statement_id = "AllowExecutionFromS3-${var.function_name}-${count.index}"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.sentinel_forwarder.function_name
principal = "s3.amazonaws.com"
source_arn = var.s3_sources[count.index].bucket_arn
source_account = data.aws_caller_identity.current.account_id
}
resource "aws_s3_bucket_notification" "sentinel_forwarder_trigger_notification" {
count = length(var.s3_sources)
bucket = var.s3_sources[count.index].bucket_id
lambda_function {
lambda_function_arn = aws_lambda_function.sentinel_forwarder.arn
events = ["s3:ObjectCreated:*"]
filter_prefix = var.s3_sources[count.index].filter_prefix
}
depends_on = [aws_lambda_permission.sentinel_forwarder_s3_triggers]
}
#
# CloudWatch: Lambda logs
#
resource "aws_cloudwatch_log_group" "sentinel_forwarder_lambda" {
name = "/aws/lambda/${var.function_name}"
retention_in_days = "14"
tags = {
(var.billing_tag_key) = var.billing_tag_value
}
}
#
# Lambda function secrets
#
resource "aws_ssm_parameter" "sentinel_forwarder_auth" {
name = "${var.function_name}-auth"
type = "SecureString"
value = chomp(<<-EOT
CUSTOMER_ID=${var.customer_id}
SHARED_KEY=${var.shared_key}
EOT
)
tags = {
(var.billing_tag_key) = var.billing_tag_value
}
}