API网关Websocket的C#中的AWS CDK构造

问题描述

我正在使用AWS CDK为API网关websocket构建堆栈。 I can see this documentation here

,但是它没有提供任何明确的解释,以说明用于Web套接字的构造。有人可以帮助我构造适合用于Websockets的东西。

解决方法

直到今天,CDK团队仍未像其他更常见的AWS组件一样发布易于使用的API,因此在这方面缺少任何文档。

当前,您可以使用较低级别的结构来获得结果,或者通过CLI,脚本或Web控制台在CDK环境之外处理WS管理。

如果要在等待CDK相对于Websockets API进入更好阶段时使用一些较低级别的结构,那么这里是一个用TypeScript编写的小例子:

// Handle your other resources like roles,lambdas,and dependencies
// ...

// Example API definition
const api = new CfnApi(this,name,{
  name: "ChatAppApi",protocolType: "WEBSOCKET",routeSelectionExpression: "$request.body.action",});

// Example lambda integration
const connectIntegration = new CfnIntegration(this,"connect-lambda-integration",{
  apiId: api.ref,integrationType: "AWS_PROXY",integrationUri: "arn:aws:apigateway:" + config["region"] + ":lambda:path/2015-03-31/functions/" + connectFunc.functionArn + "/invocations",credentialsArn: role.roleArn,});

// Example route definition
const connectRoute = new CfnRoute(this,"connect-route",routeKey: "$connect",authorizationType: "NONE",target: "integrations/" + connectIntegration.ref,});

// Finishing touches on the API definition
const deployment = new CfnDeployment(this,`${name}-deployment`,{
  apiId: api.ref
});

new CfnStage(this,`${name}-stage`,autoDeploy: true,deploymentId: deployment.ref,stageName: "dev"
});

const dependencies = new ConcreteDependable();
dependencies.add(connectRoute)

我从某人对样本文档进行的PR中获得了以下信息: https://github.com/aws-samples/aws-cdk-examples/pull/325/files

我还在对此进行试验,但是至少在最新版本的CDK中,您可以找到使用的函数和类。