簡體   English   中英

使用 @aws-cdk/aws-apigatewayv2 向 AWS websocket API 添加集成響應

[英]Adding integration response to AWS websocket API with @aws-cdk/aws-apigatewayv2

有沒有辦法使用帶有 aws-apigatewayv2 包的 AWS CDK 向 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。

仍然無法在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'
        }
      }
    }
  },
});

解決方案是使用CfnRouteResponse而不是CfnIntegrationResponse ,如下所示:

const api = new WebSocketApi(...)
const route = api.addRoute(...)

new apigateway.CfnRouteResponse(this, "wsRouteResponse", {
    apiId: api.apiId,
    routeId: route.routeId,
    routeResponseKey: "$default",
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM