是否有一种方法可以使用AWS CDK和AWS -apigatewayv2包向AWS WebSocket API添加集成响应?这个答案展示了使用CloudFormation实现这一目标的好方法。但我还没能把它翻译成AWS CDK。谢谢!
编辑:
对不起,我应该澄清我现在如何尝试添加积分响应:
const webSocketApi = new WebSocketApi(this, 'Api', {
defaultRouteOptions: {
integration: new LambdaWebSocketIntegration({ handler: lambdaFn }),
},
})
new CfnIntegrationResponse(this, 'response', {
apiId: webSocketApi.apiId,
integrationId: /* ? */,
integrationResponseKey: '$default',
})
const stage = new WebSocketStage(this, 'Stage', {
webSocketApi,
stageName: 'dev',
autoDeploy: true,
})
我可以使用CfnIntegrationResponse添加集成响应,但我没有办法访问LambdaWebSocketIntegration的集成id。
解决方案是使用CfnRouteResponse
代替CfnIntegrationResponse
,如下所示:
const api = new WebSocketApi(...)
const route = api.addRoute(...)
new apigateway.CfnRouteResponse(this, "wsRouteResponse", {
apiId: api.apiId,
routeId: route.routeId,
routeResponseKey: "$default",
});
仍然没有办法在cdk中重写这个CloudFormation示例。
##########Socket API###############
webSocket:
Type: AWS::ApiGatewayV2::Api
Properties:
Name: WebSocket
ProtocolType: WEBSOCKET
RouteSelectionExpression: "$request.body.action"
ConnectRoute:
Type: AWS::ApiGatewayV2::Route
Properties:
ApiId: !Ref webSocket
RouteKey: $connect
AuthorizationType: NONE
OperationName: ConnectRoute
RouteResponseSelectionExpression: $default # add this
Target: !Join
- '/'
- - 'integrations'
- !Ref ConnectInteg
ConnectInteg:
Type: AWS::ApiGatewayV2::Integration
Properties:
ApiId: !Ref webSocket
Description: Connect Integration
IntegrationType: AWS_PROXY
IntegrationUri:
Fn::Sub:
arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${restAndSocketLambda.Arn}/invocations
ConnectRouteResponse: # Add this
Type: 'AWS::ApiGatewayV2::RouteResponse'
Properties:
RouteId: !Ref ConnectRoute
ApiId: !Ref webSocket
RouteResponseKey: $default
ConnectIntegResponse: # Add this(if required)
Type: 'AWS::ApiGatewayV2::IntegrationResponse'
Properties:
IntegrationId: !Ref ConnectInteg
IntegrationResponseKey: /201/
ApiId: !Ref webSocket
我尝试使用逃生舱口,但我没有设法向合成的Cloudformation模板添加有效的资源引用。
我建议使用CfnInclude构造将整个模板片段包含在堆栈中。
new CfnInclude(this, 'ID', {
template: {
Resources: {
Bucket: {
Type: 'AWS::S3::Bucket',
Properties: {
BucketName: 'my-shiny-bucket'
}
}
}
},
});