-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.tf
70 lines (61 loc) · 1.77 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
###
# IAM Roles
#
# We have to create and use an IAM role that can be assumed by the two service principals -
# lambda.amazonaws.com and edgelambda.amazonaws.com.
# See: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-edge-permissions.html
resource "aws_iam_role" "lambda" {
name_prefix = "basicAuth-"
assume_role_policy = data.aws_iam_policy_document.assume_role.json
inline_policy {
name = "CloudWatchLogsAccess"
policy = data.aws_iam_policy_document.lambda.json
}
}
###
# IAM Role Policies
#
data "aws_iam_policy_document" "assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com", "edgelambda.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "lambda" {
statement {
effect = "Allow"
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = [
"arn:aws:logs:*:*:*",
]
}
}
###
# Lambda functions
#
data "archive_file" "basic_auth_function" {
type = "zip"
output_path = "${path.module}/functions/basic-auth.zip"
source {
content = templatefile("${path.module}/functions/basic-auth.js", var.basic_auth_credentials)
filename = "basic-auth.js"
}
}
resource "aws_lambda_function" "basic_auth" {
count = var.create ? 1 : 0
filename = "${path.module}/functions/basic-auth.zip"
function_name = var.function_name
role = aws_iam_role.lambda.arn
handler = "basic-auth.handler"
source_code_hash = data.archive_file.basic_auth_function.output_base64sha256
runtime = "nodejs16.x"
description = "Protect CloudFront distributions with Basic Authentication"
publish = true
}