-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.tf
189 lines (143 loc) · 5.24 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
178
179
180
181
182
183
184
185
186
187
188
189
# defining lambda-role -------------
data "aws_iam_policy_document" "lambda_edge_role_assume_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com", "edgelambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "lambda_iam_assume_role" {
name = "lambda-role"
assume_role_policy = data.aws_iam_policy_document.lambda_edge_role_assume_policy.json
}
# attaching policy to the role ----------------
# general lambda policy - gives access to ssm and logs
data "template_file" "lambda_general_policy_file" {
template = file("./templates/iam/lambda-general-policy.json.tpl")
}
resource "aws_iam_policy" "lambda_general_policy" {
name = "General-lambda-policy"
description = "Provides full access of ssm and logs"
policy = data.template_file.lambda_general_policy_file.rendered
}
# temp s3 lambda policy - gives full access to the temp s3 folder
data "template_file" "lambda_temp_s3_file" {
template = file("./templates/iam/lambda-temp-s3-policy.json.tpl")
}
resource "aws_iam_policy" "lambda_temp_s3_policy" {
name = "Temp-S3-lambda-policy"
description = "Provides full access of temp s3 bucket"
policy = data.template_file.lambda_temp_s3_file.rendered
}
# attaching policies to the role
resource "aws_iam_role_policy_attachment" "lambda_general_policy_attachment" {
role = aws_iam_role.lambda_iam_assume_role.name
policy_arn = aws_iam_policy.lambda_general_policy.arn
}
resource "aws_iam_role_policy_attachment" "lambda_general_s3_temp_attachment" {
role = aws_iam_role.lambda_iam_assume_role.name
policy_arn = aws_iam_policy.lambda_temp_s3_policy.arn
}
# defining layer variables -------------------------------------
# NOTE: some dude has maintained these layers in different regions for different python versions - https://api.klayers.cloud/api/v2/p3.10/layers/latest/ap-south-1/html
# TODO: self-host the layers
variable "pillow_layer_arn" {
default = "arn:aws:lambda:ap-south-1:770693421928:layer:Klayers-p310-Pillow:7"
}
variable "requests_layer_arn" {
default = "arn:aws:lambda:ap-south-1:770693421928:layer:Klayers-p310-requests:10"
}
# defining lambda functions ---------------------------------------
resource "aws_lambda_function" "cleanup_cron" {
filename = "./lambda/lambda_cleanup_cron.zip"
function_name = "lambda_cleanup_cron"
role = aws_iam_role.lambda_iam_assume_role.arn
handler = "lambda_handler.lambda_handler"
publish = true
source_code_hash = filebase64sha256("./lambda/lambda_cleanup_cron.zip")
runtime = "python3.10"
memory_size = 128
timeout = 100
depends_on = [
aws_iam_role_policy_attachment.lambda_general_policy_attachment,
aws_iam_role_policy_attachment.lambda_general_s3_temp_attachment
]
}
resource "aws_lambda_function" "generate_collage" {
filename = "./lambda/generate_collage.zip"
function_name = "generate_collage"
role = aws_iam_role.lambda_iam_assume_role.arn
handler = "lambda_handler.generate_collage"
publish = true
source_code_hash = filebase64sha256("./lambda/generate_collage.zip")
runtime = "python3.10"
memory_size = 256
timeout = 100
layers = [
var.pillow_layer_arn,
var.requests_layer_arn
]
depends_on = [
aws_iam_role_policy_attachment.lambda_general_policy_attachment,
aws_iam_role_policy_attachment.lambda_general_s3_temp_attachment
]
}
resource "aws_lambda_function" "generate_zip" {
filename = "./lambda/generate_zip.zip"
function_name = "generate_zip"
role = aws_iam_role.lambda_iam_assume_role.arn
handler = "lambda_handler.generate_zip"
publish = true
source_code_hash = filebase64sha256("./lambda/generate_zip.zip")
runtime = "python3.10"
memory_size = 1024
timeout = 100
layers = [
var.pillow_layer_arn,
var.requests_layer_arn
]
depends_on = [
aws_iam_role_policy_attachment.lambda_general_policy_attachment,
aws_iam_role_policy_attachment.lambda_general_s3_temp_attachment
]
}
resource "aws_lambda_function" "refresh_url" {
filename = "./lambda/refresh_url.zip"
function_name = "refresh_url"
role = aws_iam_role.lambda_iam_assume_role.arn
handler = "lambda_handler.refresh_url"
publish = true
source_code_hash = filebase64sha256("./lambda/refresh_url.zip")
runtime = "python3.10"
memory_size = 1024
timeout = 100
layers = [
var.pillow_layer_arn,
var.requests_layer_arn
]
depends_on = [
aws_iam_role_policy_attachment.lambda_general_policy_attachment,
aws_iam_role_policy_attachment.lambda_general_s3_temp_attachment
]
}
resource "aws_lambda_function" "file_zip" {
filename = "./lambda/any_file_zip.zip"
function_name = "file_zip"
role = aws_iam_role.lambda_iam_assume_role.arn
handler = "lambda_handler.generate_zip"
publish = true
source_code_hash = filebase64sha256("./lambda/any_file_zip.zip")
runtime = "python3.10"
memory_size = 1024
timeout = 100
layers = [
var.pillow_layer_arn,
var.requests_layer_arn
]
depends_on = [
aws_iam_role_policy_attachment.lambda_general_policy_attachment,
aws_iam_role_policy_attachment.lambda_general_s3_temp_attachment
]
}