使用 @aws-cdk/aws-apigatewayv2 添加对 AWS websocket API 的集成响应

Mar*_*áva 8 amazon-web-services aws-cloudformation aws-api-gateway aws-cdk

有没有办法使用 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,
  })
Run Code Online (Sandbox Code Playgroud)

我可以使用 CfnIntegrationResponse 添加集成响应,但我没有办法访问 LambdaWebSocketIntegration 的集成 ID。

Sto*_*iev 0

仍然无法在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
Run Code Online (Sandbox Code Playgroud)

我尝试使用逃生舱口,但我没有设法将有效的资源引用添加到合成的 Cloudformation 模板中。

我建议使用CfnInclude构造将整个模板片段包含在堆栈中。

new CfnInclude(this, 'ID', {
  template: {
    Resources: {
      Bucket: {
        Type: 'AWS::S3::Bucket',
        Properties: {
          BucketName: 'my-shiny-bucket'
        }
      }
    }
  },
});
Run Code Online (Sandbox Code Playgroud)