I'm building a serverless app using API Gateway and Lambda (Serverless Framework) and trying to find a way to provide multiple versions of our app's API.
Here's the way I can think of.
serverless.yml
handler: list.handler
events:
- http:
path: {ver}/list
method: get
cors: true
authorizer: aws_iam
list.js
export async function handler(event, context, callback) {
const ver = event.pathParameters.ver;
if (ver >= '1.0') {
return fooUtil.getNo(ver);
} else {
return 1;
}
}
fooUtil.js
export function getNo(ver) {
if (ver >= 1.3) {
return 3;
} else {
return 2;
}
}
However, I need to pass "ver" parameter to all functions this way.
Is there any way easier (and testable) to fetch version no from request like below?
fooUtil.js
export function getNo() {
if (session.getValue('ver') >= 1.3) {
}
}
I prefer not to divide repositories or git branches to manage multiple versions.
What is about to have for each version its own resource and separate it by folders?
Like this
handler: v1.list.handler
events:
- http:
path: v1/list
method: get
cors: true
authorizer: aws_iam
handler: v2.list.handler
events:
- http:
path: v2/list
method: get
cors: true
authorizer: aws_iam
That gives you the flexibility to test everything and its easier for newcomers to your project because the versioning is explicit through folder separation.
After reading many sources and trying to understand how AWS API Gateway works (from the versioning perspective), I decided to simply duplicate the API on my major versions and keep track on them through diff branches. The benefits:
I am using serverless-custom-domain which creates a recordset during my deployment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With