-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_gateway.tf
53 lines (44 loc) · 1.9 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
resource "aws_api_gateway_rest_api" "api" {
name = var.api_name
tags = var.tags
}
resource "aws_api_gateway_resource" "resource" {
path_part = "{proxy+}"
parent_id = aws_api_gateway_rest_api.api.root_resource_id
rest_api_id = aws_api_gateway_rest_api.api.id
}
resource "aws_api_gateway_method" "method" {
count = length(var.http_methods)
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.resource.id
http_method = var.http_methods[count.index]
authorization = "NONE"
}
resource "aws_api_gateway_integration" "integration" {
count = length(var.http_methods)
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.resource.id
integration_http_method = "POST"
http_method = aws_api_gateway_method.method[count.index].http_method
type = "AWS_PROXY"
uri = aws_lambda_function.lambda.invoke_arn
}
resource "aws_lambda_permission" "apigw_lambda" {
count = length(var.http_methods)
statement_id = "AllowLambdaExecutionForAPIGateway-${var.api_name}-${var.http_methods[count.index]}"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda.function_name
principal = "apigateway.amazonaws.com"
source_arn = "arn:aws:execute-api:${var.region}:${local.account_id}:${aws_api_gateway_rest_api.api.id}/*/${aws_api_gateway_method.method[count.index].http_method}${aws_api_gateway_resource.resource.path}"
}
resource "aws_api_gateway_deployment" "deployment" {
depends_on = ["aws_api_gateway_integration.integration"]
rest_api_id = aws_api_gateway_rest_api.api.id
stage_name = ""
}
resource "aws_api_gateway_stage" "stage" {
stage_name = var.stage
rest_api_id = aws_api_gateway_rest_api.api.id
deployment_id = aws_api_gateway_deployment.deployment.id
tags = var.tags
}