首页 > 解决方案 > c# 中用于 API 网关 websockets 的 aws CDK 构造

问题描述

我正在使用 AWS CDK 为 API 网关 websocket 构建堆栈。 我可以在这里看到这个文档

但它没有提供任何明确的解释,用于网络套接字的构造。有人可以帮助我正确构造用于 websockets。

标签: amazon-web-servicesaws-api-gatewayaws-sdk-net

解决方案


直到今天,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", {
  apiId: api.ref,
  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`, {
  apiId: api.ref,
  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 中,你可以找到使用过的函数和类。


推荐阅读