-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.tf
More file actions
126 lines (95 loc) · 3.23 KB
/
api.tf
File metadata and controls
126 lines (95 loc) · 3.23 KB
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
resource "aws_api_gateway_rest_api" "api" {
name = var.name
description = "Simple Screen Capturing utility"
binary_media_types = ["*/*"]
tags = var.tags
}
resource "aws_api_gateway_resource" "api_proxy" {
rest_api_id = aws_api_gateway_rest_api.api.id
parent_id = aws_api_gateway_rest_api.api.root_resource_id
path_part = "{proxy+}"
}
resource "aws_api_gateway_method" "api_proxy" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.api_proxy.id
http_method = "ANY"
authorization = "NONE"
api_key_required = true
}
resource "aws_api_gateway_integration" "lambda" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_method.api_proxy.resource_id
http_method = aws_api_gateway_method.api_proxy.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.lambda.invoke_arn
content_handling = "CONVERT_TO_BINARY"
}
resource "aws_api_gateway_method_response" "response_200" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_method.api_proxy.resource_id
http_method = aws_api_gateway_method.api_proxy.http_method
status_code = "200"
}
resource "aws_api_gateway_integration_response" "lambda" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_method.api_proxy.resource_id
http_method = aws_api_gateway_method.api_proxy.http_method
status_code = aws_api_gateway_method_response.response_200.status_code
content_handling = "CONVERT_TO_BINARY"
}
resource "aws_api_gateway_deployment" "api" {
depends_on = [aws_api_gateway_integration.lambda]
rest_api_id = aws_api_gateway_rest_api.api.id
}
resource "aws_api_gateway_stage" "prod" {
deployment_id = aws_api_gateway_deployment.api.id
rest_api_id = aws_api_gateway_rest_api.api.id
stage_name = "prod"
lifecycle {
create_before_destroy = true
}
}
resource "aws_api_gateway_domain_name" "api" {
count = var.domain_name == "" ? 0 : 1
domain_name = var.domain_name
regional_certificate_arn = var.certificate_arn
endpoint_configuration {
types = ["REGIONAL"]
}
tags = var.tags
}
resource "aws_api_gateway_base_path_mapping" "path_mapping" {
count = var.domain_name == "" ? 0 : 1
api_id = aws_api_gateway_rest_api.api.id
stage_name = aws_api_gateway_stage.prod.stage_name
domain_name = aws_api_gateway_domain_name.api[0].domain_name
}
resource "aws_api_gateway_usage_plan" "usageplan" {
name = var.name
api_stages {
api_id = aws_api_gateway_rest_api.api.id
stage = aws_api_gateway_stage.prod.stage_name
}
tags = var.tags
}
resource "aws_api_gateway_api_key" "key" {
for_each = var.keys
name = each.key
tags = var.tags
}
resource "aws_api_gateway_usage_plan_key" "main" {
for_each = var.keys
key_id = aws_api_gateway_api_key.key[each.key].id
key_type = "API_KEY"
usage_plan_id = aws_api_gateway_usage_plan.usageplan.id
}
output "base_url" {
value = aws_api_gateway_stage.prod.invoke_url
}
output "domain_cname" {
value = aws_api_gateway_domain_name.api.*.regional_domain_name
}
output "api_keys" {
value = {for key, val in aws_api_gateway_api_key.key : key => val.value}
}