-
Notifications
You must be signed in to change notification settings - Fork 16
/
lambda.tf
117 lines (100 loc) · 2.76 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
locals {
lambda_function_name = "${var.global_prefix}-lambda"
lambda_parameters_file = "builds/ggcanary_lambda_parameters.json"
}
resource "local_sensitive_file" "notifier_parameters" {
content = jsonencode(local.ggcanary_lambda_parameters)
filename = local.lambda_parameters_file
file_permission = "0600"
}
module "lambda_function" {
source = "terraform-aws-modules/lambda/aws"
version = "3.2.0"
depends_on = [
local_sensitive_file.notifier_parameters
]
source_path = [
{
path = "lambda/entrypoint.py",
pip_requirements = "lambda/requirements.txt"
},
{
path = "lambda/lambda_py",
prefix_in_zip = "lambda_py",
},
{
path = "."
patterns = ["!.*", local.lambda_parameters_file]
}
]
function_name = local.lambda_function_name
handler = "entrypoint.lambda_handler"
runtime = "python3.8"
timeout = 90
publish = true
environment_variables = {
GGCANARY_USER_PREFIX = var.global_prefix,
}
allowed_triggers = {
AllowExecutionFromS3Bucket = {
service = "s3"
source_arn = aws_s3_bucket.ggcanary_bucket.arn
}
}
assume_role_policy_statements = {
assume_role = {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals = {
account_principals = {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
}
cloudwatch_logs_retention_in_days = 90
attach_cloudwatch_logs_policy = true
attach_policy_statements = true
policy_statements = {
get_object = {
effect = "Allow"
actions = ["s3:GetObject"]
resources = ["arn:aws:s3:::${aws_s3_bucket.ggcanary_bucket.bucket}/ggcanary/*"]
}
list_user_tags = {
effect = "Allow"
actions = [
"iam:ListUserTags",
]
resources = ["arn:aws:iam::${data.aws_caller_identity.current.id}:user/ggcanary/*"]
}
ses_send_email = {
effect = "Allow"
actions = [
"ses:SendEmail",
"ses:SendRawEmail"
]
resources = ["arn:aws:ses:*:${data.aws_caller_identity.current.id}:identity/*"]
}
}
}
resource "aws_s3_bucket_notification" "bucket_notification" {
bucket = aws_s3_bucket.ggcanary_bucket.id
lambda_function {
lambda_function_arn = module.lambda_function.lambda_function_arn
events = ["s3:ObjectCreated:*"]
filter_prefix = "ggcanary/AWSLogs/"
filter_suffix = ".json.gz"
}
depends_on = [module.lambda_function]
}
resource "null_resource" "remove_temp_file" {
provisioner "local-exec" {
command = "rm -f ${local.lambda_parameters_file}"
}
depends_on = [module.lambda_function]
triggers = {
always_run = timestamp()
}
}