AWS API 网关:如何实现持续交付?

AWS API Gateway: How to achieve continuous delivery?

我正在使用 AWS API 网关和 AWS Lambda 构建 API。我想为此 API 实现持续交付。我选择的方法是通过 AWS CodePipeline 使用 CloudFormation。我已经设法将它用于另一个使用 Lambdas 的项目(没有 API 网关),它运行良好并且使用起来非常愉快。

我在部署时面临的问题是 Lambda 已正确更新,但 API 定义未正确更新。据我了解,AWS::ApiGateway::Deployment 是不可变资源,这意味着对于 API 的每次部署,我都需要创建一个新的 AWS::ApiGateway::Deployment 资源。这根本不实用,因为对于每个 AWS::ApiGateway::Deployment 我都有一个新的 Invoke URL。这是不可接受的,因为我必须将我的 DNS 记录更改为新部署的 API 调用 URL 或要求我们的 API 用户更改其应用程序中的 URL。

我想要的是能够更改 API 定义和 Lambdas 实现,而我的 API 用户无需更改其应用程序中的任何内容。

我怎样才能实现这种行为?

我创建了一个教程来强调我的问题。您可以在以下位置找到它:https://github.com/JonathanGailliez/aws-api-gateway-lambda-example

实现这一目标的一种方法是利用

等现有框架
  1. AWS SAM
  2. Serverless
  3. Claudia

您可以 运行 从命令行或 AWS 控制台更新 Cloudformation。这将更改 API 定义和任何 lambda 代码,而不会更改唯一 ID 以访问您的网关。

另一种选择是将您的 API 置于自定义域名之后,然后您可以继续部署新的 API 或暂存并在准备就绪时切换自定义域映射。用户不会识别任何更改。

根据:https://forums.aws.amazon.com/thread.jspa?messageID=789869&#789869

joey-aws 说:

We are currently in the process of rolling out a solution which addresses this exact problem. In the meantime, a common workaround would be to update something small, such as a "description" field which could then be used to "trigger" an API Gateway deployment when updating the CloudFormation stack.

一旦推出,我将更新此答案和示例存储库。

我能够通过在 Python 中使用对流层和 boto3 api 生成的 CloudFormation 模板来实现此目的,如下所示:

  1. 将模板分成两部分
    • API 定义、方法、IAM 角色、ApiKey 和 Lambda (a)
    • 部署、UsagePlan 和 UsagePlanKey (b)
  2. 更改后的 Lambda 代码将压缩并使用 boto3 上传到 S3 api
  3. 堆栈 (b) 已删除
  4. 堆栈 (a) 更新为连接到 lambda 的 GET 方法的新资源 ID
  5. 堆栈 (b) 重新创建

第 3、4、5 步使用 CloudFormation boto3 api 执行,直到完成为止。

最重要的是,在完成所有步骤后,ApiKey 值和阶段调用 URL 保持不变,运行 更新了 Lambda 代码,如使用 curl 测试的那样。

注意:CloudFormation 更新完成后,API 可能还需要 30-60 秒才能完全正常运行。