AWS CDK 将更改部署到 swagger 保留旧值

AWS CDK deploying changes to swagger keeps old values

我正在使用 AWS CDK (v1.100.0) 来管理 API 网关部署。 端点定义来自 swagger 文件。 堆栈看起来像这样:

export class MyStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const api = new apigateway.SpecRestApi(this, 'my-api', {
      cloudWatchRole: false,
      endpointTypes: [ EndpointType.REGIONAL ],
      apiDefinition: apigateway.ApiDefinition.fromAsset('path/to/swagger.yaml'),
      deployOptions: {
        stageName: 'dev',
        etc...
      }
    });
  }
}

这正确地创建了一个 API 网关和阶段。

但是,每当我更改 swagger 文件时,例如,我将端点方法从 POST 更改为 PUT,当我重新部署更改时,旧方法和新方法都存在(如果我做其他更改也是一样,例如修改端点路径等)。

如果能深入了解为什么会发生这种情况,我们将不胜感激。

您必须重新部署舞台。您可以通过更改 AWS::ApiGatway::Deployment 或 AWS::ApiGateway::Stage 上的某些内容以强制其更新或使用自定义 lambda 函数来执行此操作。

AWS::ApiGateway::RestApi中有一个Mode属性,它曾经默认为overwrite(见https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-restapi.html#cfn-apigateway-restapi-mode)。

但是最近默认值从 overwrite 更改为 merge

If you don't specify this property, a default value is chosen. For REST APIs created before March 29, 2021, the default is overwrite. Otherwise, the default value is merge.

这就是导致此问题的原因。

遗憾的是,此 属性 尚未在 CDK API 中公开。我提出了一个 GitHub 问题,请参阅 https://github.com/aws/aws-cdk/issues/14436

与此同时,解决方法(如票证中所建议的)是:

const api = new apigateway.SpecRestApi(this, 'my-api', {....});

(api.node.defaultChild as CfnRestApi).addPropertyOverride('Mode', 'overwrite');