-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapiGateway.tf
155 lines (131 loc) · 4.63 KB
/
apiGateway.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
resource "aws_api_gateway_rest_api" "apiGateway" {
name = "api-gateway-SQS"
description = "POST records to SQS queue"
}
resource "aws_api_gateway_resource" "form_score" {
rest_api_id = aws_api_gateway_rest_api.apiGateway.id
parent_id = aws_api_gateway_rest_api.apiGateway.root_resource_id
path_part = "form-score"
}
resource "aws_api_gateway_request_validator" "validator_query" {
name = "queryValidator"
rest_api_id = aws_api_gateway_rest_api.apiGateway.id
validate_request_body = true
validate_request_parameters = true
}
resource "aws_api_gateway_method" "method_form_score" {
rest_api_id = aws_api_gateway_rest_api.apiGateway.id
resource_id = aws_api_gateway_resource.form_score.id
http_method = "POST"
authorization = "NONE"
api_key_required = true
request_models = {
"application/json" = aws_api_gateway_model.my_model.name
}
request_parameters = {
"method.request.path.proxy" = false
"method.request.querystring.unity" = true
# example of validation: the above requires this in query string
# https://my-api/dev/form-score?unity=1
}
request_validator_id = aws_api_gateway_request_validator.validator_query.id
}
resource "aws_api_gateway_model" "my_model" {
rest_api_id = aws_api_gateway_rest_api.apiGateway.id
name = "validateBody"
description = "a JSON schema"
content_type = "application/json"
schema = <<EOF
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"title" : "validateTheBody",
"type" : "object",
"properties" : {
"message" : { "type" : "string" }
},
"required" :["message"]
}
EOF
}
resource "aws_api_gateway_usage_plan" "myusageplan" {
name = "my_usage_plan"
api_stages {
api_id = aws_api_gateway_rest_api.apiGateway.id
stage = aws_api_gateway_deployment.api.stage_name
}
}
resource "aws_api_gateway_api_key" "mykey" {
name = "my_key"
}
resource "aws_api_gateway_usage_plan_key" "main" {
key_id = aws_api_gateway_api_key.mykey.id
key_type = "API_KEY"
usage_plan_id = aws_api_gateway_usage_plan.myusageplan.id
}
resource "aws_api_gateway_integration" "api" {
rest_api_id = aws_api_gateway_rest_api.apiGateway.id
resource_id = aws_api_gateway_resource.form_score.id
http_method = aws_api_gateway_method.method_form_score.http_method
type = "AWS"
integration_http_method = "POST"
credentials = aws_iam_role.apiSQS.arn
uri = "arn:aws:apigateway:${var.region}:sqs:path/${aws_sqs_queue.queue.name}"
request_parameters = {
"integration.request.header.Content-Type" = "'application/x-www-form-urlencoded'"
}
# Request Template for passing Method, Body, QueryParameters and PathParams to SQS messages
request_templates = {
"application/json" = <<EOF
Action=SendMessage&MessageBody={
"method": "$context.httpMethod",
"body-json" : $input.json('$'),
"queryParams": {
#foreach($param in $input.params().querystring.keySet())
"$param": "$util.escapeJavaScript($input.params().querystring.get($param))" #if($foreach.hasNext),#end
#end
},
"pathParams": {
#foreach($param in $input.params().path.keySet())
"$param": "$util.escapeJavaScript($input.params().path.get($param))" #if($foreach.hasNext),#end
#end
}
}"
EOF
}
depends_on = [
aws_iam_role_policy_attachment.api_exec_role
]
}
# Mapping SQS Response
resource "aws_api_gateway_method_response" "http200" {
rest_api_id = aws_api_gateway_rest_api.apiGateway.id
resource_id = aws_api_gateway_resource.form_score.id
http_method = aws_api_gateway_method.method_form_score.http_method
status_code = 200
}
resource "aws_api_gateway_integration_response" "http200" {
rest_api_id = aws_api_gateway_rest_api.apiGateway.id
resource_id = aws_api_gateway_resource.form_score.id
http_method = aws_api_gateway_method.method_form_score.http_method
status_code = aws_api_gateway_method_response.http200.status_code
selection_pattern = "^2[0-9][0-9]" // regex pattern for any 200 message that comes back from SQS
depends_on = [
aws_api_gateway_integration.api
]
}
resource "aws_api_gateway_deployment" "api" {
rest_api_id = aws_api_gateway_rest_api.apiGateway.id
stage_name = var.environment
depends_on = [
aws_api_gateway_integration.api,
]
# Redeploy when there are new updates
triggers = {
redeployment = sha1(join(",", list(
jsonencode(aws_api_gateway_integration.api),
)))
}
lifecycle {
create_before_destroy = true
}
}