-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_gateway.tf
208 lines (194 loc) · 7.39 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Create an API Gateway with integrations to SQS to act as PPS Builder API
*/
resource "aws_api_gateway_rest_api" "example" {
name = "API Skeleton - ${local.suffix}"
description = "Skeleton for building an API on AWS with predefined roles and able to use a VPC."
endpoint_configuration {
types = ["PRIVATE"]
}
# set the policy to only allow access via VPC Endpoint
policy = templatefile("${path.module}/policies/api_gateway.json.tmpl",
{
region=data.aws_region.current.name,
account=data.aws_caller_identity.current.account_id,
vpce=var.vpce
})
}
/*
* paths
*/
# /host
resource "aws_api_gateway_resource" "host" {
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
parent_id = "${aws_api_gateway_rest_api.example.root_resource_id}"
path_part = "host"
}
# /host/request
resource "aws_api_gateway_resource" "hostRequest" {
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
parent_id = "${aws_api_gateway_resource.host.id}"
path_part = "request"
}
# /host/status
resource "aws_api_gateway_resource" "hostStatus" {
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
parent_id = "${aws_api_gateway_resource.host.id}"
path_part = "status"
}
/*
* POST /host/request
*/
resource "aws_api_gateway_model" "host_config_request_model" {
// a model to validate body of POST to /host/request
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
name = "hostConfigRequestModel"
description = "schema for requests to apply a playbook to a system"
content_type = "application/json"
schema = "${file("${path.module}/host/request/postModel.json")}"
}
resource "aws_api_gateway_request_validator" "host_config_request_validator" {
// a body validator for POST to /host/request
name = "hostConfigRequestValidator"
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
validate_request_body = true
}
resource "aws_api_gateway_method" "hostRequest" {
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
resource_id = "${aws_api_gateway_resource.hostRequest.id}"
http_method = "POST"
authorization = "NONE"
request_validator_id = "${aws_api_gateway_request_validator.host_config_request_validator.id}"
request_models = { "application/json" = "hostConfigRequestModel" }
depends_on = [
"aws_api_gateway_model.host_config_request_model"
]
}
resource "aws_api_gateway_integration" "request_to_sqs" {
// put POSTs to /host/request into an SQS queue
// basic integration parameters
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
resource_id = "${aws_api_gateway_method.hostRequest.resource_id}"
http_method = "${aws_api_gateway_method.hostRequest.http_method}"
integration_http_method = "POST"
type = "AWS"
// predefine a queue to write to and a role to use
uri = "arn:aws:apigateway:${data.aws_region.current.name}:sqs:path/${local.sqs_queue_name}"
credentials = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${var.apigw_role}"
// how to integrate with SQS
passthrough_behavior = "NEVER"
request_parameters = {
"integration.request.header.Content-Type" = "'application/x-www-form-urlencoded'"
}
request_templates = {
// this is the code that will run to translate the JSON to SQS primitives
"application/json" = "${file("${path.module}/host/request/intReqMapAppJson")}"
}
}
resource "aws_api_gateway_method_response" "response200" {
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
resource_id = "${aws_api_gateway_method.hostRequest.resource_id}"
http_method = "${aws_api_gateway_method.hostRequest.http_method}"
status_code = "200"
}
resource "aws_api_gateway_integration_response" "hostRequest" {
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
resource_id = "${aws_api_gateway_method.hostRequest.resource_id}"
http_method = "${aws_api_gateway_method.hostRequest.http_method}"
status_code = "${aws_api_gateway_method_response.response200.status_code}"
}
/*
* GET /host/status
*/
resource "aws_api_gateway_request_validator" "host_status_validator" {
name = "hostStatusValidator"
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
validate_request_body = false
validate_request_parameters = true
}
resource "aws_api_gateway_method" "hostStatus" {
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
resource_id = "${aws_api_gateway_resource.hostStatus.id}"
http_method = "GET"
authorization = "NONE"
request_validator_id = "${aws_api_gateway_request_validator.host_status_validator.id}"
request_parameters = {
"method.request.querystring.hosts" = true
}
depends_on = [
aws_api_gateway_resource.hostStatus,
"aws_api_gateway_model.host_config_request_model"
]
}
resource "aws_api_gateway_integration" "hostStatus" {
// basic integration definition
depends_on = [ aws_api_gateway_method.hostStatus ]
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
resource_id = "${aws_api_gateway_method.hostStatus.resource_id}"
http_method = "${aws_api_gateway_method.hostStatus.http_method}"
integration_http_method = "POST"
type = "AWS"
// integration details
uri = "arn:aws:apigateway:${data.aws_region.current.name}:dynamodb:action/BatchGetItem"
request_templates = {
// this is the code that will run to translate the JSON to SQS primitives
"application/json" = templatefile("${path.module}/host/status/intReqMapAppJson.tmpl",
{
user=local.suffix
})
}
credentials = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${var.apigw_role}"
}
resource "aws_api_gateway_method_response" "hostStatusResponse200" {
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
resource_id = "${aws_api_gateway_method.hostStatus.resource_id}"
http_method = "${aws_api_gateway_method.hostStatus.http_method}"
status_code = "200"
}
resource "aws_api_gateway_integration_response" "hostStatus" {
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
resource_id = "${aws_api_gateway_method.hostStatus.resource_id}"
http_method = "${aws_api_gateway_method.hostStatus.http_method}"
status_code = "${aws_api_gateway_method_response.hostStatusResponse200.status_code}"
response_templates = {
"application/json" = templatefile("${path.module}/host/status/intResMapAppJson.tmpl", {user=local.suffix})
}
}
/*
* make deployments, one at-a-time to avoid errors
*/
resource "aws_api_gateway_deployment" "dev" {
# will not be implicitly updated
depends_on = [
aws_api_gateway_integration.hostStatus,
aws_api_gateway_integration.request_to_sqs
]
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
variables = {
deployed_at = "${timestamp()}"
}
stage_name = "dev"
}
resource "aws_api_gateway_deployment" "integration" {
# will be implicitly updated
depends_on = [
aws_api_gateway_deployment.dev,
aws_api_gateway_integration.hostStatus,
aws_api_gateway_integration.request_to_sqs
]
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
variables = {
deployed_at = "${timestamp()}"
}
stage_name = "integration"
}
resource "aws_api_gateway_deployment" "prod" {
# will be implicitly updated
depends_on = [
aws_api_gateway_deployment.integration,
aws_api_gateway_integration.hostStatus,
aws_api_gateway_integration.request_to_sqs
]
rest_api_id = "${aws_api_gateway_rest_api.example.id}"
stage_name = "prod"
}