main.tf
6.04 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
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
# main.tf
# 查询由 K8s 创建的 NLB
data "aws_lb" "microservices_nlb" {
name = var.nlb_service_name
}
# 获取 ACM 证书
data "aws_acm_certificate" "wildcard_cert" {
domain = var.wildcard_cert_domain
statuses = ["ISSUED"]
most_recent = true
}
# REST API Gateway
resource "aws_api_gateway_rest_api" "ecommerce_api" {
name = var.api_gateway_name
description = "Ecommerce Microservices API Gateway"
endpoint_configuration {
types = ["REGIONAL"]
}
tags = {
Environment = var.environment
Project = var.project_name
}
}
# API Gateway Resource - 代理所有路径
resource "aws_api_gateway_resource" "proxy" {
rest_api_id = aws_api_gateway_rest_api.ecommerce_api.id
parent_id = aws_api_gateway_rest_api.ecommerce_api.root_resource_id
path_part = "{proxy+}"
}
# 代理方法 - ANY
resource "aws_api_gateway_method" "proxy" {
rest_api_id = aws_api_gateway_rest_api.ecommerce_api.id
resource_id = aws_api_gateway_resource.proxy.id
http_method = "ANY"
authorization = "NONE"
request_parameters = {
"method.request.path.proxy" = true
}
}
# 集成到 NLB - 使用 HTTPS
resource "aws_api_gateway_integration" "nlb_integration" {
rest_api_id = aws_api_gateway_rest_api.ecommerce_api.id
resource_id = aws_api_gateway_resource.proxy.id
http_method = aws_api_gateway_method.proxy.http_method
integration_http_method = "ANY"
type = "HTTP_PROXY"
uri = "https://${data.aws_lb.microservices_nlb.dns_name}:443/{proxy}" # 使用 HTTPS 和 443 端口
connection_type = "VPC_LINK"
connection_id = aws_api_gateway_vpc_link.nlb_vpc_link.id
request_parameters = {
"integration.request.path.proxy" = "method.request.path.proxy"
}
}
# 方法响应
resource "aws_api_gateway_method_response" "proxy_response" {
rest_api_id = aws_api_gateway_rest_api.ecommerce_api.id
resource_id = aws_api_gateway_resource.proxy.id
http_method = aws_api_gateway_method.proxy.http_method
status_code = "200"
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_response" "proxy_integration_response" {
rest_api_id = aws_api_gateway_rest_api.ecommerce_api.id
resource_id = aws_api_gateway_resource.proxy.id
http_method = aws_api_gateway_method.proxy.http_method
status_code = aws_api_gateway_method_response.proxy_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" = "'*'"
"method.response.header.Access-Control-Allow-Origin" = "'*'"
}
depends_on = [aws_api_gateway_integration.nlb_integration]
}
# VPC Link 连接到 NLB
resource "aws_api_gateway_vpc_link" "nlb_vpc_link" {
name = var.vpc_link_name
description = "VPC Link to Ecommerce NLB"
target_arns = [data.aws_lb.microservices_nlb.arn]
tags = {
Environment = var.environment
Project = var.project_name
}
}
# 部署 API Gateway
resource "aws_api_gateway_deployment" "api_deployment" {
depends_on = [
aws_api_gateway_integration.nlb_integration,
aws_api_gateway_integration_response.proxy_integration_response
]
rest_api_id = aws_api_gateway_rest_api.ecommerce_api.id
lifecycle {
create_before_destroy = true
}
}
# 自定义域名
resource "aws_api_gateway_domain_name" "api_domain" {
domain_name = var.api_domain_name
regional_certificate_arn = data.aws_acm_certificate.wildcard_cert.arn
endpoint_configuration {
types = ["REGIONAL"]
}
tags = {
Environment = var.environment
Project = var.project_name
}
}
# API 映射到自定义域名
resource "aws_api_gateway_base_path_mapping" "api_mapping" {
api_id = aws_api_gateway_rest_api.ecommerce_api.id
stage_name = aws_api_gateway_deployment.api_deployment.stage_name
domain_name = aws_api_gateway_domain_name.api_domain.domain_name
}
# 添加 CORS 支持 - 根路径 OPTIONS 方法
resource "aws_api_gateway_method" "root_options" {
rest_api_id = aws_api_gateway_rest_api.ecommerce_api.id
resource_id = aws_api_gateway_rest_api.ecommerce_api.root_resource_id
http_method = "OPTIONS"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "root_options_integration" {
rest_api_id = aws_api_gateway_rest_api.ecommerce_api.id
resource_id = aws_api_gateway_rest_api.ecommerce_api.root_resource_id
http_method = aws_api_gateway_method.root_options.http_method
type = "MOCK"
request_templates = {
"application/json" = "{\"statusCode\": 200}"
}
}
resource "aws_api_gateway_method_response" "root_options_response" {
rest_api_id = aws_api_gateway_rest_api.ecommerce_api.id
resource_id = aws_api_gateway_rest_api.ecommerce_api.root_resource_id
http_method = aws_api_gateway_method.root_options.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_response" "root_options_integration_response" {
rest_api_id = aws_api_gateway_rest_api.ecommerce_api.id
resource_id = aws_api_gateway_rest_api.ecommerce_api.root_resource_id
http_method = aws_api_gateway_method.root_options.http_method
status_code = aws_api_gateway_method_response.root_options_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" = "'GET,POST,PUT,DELETE,OPTIONS'"
"method.response.header.Access-Control-Allow-Origin" = "'*'"
}
depends_on = [aws_api_gateway_integration.root_options_integration]
}