【问题标题】:Adding integration response to AWS websocket API with @aws-cdk/aws-apigatewayv2使用 @aws-cdk/aws-apigatewayv2 向 AWS websocket API 添加集成响应
【发布时间】:2021-11-28 12:44:59
【问题描述】:

有没有办法使用带有 aws-apigatewayv2 包的 AWS CDK 向 AWS WebSocket API 添加集成响应? This answer 展示了使用 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。

【问题讨论】:

  • Stack Overflow 希望您先尝试一些事情 - 那么您尝试了什么?如果您查看 apigatewayv2 的 CDK 文档,您会发现 Websockets 存在但处于试验阶段(这意味着它们可以在任何部署中更改)。 docs.aws.amazon.com/cdk/api/latest/docs/…你有什么尝试?
  • 我认为 AWS CDK 还不支持集成响应。看一下官方文档。 docs.aws.amazon.com/cdk/api/latest/docs/…
  • 使用 CloudFormation 可以做的任何事情都可以用 aws-cdk 做。
  • 感谢大家对此进行调查。 @lynkfox - 我已经更新了问题。
  • 看起来您将不得不使用其中一个逃生舱口 - cdk 的核心模块中有许多代表 cloudformation 标签的功能 - 它们允许您“编写”自己的 cloudformation sn -p 在 cdk 中。看看如何使用 cfn 的 Escape Hatches 来实现更高级别的 CDK 结构尚未连接到的功能docs.aws.amazon.com/cdk/latest/guide/cfn_layer.html

标签: amazon-web-services amazon-cloudformation aws-api-gateway aws-cdk


【解决方案1】:

仍然无法在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

我尝试使用escape hatches,但我没有设法将有效资源引用添加到合成的 Cloudformation 模板。

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

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

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2019-07-05
  • 2021-08-15
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-16
  • 2020-06-04
相关资源
最近更新 更多