main.tf
7.12 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# 查询由 K8s 创建的 NLB
data "aws_lb" "user_mgmt_nlb" {
name = "user-mgmt-nlb" # 这个名称在配置中是固定的
}
# 创建 REST API Gateway
resource "aws_api_gateway_rest_api" "user_management" {
name = "user-management-api"
description = "API Gateway for Spring Boot User Management Application"
endpoint_configuration {
types = ["REGIONAL"]
}
}
# 创建代理资源,捕获所有路径
resource "aws_api_gateway_resource" "proxy" {
rest_api_id = aws_api_gateway_rest_api.user_management.id
parent_id = aws_api_gateway_rest_api.user_management.root_resource_id
path_part = "{proxy+}"
}
# 为代理资源创建 ANY 方法
resource "aws_api_gateway_method" "proxy" {
rest_api_id = aws_api_gateway_rest_api.user_management.id
resource_id = aws_api_gateway_resource.proxy.id
http_method = "ANY"
authorization = "NONE"
request_parameters = {
"method.request.path.proxy" = true
}
}
# 为根路径创建方法
resource "aws_api_gateway_method" "root" {
rest_api_id = aws_api_gateway_rest_api.user_management.id
resource_id = aws_api_gateway_rest_api.user_management.root_resource_id
http_method = "ANY"
authorization = "NONE"
}
# 创建 VPC Link
resource "aws_api_gateway_vpc_link" "user_management_nlb_link" {
name = "user-management-nlb-link"
target_arns = [data.aws_lb.user_mgmt_nlb.arn] # 指向内部NLB ARN
description = "VPC Link for internal User Management NLB"
}
# 创建与 K8s NLB 的集成 - 代理路径
resource "aws_api_gateway_integration" "k8s_integration" {
rest_api_id = aws_api_gateway_rest_api.user_management.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.user_mgmt_nlb.dns_name}/{proxy}"
connection_type = "VPC_LINK"
connection_id = aws_api_gateway_vpc_link.user_management_nlb_link.id # 关联VPC Link
request_parameters = {
"integration.request.path.proxy" = "method.request.path.proxy"
}
}
# 创建与 K8s NLB 的集成 - 根路径(修复:添加 VPC Link 配置)
resource "aws_api_gateway_integration" "root_integration" {
rest_api_id = aws_api_gateway_rest_api.user_management.id
resource_id = aws_api_gateway_rest_api.user_management.root_resource_id
http_method = aws_api_gateway_method.root.http_method
integration_http_method = "ANY"
type = "HTTP_PROXY"
uri = "https://${data.aws_lb.user_mgmt_nlb.dns_name}/"
connection_type = "VPC_LINK" # 添加这行
connection_id = aws_api_gateway_vpc_link.user_management_nlb_link.id # 添加这行
}
# 为代理路径添加方法响应
resource "aws_api_gateway_method_response" "proxy_200" {
rest_api_id = aws_api_gateway_rest_api.user_management.id
resource_id = aws_api_gateway_resource.proxy.id
http_method = aws_api_gateway_method.proxy.http_method
status_code = "200"
response_models = {
"application/json" = "Empty"
}
}
resource "aws_api_gateway_method_response" "proxy_500" {
rest_api_id = aws_api_gateway_rest_api.user_management.id
resource_id = aws_api_gateway_resource.proxy.id
http_method = aws_api_gateway_method.proxy.http_method
status_code = "500"
}
# 为代理路径添加集成响应
resource "aws_api_gateway_integration_response" "proxy_200" {
rest_api_id = aws_api_gateway_rest_api.user_management.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_200.status_code
depends_on = [
aws_api_gateway_integration.k8s_integration
]
}
resource "aws_api_gateway_integration_response" "proxy_500" {
rest_api_id = aws_api_gateway_rest_api.user_management.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_500.status_code
selection_pattern = "5\\d{2}"
depends_on = [
aws_api_gateway_integration.k8s_integration
]
}
# 为根路径添加方法响应
resource "aws_api_gateway_method_response" "root_200" {
rest_api_id = aws_api_gateway_rest_api.user_management.id
resource_id = aws_api_gateway_rest_api.user_management.root_resource_id
http_method = aws_api_gateway_method.root.http_method
status_code = "200"
response_models = {
"application/json" = "Empty"
}
}
resource "aws_api_gateway_method_response" "root_500" {
rest_api_id = aws_api_gateway_rest_api.user_management.id
resource_id = aws_api_gateway_rest_api.user_management.root_resource_id
http_method = aws_api_gateway_method.root.http_method
status_code = "500"
}
# 为根路径添加集成响应
resource "aws_api_gateway_integration_response" "root_200" {
rest_api_id = aws_api_gateway_rest_api.user_management.id
resource_id = aws_api_gateway_rest_api.user_management.root_resource_id
http_method = aws_api_gateway_method.root.http_method
status_code = aws_api_gateway_method_response.root_200.status_code
depends_on = [
aws_api_gateway_integration.root_integration
]
}
resource "aws_api_gateway_integration_response" "root_500" {
rest_api_id = aws_api_gateway_rest_api.user_management.id
resource_id = aws_api_gateway_rest_api.user_management.root_resource_id
http_method = aws_api_gateway_method.root.http_method
status_code = aws_api_gateway_method_response.root_500.status_code
selection_pattern = "5\\d{2}"
depends_on = [
aws_api_gateway_integration.root_integration
]
}
# 创建部署
resource "aws_api_gateway_deployment" "deployment" {
depends_on = [
aws_api_gateway_integration.k8s_integration,
aws_api_gateway_integration.root_integration,
aws_api_gateway_integration_response.proxy_200,
aws_api_gateway_integration_response.proxy_500,
aws_api_gateway_integration_response.root_200,
aws_api_gateway_integration_response.root_500,
aws_api_gateway_vpc_link.user_management_nlb_link # 添加 VPC Link 依赖
]
rest_api_id = aws_api_gateway_rest_api.user_management.id
triggers = {
redeployment = sha1(jsonencode([
aws_api_gateway_resource.proxy.id,
aws_api_gateway_method.proxy.id,
aws_api_gateway_method.root.id,
aws_api_gateway_integration.k8s_integration.id,
aws_api_gateway_integration.root_integration.id,
aws_api_gateway_method_response.proxy_200.id,
aws_api_gateway_method_response.proxy_500.id,
aws_api_gateway_method_response.root_200.id,
aws_api_gateway_method_response.root_500.id,
aws_api_gateway_integration_response.proxy_200.id,
aws_api_gateway_integration_response.proxy_500.id,
aws_api_gateway_integration_response.root_200.id,
aws_api_gateway_integration_response.root_500.id,
aws_api_gateway_vpc_link.user_management_nlb_link.id # 添加 VPC Link 到触发器
]))
}
lifecycle {
create_before_destroy = true
}
}
# 创建阶段
resource "aws_api_gateway_stage" "prod" {
deployment_id = aws_api_gateway_deployment.deployment.id
rest_api_id = aws_api_gateway_rest_api.user_management.id
stage_name = "prod"
}