[英]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.您可以做几件事。
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"
event
payload to determine which path was called and use that to modify the response or functionality to adhere to either API spec.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.