-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.tf
114 lines (99 loc) · 3.98 KB
/
api.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
resource "aws_api_gateway_rest_api" "crc_api" {
name = "crc_api"
description = "API to get data from DynamoDB via Lambda"
endpoint_configuration {
types = ["REGIONAL"]
}
tags = local.tags
}
resource "aws_api_gateway_resource" "crc_api" {
parent_id = aws_api_gateway_rest_api.crc_api.root_resource_id
path_part = "crc_api"
rest_api_id = aws_api_gateway_rest_api.crc_api.id
}
# module "cors" {
# source = "squidfunk/api-gateway-enable-cors/aws"
# version = "0.3.3"
# api_id = aws_api_gateway_rest_api.crc_api.id
# api_resource_id = aws_api_gateway_resource.crc_api.id
# }
resource "aws_api_gateway_method" "crc_api" {
authorization = "NONE"
http_method = "GET"
resource_id = aws_api_gateway_resource.crc_api.id
rest_api_id = aws_api_gateway_rest_api.crc_api.id
}
resource "aws_api_gateway_integration" "crc_api" {
http_method = aws_api_gateway_method.crc_api.http_method
resource_id = aws_api_gateway_resource.crc_api.id
rest_api_id = aws_api_gateway_rest_api.crc_api.id
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.lambda_py.invoke_arn
}
resource "aws_api_gateway_method_response" "response_200" {
depends_on = [
aws_api_gateway_method.crc_api
]
rest_api_id = aws_api_gateway_rest_api.crc_api.id
resource_id = aws_api_gateway_resource.crc_api.id
http_method = aws_api_gateway_method.crc_api.http_method
status_code = "200"
response_parameters = {
"method.response.header.Access-Control-Allow-Origin" = true,
"method.response.header.Access-Control-Allow-Methods" = true,
"method.response.header.Access-Control-Allow-Headers" = true
}
response_models = {
"application/json" = "Empty"
}
}
resource "aws_api_gateway_integration_response" "cors" {
depends_on = [aws_api_gateway_integration.crc_api, aws_api_gateway_method_response.response_200]
rest_api_id = aws_api_gateway_rest_api.crc_api.id
resource_id = aws_api_gateway_resource.crc_api.id
http_method = aws_api_gateway_method.crc_api.http_method
status_code = 200
response_parameters = {
"method.response.header.Access-Control-Allow-Origin" = "'*'", # replace with hostname of frontend (CloudFront)
"method.response.header.Access-Control-Allow-Headers" = "'Content-Type'",
"method.response.header.Access-Control-Allow-Methods" = "'GET, POST'" # remove or add HTTP methods as needed
}
}
resource "aws_api_gateway_deployment" "crc_api" {
rest_api_id = aws_api_gateway_rest_api.crc_api.id
triggers = {
# NOTE: The configuration below will satisfy ordering considerations,
# but not pick up all future REST API changes. More advanced patterns
# are possible, such as using the filesha1() function against the
# Terraform configuration file(s) or removing the .id references to
# calculate a hash against whole resources. Be aware that using whole
# resources will show a difference after the initial implementation.
# It will stabilize to only change when resources change afterwards.
redeployment = sha1(jsonencode([
aws_api_gateway_resource.crc_api.id,
aws_api_gateway_method.crc_api.id,
aws_api_gateway_integration.crc_api.id,
]))
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_api_gateway_stage" "crc_api" {
deployment_id = aws_api_gateway_deployment.crc_api.id
rest_api_id = aws_api_gateway_rest_api.crc_api.id
stage_name = "crc_api"
}
resource "aws_lambda_permission" "apigw" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda_py.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.crc_api.execution_arn}/*/*"
}
# output "base_url" {
# value = aws_api_gateway_deployment.crc_api.invoke_url
# }