Tall Paul
Tall Paul

Reputation: 2450

How to use Stage Variables in API gateway to point to different lambda functions with the serverless framework

I have been trying to automate my serverless deployment and found this article about how you can use stage variables to point to different functions. I thought this is great and would make stages useful. However I cannot find any documentation on it from serverless.com and was wondering how others have tackled this issue. Our back up plan is to change the path it is deployed to instead of using stages.

Upvotes: 2

Views: 728

Answers (1)

Thales Minussi
Thales Minussi

Reputation: 7235

Probably not the answer you're looking for, but the guys at Serverless.com don't recommend using API Gateway's built-in stages as it can get very messy really quick. I have tried to find where they stated it once but I couldn't find it now (but it was surely inside their forums).

Since I have gone (likely) the same path as you I ended up giving up about using API Gateway's built-in environments and used Serverless's environments instead. This means that you'll create N Api Gateways and N functions, where N represents one stage. On my case, it's always /dev, /test and /prod, but you may have more.

I can't recommend you enough to read this Chapter: Stages in Serverless Framework to get more acquainted with Stages and see if you want to go down the same road as I did.

Long story short: using API Gateway's built-in stages is harder to manage because one can easily screw up IAM roles, forget to change the path in the URI (/dev, /test/, /prod). Using Serverless Framework's stages is much easier because you can deploy different environments in different accounts, easier to manage IAM roles and it's very unlikely one will mess up with the URLs themselves, since you'd have a different URL for every stage. You can also set stage-specific endpoints if you want to (/metrics, /logs). This list goes on and on, but you get the idea over the flexibility you gain by deploying every stage independently.

Upvotes: 1

Related Questions