-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_api.tf
145 lines (122 loc) · 5.68 KB
/
rest_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
resource "aws_api_gateway_rest_api" "harsh-viradia-api" {
name = "harsh-viradia-wildrides-api"
}
resource "aws_api_gateway_resource" "harsh-viradia-api-resource" {
rest_api_id = aws_api_gateway_rest_api.harsh-viradia-api.id
parent_id = aws_api_gateway_rest_api.harsh-viradia-api.root_resource_id
path_part = "ride"
depends_on = [
aws_api_gateway_rest_api.harsh-viradia-api,
]
}
resource "aws_api_gateway_authorizer" "harsh-viradia-api-authorozer" {
rest_api_id = aws_api_gateway_rest_api.harsh-viradia-api.id
name = "CognitoUserPoolAuthorizer"
type = "COGNITO_USER_POOLS"
provider_arns = [aws_cognito_user_pool.harsh-viradia-user-pool.arn]
}
#-------------------------------------------------------------------- Option Method -----------------------------------------------------
resource "aws_api_gateway_method" "harsh-viradia-option-method" {
rest_api_id = aws_api_gateway_rest_api.harsh-viradia-api.id
resource_id = aws_api_gateway_resource.harsh-viradia-api-resource.id
http_method = "OPTIONS"
authorization = "NONE"
}
resource "aws_api_gateway_method_response" "harsh-viradia-option-response" {
rest_api_id = aws_api_gateway_rest_api.harsh-viradia-api.id
resource_id = aws_api_gateway_resource.harsh-viradia-api-resource.id
http_method = aws_api_gateway_method.harsh-viradia-option-method.http_method
status_code = "200"
response_models = {
"application/json" = "Empty"
}
response_parameters = {
"method.response.header.Access-Control-Allow-Headers" = true
"method.response.header.Access-Control-Allow-Methods" = true
"method.response.header.Access-Control-Allow-Origin" = true
}
}
resource "aws_api_gateway_integration" "harsh-viradia-option-integration" {
rest_api_id = aws_api_gateway_rest_api.harsh-viradia-api.id
resource_id = aws_api_gateway_resource.harsh-viradia-api-resource.id
http_method = aws_api_gateway_method.harsh-viradia-option-method.http_method
integration_http_method = "OPTIONS"
content_handling = "CONVERT_TO_TEXT"
type = "MOCK"
uri = aws_lambda_function.harsh-viradia-lambda-function.invoke_arn
request_templates = { "application/json" = "{\"statusCode\": 200}"}
}
resource "aws_api_gateway_integration_response" "harsh-viradia-option-integration-response" {
rest_api_id = aws_api_gateway_rest_api.harsh-viradia-api.id
resource_id = aws_api_gateway_resource.harsh-viradia-api-resource.id
http_method = aws_api_gateway_method.harsh-viradia-option-method.http_method
status_code = aws_api_gateway_method_response.harsh-viradia-option-response.status_code
response_parameters = {
"method.response.header.Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
"method.response.header.Access-Control-Allow-Methods" = "'OPTIONS,GET,POST,PUT'",
"method.response.header.Access-Control-Allow-Origin" = "'*'"
}
depends_on = [
aws_api_gateway_integration.harsh-viradia-integration
]
}
#-------------------------------------- POST ------------------------------
resource "aws_api_gateway_method" "harsh-viradia-post-method" {
rest_api_id = aws_api_gateway_rest_api.harsh-viradia-api.id
resource_id = aws_api_gateway_resource.harsh-viradia-api-resource.id
http_method = "ANY"
authorization = "COGNITO_USER_POOLS"
authorizer_id = aws_api_gateway_authorizer.harsh-viradia-api-authorozer.id
}
resource "aws_api_gateway_method_response" "harsh-viradia-post-response" {
rest_api_id = aws_api_gateway_rest_api.harsh-viradia-api.id
resource_id = aws_api_gateway_resource.harsh-viradia-api-resource.id
http_method = aws_api_gateway_method.harsh-viradia-post-method.http_method
status_code = "200"
response_models = {
"application/json" = "Empty"
}
}
resource "aws_api_gateway_integration" "harsh-viradia-integration" {
rest_api_id = aws_api_gateway_rest_api.harsh-viradia-api.id
resource_id = aws_api_gateway_resource.harsh-viradia-api-resource.id
http_method = aws_api_gateway_method.harsh-viradia-post-method.http_method
integration_http_method = "ANY"
type = "AWS_PROXY"
uri = aws_lambda_function.harsh-viradia-lambda-function.invoke_arn
}
resource "aws_api_gateway_integration_response" "harsh-viradia-post-integration" {
rest_api_id = aws_api_gateway_rest_api.harsh-viradia-api.id
resource_id = aws_api_gateway_resource.harsh-viradia-api-resource.id
http_method = aws_api_gateway_method.harsh-viradia-post-method.http_method
status_code = aws_api_gateway_method_response.harsh-viradia-post-response.status_code
response_templates = {"application/json" = ""}
depends_on = [
aws_api_gateway_integration.harsh-viradia-integration
]
}
#------------------------------------- Deploy ---------------------------
resource "aws_api_gateway_deployment" "harsh-viradia-deployment" {
rest_api_id = aws_api_gateway_rest_api.harsh-viradia-api.id
stage_name = "prod"
depends_on = [
aws_api_gateway_integration.harsh-viradia-integration
]
triggers = {
redeployment = sha1(jsonencode([
aws_api_gateway_resource.harsh-viradia-api-resource.id,
aws_api_gateway_method.harsh-viradia-post-method.id,
aws_api_gateway_integration.harsh-viradia-integration.id,
]))
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_lambda_permission" "apigw_lambda" {
statement_id = "AllowAPIGatewayInvocation"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.harsh-viradia-lambda-function.arn}"
principal = "apigateway.amazonaws.com"
source_arn = "arn:aws:execute-api:us-east-1:587172484624:${aws_api_gateway_rest_api.harsh-viradia-api.id}/*/*/ride"
}