简体   繁体   English

如何使用无服务器框架创建 API 的新版本?

[英]How do you create a new version of an API using serverless framework?

I have a simple API developed using serverless framework that is deployed in production.我有一个使用无服务器框架开发的简单 API 部署在生产环境中。 The serverless.yaml is similar to this one: serverless.yaml 与这个类似:

service: example

provider:
    name: aws

plugins:
    - serverless-plugin-scripts
    - serverless-offline

functions:
    package:
        runtime: nodejs14.x
        handler: build/index.handler
        events:
            - httpApi: "POST /test"

The API will change in the next version and I want to offer backward compatibility to my clients. API 将在下一个版本中更改,我想为我的客户提供向后兼容性。 I want to create a /v1/test route in API Gateway that will point to the new implementation of the function and I want /test to remain the same.我想在 API 网关中创建一个/v1/test路由,它将指向 function 的新实现,我希望/test保持不变。

Is there a way to do this using serverless framework?有没有办法使用无服务器框架来做到这一点?

There are a few things you can do.您可以做几件事。

  1. Create a new function entirely, with a new route.使用新路由完全创建一个新的 function。 This option is simplest to implement, but your setup may not allow you to create a new function for some reason (CloudFormation stack limits, or other non-functional reasons).此选项实施起来最简单,但由于某些原因(CloudFormation 堆栈限制或其他非功能性原因),您的设置可能不允许您创建新的 function。
functions:
    # V0 package function
    package:
        runtime: nodejs14.x
        handler: build/index.handler
        events:
            - httpApi: "POST /test"
    # V1 package function
    packageV1:
        runtime: nodejs14.x
        handler: build/index.handlerv1
        events:
            - httpApi: "POST v1/test"
  1. Use the same function, but append a new path.使用相同的 function,但 append 是一条新路径。 Then inside your function code inspect the event payload to determine which path was called and use that to modify the response or functionality to adhere to either API spec.然后在您的 function 代码中检查event负载以确定调用了哪个路径并使用它来修改响应或功能以遵守 API 规范。
    package:
        runtime: nodejs14.x
        handler: build/index.handler # Both routes share the function, you'll need to modify logic to implement both v0/v1 spec
        events:
            - httpApi: "POST /test" # V0 route
            - httpApi: "POST v1/test" # V1 route

Both of these are good for temporary migrations where you'll eventually deprecate the old API. If you need both in perpetuity, you could also migrate your v0 API into a new stack (or similarly create a new stack for the v1 API).这两个都适用于临时迁移,您最终会弃用旧的 API。如果您永久需要这两个,您还可以将 v0 API 迁移到一个新堆栈(或类似地为 v1 API 创建一个新堆栈)。

Lambda is priced per-invocation, not per-function. Lambda 按调用计费,而不是按函数计费。 So with that in mind, I'd suggest creating a totally distinct function, that will make it easier to deprecate and delete when the time comes.因此,考虑到这一点,我建议创建一个完全不同的 function,这样在时机成熟时更容易弃用和删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Serverless Framework 管理 Lambda 内部的 Aurora Serverless 数据 api 的 typeORM 连接 - How to manage typeORM connection of Aurora Serverless data api inside Lambda using Serverless Framework 如何使用无服务器框架为 API 网关设置描述? - How to set a description for an API Gateway using the Serverless Framework? 如何使用 AWS 的无服务器框架获取最新的 Layer 版本 Lambda - How to get latest Layer version with serverless framework for AWS Lambda 如何在 Serverless Framework 中分配 function 级别的 IamRoleStatements? - How do I assign function level IamRoleStatements in Serverless Framework? 如何使用 Serverless Framework 在多个区域部署堆栈? - how to deploy a stack in multiple regions using the Serverless Framework? 使用无服务器框架请求验证 - Request validation using serverless framework 如何使用 serverless.io 框架和 serverless.yml 文件将整数作为变量传递给 aws-step-function 中的等待状态类型 - How to Pass integer as variable to Wait Type of State in aws-step-function using serverless.io framework and the serverless.yml file AWS - 无服务器离线框架上可用的 API 个密钥? - AWS - API keys available on the Serverless Offline framework? 如何使用 GCP 的 NodeJS 库部署新的 API 网关 OpenAPI 规范? - How do you use GCP's NodeJS library to deploy new API Gateway OpenAPI Specs? 将无服务器框架转换为 Typescript CDK - API 网关身份验证 - Converting Serverless framework to Typescript CDK - API Gateway authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM