Score:2

AWS - lambda versions to different gateway stages?

mx flag

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'.

Purefan avatar
in flag
have you read https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-stages.html ? "You can use a stage variable in place of a Lambda function name or alias, as shown in the following examples."
fudo avatar
mx flag
@Purefan thanks, it helped me a bit, but when syncing or deploying from cli the gateway resource integration request is being overwritten, probably due the missing of these configuration in my `template.yaml`, I'll update my question right now.
fudo avatar
mx flag
@Purefan your comment actually solved my question on "how call two aliases in a gateway stages" since my problem is now shifted to the persistence of this configuration in my local `template.yaml`, so you can write it down as an answer.
Purefan avatar
in flag
I think maybe SAM templates dont have all the functionality that you need, for comparison cloudformation templates do allow you to define aliases but the closest sam offers is the autopublishalias. Im passionate about cloudformation so if you want to give that a try I'd be willing to help :)
fudo avatar
mx flag
That'd be great, but I've just started with aws sam and my actual level of knowledge about cloudformation templates is below zero, so just give me some time to gain confidence with sam (for work) and probably I'll call you back, but thanks man ;)
Purefan avatar
in flag
anytime :) glhf
Score:1
in flag

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

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.