0

我在查找有关如何通过云开发工具包 (CDK) 在 API 网关上为“集成请求”创建“URL 查询字符串参数”的示例时遇到问题。我发现的大多数示例都是针对 lambda(我不需要这个)而不是 REST(我需要这个),甚至那些不涵盖集成请求。

我正在通过 aws-apigateway.SpecRestAPI 创建 api 定义。

const api = new apiGateway.SpecRestApi(this, 'my-api', {
      apiDefinition: apiGateway.ApiDefinition.fromInline(openApiDefinition),

我不确定我是否将集成绑定到 API。

如何将集成与 API 联系起来,如何通过 GUI 映射集成请求?

我尝试导出手动配置的 API 网关,但它不包含有关在何处执行翻译的任何信息。

让我知道是否需要添加更多信息并提前致谢!

4

1 回答 1

1

如果使用 ApiDefinition.fromInline 则请求映射进入 OpenAPI 文件。请参阅https://docs.aws.amazon.com/apigateway/latest/developerguide/request-response-data-mappings.htmlhttps://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway -swagger-extensions-integration-requestParameters.html

“requestParameters”位于 x-amazon-apigateway-integration 节点下。如果您不知道如何获取 OpenAPI 规范,请像往常一样创建 API 和集成,然后通过https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-migrate-accounts导出文件-地区/

此外,为了将集成映射到另一个 AWS 服务(在我的情况下为 SNS),我在实例化集成时没有指定 API 对象。下面是一个工作示例。

const api = new apiGateway.SpecRestApi(this, 'my-api', {
      apiDefinition: apiGateway.ApiDefinition.fromInline(openApiDefinition)
)

const snsIntegration = new apiGateway.AwsIntegration(
  api,
  {
    proxy: false,
    service: "sns",
    action: "PutItem",
  }
);

此外,如果您遇到“指定的映射表达式参数无效”的问题,请确保您在方法请求和集成请求中都定义了参数。

OpenAPI 文件的超级精简版本如下:

paths:
  /v1/contact:
    post:
      parameters:
        - name: "TopicArn"
          in: "query"
          required: true
          schema:
            type: "string"
      x-amazon-apigateway-integration:
        requestParameters : {
          integration.request.querystring.TopicArn : "method.request.header.TopicArn",
          integration.request.querystring.Message : "method.request.body",
        }
于 2020-12-18T20:34:53.463 回答