-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunction.tf
112 lines (93 loc) · 2.78 KB
/
function.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
data "aws_region" "current" {}
data "aws_iam_role" "screencap_role" {
count = var.lambda_role == "" ? 0 : 1
name = var.lambda_role
}
resource "aws_iam_role" "screencap_role" {
count = var.lambda_role == "" ? 1 : 0
name = var.name
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy" "log_perms" {
count = var.lambda_role == "" ? 1 : 0
name = "log_perms"
role = aws_iam_role.screencap_role[0].id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}
EOF
}
locals {
source_path = "${path.module}/lambda-code"
}
resource "null_resource" "zip" {
triggers = {
index_js = filebase64sha256("${local.source_path}/src/index.js")
package_json = filebase64sha256("${local.source_path}/package.json")
package_lock = filebase64sha256("${local.source_path}/package-lock.json")
}
provisioner "local-exec" {
command = "cd ${local.source_path} && npm install --production"
}
}
# The native archive module can't handle directory symlinks
data "external" "package_zip" {
program = ["${path.module}/zip.sh", "${path.module}/${var.name}-${null_resource.zip.id}.zip", local.source_path]
}
resource "aws_lambda_function" "lambda" {
filename = "${path.module}/${var.name}-${null_resource.zip.id}.zip"
function_name = var.name
handler = "src/index.handler"
role = var.lambda_role == "" ? aws_iam_role.screencap_role[0].arn : data.aws_iam_role.screencap_role[0].arn
runtime = "nodejs18.x"
timeout = 60
memory_size = 3008
source_code_hash = data.external.package_zip.result.hash
# This has sparticuz/chromium 117; bump to appropriate latest version when upgrading
# https://github.com/shelfio/chrome-aws-lambda-layer
layers = ["arn:aws:lambda:${data.aws_region.current.name}:764866452798:layer:chrome-aws-lambda:43"]
environment {
variables = var.env
}
tags = var.tags
}
resource "aws_lambda_permission" "gateway" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda.arn
principal = "apigateway.amazonaws.com"
# The /*/* portion grants access from any method on any resource
# within the API Gateway "REST API".
source_arn = "${aws_api_gateway_rest_api.api.execution_arn}/*/*"
}
resource "aws_cloudwatch_log_group" "screencap" {
name = "/aws/lambda/${var.name}"
retention_in_days = 30
tags = var.tags
}