Repository navigation
Expand file tree
/
Copy pathapi-gateway.tf
More file actions
85 lines (73 loc) · 2.77 KB
/
Copy pathapi-gateway.tf
File metadata and controls
85 lines (73 loc) · 2.77 KB
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
resource "aws_api_gateway_rest_api" "lambda" {
name = "${var.name}-api"
description = "API for AWS Lambda"
tags = var.tags
endpoint_configuration {
types = ["REGIONAL"]
}
disable_execute_api_endpoint = true
}
resource "aws_api_gateway_method" "lambda" {
authorization = "NONE"
http_method = "GET"
resource_id = aws_api_gateway_rest_api.lambda.root_resource_id
rest_api_id = aws_api_gateway_rest_api.lambda.id
}
resource "aws_api_gateway_integration" "lambda" {
rest_api_id = aws_api_gateway_rest_api.lambda.id
resource_id = aws_api_gateway_rest_api.lambda.root_resource_id
http_method = aws_api_gateway_method.lambda.http_method
integration_http_method = "POST" # Lambda function can only be invoked via POST
type = "AWS"
uri = aws_lambda_function.this.invoke_arn
content_handling = "CONVERT_TO_TEXT"
}
resource "aws_api_gateway_method_response" "lambda" {
rest_api_id = aws_api_gateway_rest_api.lambda.id
resource_id = aws_api_gateway_rest_api.lambda.root_resource_id
http_method = aws_api_gateway_method.lambda.http_method
status_code = "200"
response_models = {
"application/json" = "Empty"
}
}
resource "aws_api_gateway_integration_response" "lambda" {
rest_api_id = aws_api_gateway_rest_api.lambda.id
resource_id = aws_api_gateway_rest_api.lambda.root_resource_id
http_method = aws_api_gateway_method.lambda.http_method
status_code = aws_api_gateway_method_response.lambda.status_code
depends_on = [aws_api_gateway_integration.lambda]
}
resource "aws_lambda_permission" "lambda" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.this.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.lambda.execution_arn}/*"
}
resource "aws_api_gateway_deployment" "lambda" {
rest_api_id = aws_api_gateway_rest_api.lambda.id
triggers = {
redeployment = sha1(jsonencode([
aws_api_gateway_method.lambda.id,
aws_api_gateway_integration.lambda.id,
aws_api_gateway_method_response.lambda,
aws_api_gateway_integration_response.lambda,
]))
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_api_gateway_stage" "lambda" {
deployment_id = aws_api_gateway_deployment.lambda.id
rest_api_id = aws_api_gateway_rest_api.lambda.id
stage_name = "run" # Any Name you wish
tags = var.tags
}
resource "aws_api_gateway_base_path_mapping" "lambda" {
domain_name = aws_api_gateway_domain_name.this.domain_name
api_id = aws_api_gateway_rest_api.lambda.id
stage_name = aws_api_gateway_stage.lambda.stage_name
base_path = null # null means root of api domain name
}