-
Notifications
You must be signed in to change notification settings - Fork 0
/
resizer.tf
85 lines (63 loc) · 1.86 KB
/
resizer.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
data "aws_iam_policy_document" "resizer_assume" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
data "aws_iam_policy_document" "resizer_perm" {
statement {
effect = "Allow"
resources = [
"${aws_s3_bucket.images.arn}/*",
"${aws_s3_bucket.scaled_images.arn}/*"
]
actions = ["s3:PutObject", "s3:GetObject"]
}
}
resource "aws_iam_role" "resizer" {
name = "resizer"
assume_role_policy = data.aws_iam_policy_document.resizer_assume.json
inline_policy {
name = "resizer_perms"
policy = data.aws_iam_policy_document.resizer_perm.json
}
}
resource "aws_lambda_permission" "resizer_allow_bucket" {
statement_id = "AllowExecutionFromS3Bucket"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.resizer.arn
principal = "s3.amazonaws.com"
source_arn = aws_s3_bucket.images.arn
}
resource "aws_lambda_function" "resizer" {
function_name = "image_resizer"
role = aws_iam_role.resizer.arn
filename = data.archive_file.resizer_code.output_path
source_code_hash = data.archive_file.resizer_code.output_md5
handler = "index.handler"
runtime = "nodejs18.x"
memory_size = 3008
environment {
variables = {
"TARGET_BUCKET": aws_s3_bucket.scaled_images.id
}
}
}
resource "aws_s3_bucket_notification" "resize_notif" {
bucket = aws_s3_bucket.images.id
lambda_function {
lambda_function_arn = aws_lambda_function.resizer.arn
events = ["s3:ObjectCreated:*"]
}
depends_on = [aws_lambda_permission.resizer_allow_bucket]
}
data "archive_file" "resizer_code" {
type = "zip"
source_dir = "${path.module}/resizer/"
output_path = "${path.module}/resizer.zip"
}
// auto resizer. Resizing config?