AWS – How to Use Lambda Versions with Different Gateway Stages

amazon-api-gatewayamazon-lambdaamazon-web-servicesdeploymentversioning

Coming from a "classic/standard" development I'm used to have different deployment stages, e.g. staging and production, each one with its application version.

AWS Lambda functions and API Gateway are freaking me out on how to handle versions and releases, and maybe it is because I'm trying to do it in the non correct way it's meant to be on AWS.

Lambda functions can have both versions and aliases while gateways can have stages, so I'm expecting to be able to have an alias pointing to the production version of the function and an alias pointing to the staging version, and have each one being called on the same gateway resource from the two stages, e.g.:

enter image description here

But it seems to be that api gateway stages cannot call different lambda function alias nor versions, the only things I can override in a stage resource are just CloudWatch and throttling settings.

Probably I'm missing something about this aspect of development on AWS, can anyone point me in the right direction?

Just to be clear, I'm approaching AWS development with vscode and aws/sam cli, anything that needs to be done by web interface looks wrong to me, even if I'm aware that there may be technical/logical/business constraints to have it done that way, so I'd prefer answers that involve code generation or cli commands if possible.

Update #1

As @Purefan suggested in his comment, Working with stages for HTTP APIs section of the docs explain how to use stages variables to reference different lambda versions/aliases on each gateway stage, the problem is that I couldn't create my staging function alias pointing it to the latest version from sam cli:

aws lambda create-alias --function-name test-hello-world --name staging --function-version $LATEST 

usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help

aws: error: argument --function-version: expected one argument

I did fallback to creating it from web console, but now seems that any sam deploy from cli overwrite the alias reference (function name + stage variable) in the gateway resource, probably due the missing of this configuration in my local 'template.yaml'.

Best Answer

Glad it helped :)

To API Gateway a reference to a lambda is a string and we can use variables in that string to point to different. Conveniently, API Gateway supports Stage variables, so you can define a variable in the stage "production" that would point to a lambda version called "production", another stage could have the same variable pointing to another lambda or another lambda version.

More information can be found here