-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthorizer.tf
73 lines (60 loc) · 1.96 KB
/
authorizer.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
resource "aws_api_gateway_authorizer" "authorizer" {
name = "lambda_authorizer"
rest_api_id = aws_api_gateway_rest_api.test.id
authorizer_uri = aws_lambda_function.authorizer.invoke_arn
authorizer_credentials = aws_iam_role.invocation_role.arn
}
data "aws_iam_policy_document" "invocation_assume_role" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["apigateway.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
resource "aws_iam_role" "invocation_role" {
name = "api_gateway_authorizer_invocation"
path = "/"
assume_role_policy = data.aws_iam_policy_document.invocation_assume_role.json
}
data "aws_iam_policy_document" "invocation_policy" {
statement {
effect = "Allow"
actions = ["lambda:InvokeFunction"]
resources = [aws_lambda_function.authorizer.arn]
}
}
resource "aws_iam_role_policy" "invocation_policy" {
name = "authorizer_invocation_policy"
role = aws_iam_role.invocation_role.id
policy = data.aws_iam_policy_document.invocation_policy.json
}
data "aws_iam_policy_document" "lambda_assume_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "lambda" {
name = "authorizer_lambda_role"
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role.json
}
data "archive_file" "authorizer_lambda" {
type = "zip"
source_file = "authorizer/index.js"
output_path = "lambda.zip"
}
resource "aws_lambda_function" "authorizer" {
filename = data.archive_file.authorizer_lambda.output_path
function_name = "api_gateway_authorizer"
role = aws_iam_role.lambda.arn
handler = "exports.handler"
source_code_hash = data.archive_file.authorizer_lambda.output_base64sha256
runtime = "nodejs18.x"
}