2

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

fudo
  • 141
  • 1
  • 6
  • 1
    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." – Purefan Jan 09 '23 at 11:53
  • @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 Jan 09 '23 at 13:52
  • @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. – fudo Jan 09 '23 at 14:02
  • 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 :) – Purefan Jan 10 '23 at 09:58
  • 1
    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 ;) – fudo Jan 11 '23 at 13:54
  • anytime :) glhf – Purefan Jan 12 '23 at 12:43

1 Answers1

1

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

Purefan
  • 185
  • 7