1
votes

Let's assume I want to have 2 stage environments for some REST API - prod and staging, and I'm using lambda+api gateway. How to set it up correctly?

As of my point of view, both environments should use the same Lambda function, but different versions of them. Each environment should use a different configuration (pointing to the different databases, or providing a different set of features flags etc), and the best way to provide a configuration is env variables.

But stages in API gateway do not support that, the only what is there, is stage variables, and I cannot figure out, how can I use them to "feed" different configs to the same lambda.

So how is it supposed to be done?

1
We tried to use the stages for Gateway API, but IMHO it's require a lot of configuration. As the solution, we have different cloudformation stacks for prod/test/dev environments.elbik
I also see the only possibility to have a whole set of gateway+lambda for each env. But I guess it's not how it's supposed to be used, otherwise why would be these stages introduced at all?..Dmitry Malyshenko
BTW: as for usage of stage variables you could check the following article: aws.amazon.com/blogs/compute/…elbik
And you know that a lot of AWS services overlap the functionality of each other. I would suppose that the staging for gateway API was initially implemented, but it is hard to work with it. So, I would say that the best and common practice is to split the environments to different stacks.elbik
I found the article about comparison single stack vs multi stack - medium.com/hackernoon/…elbik

1 Answers

0
votes

You can specify Lambda version with aliases in its ARN when you set it in API Gateway. For example:

  • staging - arn:aws:lambda:aws-region:acct-id:function:helloworld:$LATEST
  • prod - arn:aws:lambda:aws-region:acct-id:function:helloworld:stable

And use these ARNs for different stages in API Gateway.

Also, in general, for different stages it worth having separate accounts in an AWS Organization and deploy similar or same CloudFormation stack to these accounts in your integration pipeline.