-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_gateway.tf
58 lines (47 loc) · 1.81 KB
/
api_gateway.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
resource "aws_apigatewayv2_api" "lambda" {
name = "lambda_gateway_demo"
protocol_type = "HTTP"
}
resource "aws_apigatewayv2_stage" "demo_stage" {
api_id = aws_apigatewayv2_api.lambda.id
name = "demo"
auto_deploy = true
access_log_settings {
destination_arn = aws_cloudwatch_log_group.api_gateway.arn
format = jsonencode({
requestId = "$context.requestId"
sourceIp = "$context.identity.sourceIp"
requestTime = "$context.requestTime"
protocol = "$context.protocol"
httpMethod = "$context.httpMethod"
resourcePath = "$context.resourcePath"
routeKey = "$context.routeKey"
status = "$context.status"
responseLength = "$context.responseLength"
integrationErrorMessage = "$context.integrationErrorMessage"
}
)
}
}
resource "aws_apigatewayv2_integration" "lambda_gateway_integration" {
api_id = aws_apigatewayv2_api.lambda.id
integration_uri = aws_lambda_function.lambda_demo.invoke_arn
integration_type = "AWS_PROXY"
integration_method = "POST"
}
resource "aws_apigatewayv2_route" "lambda_gateway" {
api_id = aws_apigatewayv2_api.lambda.id
route_key = "ANY /"
target = "integrations/${aws_apigatewayv2_integration.lambda_gateway_integration.id}"
}
resource "aws_cloudwatch_log_group" "api_gateway" {
name = "/aws/api_gw/${aws_apigatewayv2_api.lambda.name}"
retention_in_days = 1
}
resource "aws_lambda_permission" "api_gateway" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda_demo.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_apigatewayv2_api.lambda.execution_arn}/*/*"
}