【发布时间】:2021-02-20 11:01:34
【问题描述】:
我一直在将 AWS SAM 用于 websocket API,并且运行良好。今天,我需要添加一个 HTTPS API,但是我尝试这样做会导致在不同的子域下创建一个新的 API 网关。我希望他们共享同一个域,因此用户只需更改协议(从 HTTPS 到 WSS)。
这是我template.yaml的相关部分:
# This seems to implicitly create and use a `ServerlessRestApi` API gateway
PingLambda:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambda/
Handler: app.pingHandler
Runtime: nodejs12.x
Events:
Ping:
Type: Api
Properties:
Path: /ping
Method: get
# I use this for the various websocket lambdas, works like a charm
WebSocketApi:
Type: AWS::ApiGatewayV2::Api
Properties:
Name: SimpleRelayWebSocket
ProtocolType: WEBSOCKET
RouteSelectionExpression: \\$default
# ...
# I'd love both of these to use the same subdomain/API gateway setup
Outputs:
APIURL:
Description: URL of the Simple Relay HTTPS API
Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/${Stage}'
WebSocketURL:
Description: URL of the Simple Relay WSS API
Value: !Sub 'wss://${WebSocketApi}.execute-api.${AWS::Region}.amazonaws.com/${Stage}'
考虑到WebSocketApi 在那里设置了ProtocolType: WEBSOCKET,我不确定如何继续。是否有某种方法可以将 API 拆分为具有不同协议的多个事物,或者我还缺少什么?
【问题讨论】:
-
您需要有两个 API。您可以使用自定义域(例如 api.mydomain.com 和 ws.mydomain.com)来屏蔽两个物理上不同的 API 网关。
标签: amazon-web-services aws-api-gateway aws-sam