-
Notifications
You must be signed in to change notification settings - Fork 0
/
petstore-api.tf
169 lines (145 loc) · 5.01 KB
/
petstore-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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
resource "aws_api_gateway_rest_api" "api" {
name = var.petstore-api-name
description = "API para ${var.petstore-api-name}"
}
resource "aws_api_gateway_authorizer" "my_api_authorizer" {
name = "CognitoUserPoolAuthorizer"
type = "COGNITO_USER_POOLS"
rest_api_id = aws_api_gateway_rest_api.api.id
provider_arns = [module.tf-cgnt-api.cognito_user_pool_arn]
identity_source = "method.request.header.Authorization"
}
# Pets Resource -- GET
resource "aws_api_gateway_resource" "pets_res" {
rest_api_id = aws_api_gateway_rest_api.api.id
parent_id = aws_api_gateway_rest_api.api.root_resource_id
path_part = "pets"
}
resource "aws_api_gateway_method" "pets_meth" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.pets_res.id
http_method = "GET"
authorization = "COGNITO_USER_POOLS"
authorizer_id = aws_api_gateway_authorizer.my_api_authorizer.id
}
resource "aws_api_gateway_integration" "pets_int" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.pets_res.id
http_method = aws_api_gateway_method.pets_meth.http_method
integration_http_method = "GET"
type = "HTTP_PROXY"
uri = var.petstore-uri
request_parameters = {
"integration.request.header.Access-Control-Allow-Origin" = "'*'"
}
}
resource "aws_api_gateway_model" "pet_list_model" {
rest_api_id = aws_api_gateway_rest_api.api.id
name = "PetListModel"
description = "Model for list of pets"
content_type = "application/json"
schema = jsonencode({
type = "array",
items = {
type = "object",
properties = {
id = {
type = "integer"
},
type = {
type = "string"
},
price = {
type = "number"
}
},
required = ["id", "type", "price"]
}
})
}
resource "aws_api_gateway_method_response" "pets_get_200" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.pets_res.id
http_method = aws_api_gateway_method.pets_meth.http_method
status_code = "200"
response_models = {
"application/json" = aws_api_gateway_model.pet_list_model.name
}
response_parameters = {
"method.response.header.Access-Control-Allow-Origin" = true
}
}
# Pets Resource -- POST
resource "aws_api_gateway_method" "pets_meth_post" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.pets_res.id
http_method = "POST"
authorization = "COGNITO_USER_POOLS"
authorizer_id = aws_api_gateway_authorizer.my_api_authorizer.id
}
resource "aws_api_gateway_integration" "pets_int_post" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.pets_res.id
http_method = aws_api_gateway_method.pets_meth_post.http_method
integration_http_method = "POST"
type = "HTTP_PROXY"
uri = var.petstore-uri
}
# Pet by ID Resource -- GET
resource "aws_api_gateway_resource" "pets_id_res" {
rest_api_id = aws_api_gateway_rest_api.api.id
parent_id = aws_api_gateway_resource.pets_res.id
path_part = "{id}"
}
resource "aws_api_gateway_method" "pets_id_meth" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.pets_id_res.id
http_method = "GET"
authorization = "COGNITO_USER_POOLS"
authorizer_id = aws_api_gateway_authorizer.my_api_authorizer.id
request_parameters = {
"method.request.path.id" = true
}
}
resource "aws_api_gateway_integration" "pets_id_int" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.pets_id_res.id
http_method = aws_api_gateway_method.pets_id_meth.http_method
integration_http_method = "GET"
type = "HTTP_PROXY"
uri = "${var.petstore-uri}/{id}"
request_parameters = {
"integration.request.path.id" = "method.request.path.id"
}
}
resource "aws_api_gateway_model" "pet_model" {
rest_api_id = aws_api_gateway_rest_api.api.id
name = "PetModel"
description = "Model for pet response"
content_type = "application/json"
schema = jsonencode({
type = "object",
properties = {
type = { type = "string" },
price = { type = "number" }
}
})
}
resource "aws_api_gateway_method_response" "pets_id_get_200" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.pets_id_res.id
http_method = aws_api_gateway_method.pets_id_meth.http_method
status_code = "200"
response_models = {
"application/json" = aws_api_gateway_model.pet_model.name
}
}
resource "aws_api_gateway_deployment" "api_deployment" {
rest_api_id = aws_api_gateway_rest_api.api.id
stage_name = "v1"
depends_on = [
aws_api_gateway_integration.pets_int,
aws_api_gateway_integration.pets_id_int,
aws_api_gateway_integration.pets_int_post
]
}