-
Notifications
You must be signed in to change notification settings - Fork 4
/
lambda.tf
95 lines (86 loc) · 3.16 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
# IAM Policy for Simple Lambda Invokation
resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
# Lambda Function, the code is saved in the top level of a zip
# file calles lambda_function_payload
resource "aws_lambda_function" "wcalink_lambda" {
filename = "build/lambda_function_payload.zip"
function_name = "wcalink_prod"
role = aws_iam_role.iam_for_lambda.arn
handler = "lambda_function.lambda_handler"
# The filebase64sha256() function is available in Terraform 0.11.12 and later
source_code_hash = filebase64sha256("build/lambda_function_payload.zip")
runtime = "ruby3.2"
}
# The Proxy Ressource Makes it possible for a whole path to be handled by
# the same lambda
resource "aws_api_gateway_resource" "proxy" {
rest_api_id = aws_api_gateway_rest_api.wcalink_gateway.id
parent_id = aws_api_gateway_rest_api.wcalink_gateway.root_resource_id
path_part = "{proxy+}"
}
resource "aws_api_gateway_method" "proxy" {
rest_api_id = aws_api_gateway_rest_api.wcalink_gateway.id
resource_id = aws_api_gateway_resource.proxy.id
http_method = "ANY"
authorization = "NONE"
}
# Additional Route for the root
resource "aws_api_gateway_method" "proxy_root" {
rest_api_id = aws_api_gateway_rest_api.wcalink_gateway.id
resource_id = aws_api_gateway_rest_api.wcalink_gateway.root_resource_id
http_method = "ANY"
authorization = "NONE"
}
# Plugging the Lambda behind the ressources
resource "aws_api_gateway_integration" "lambda" {
rest_api_id = aws_api_gateway_rest_api.wcalink_gateway.id
resource_id = aws_api_gateway_method.proxy.resource_id
http_method = aws_api_gateway_method.proxy.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.wcalink_lambda.invoke_arn
}
resource "aws_api_gateway_integration" "lambda_root" {
rest_api_id = aws_api_gateway_rest_api.wcalink_gateway.id
resource_id = aws_api_gateway_method.proxy_root.resource_id
http_method = aws_api_gateway_method.proxy_root.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.wcalink_lambda.invoke_arn
}
# Deploying the API Gateway
resource "aws_api_gateway_deployment" "wcalink_prod_deployment" {
depends_on = [
aws_api_gateway_integration.lambda,
aws_api_gateway_integration.lambda_root,
]
rest_api_id = aws_api_gateway_rest_api.wcalink_gateway.id
stage_name = "prod"
}
# Give the API Gateway Permission to invoke the Lambda
resource "aws_lambda_permission" "apigw" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.wcalink_lambda.function_name
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.wcalink_gateway.execution_arn}/*/*"
}