47

I created a resource in Amazon's ApiGateway. It is pointing to a Lambda function. This is being hit by a native mobile application (android and ios) which is already in the wild.

I now want to modify the Lambda function, but I see no way to change my ApiGateway resource to point to an alias of the lambda. This is my first time playing with any of these technologies and I see no easy mechanism to manage this in the aws console.

How can I modify my ApiGateway resource to point to my lambda alias so I can edit trunk without affecting existing clients?

digitaljoel
  • 26,265
  • 15
  • 89
  • 115

4 Answers4

57

Under Integration Type -> Lambda Function you need to add a reference to the stage variable MyLambdaFuntionName:${stageVariables.lambdaAlias} and then for each stage set the lambdaAlias in the Stage Variables tab accordingly(lambdaAlias=dev, lambdaAlias=prod, etc.)

There is an example with screenshots here: https://aws.amazon.com/blogs/compute/using-api-gateway-stage-variables-to-manage-lambda-functions/

Its kind of hidden towards the very bottom of the page starting with "Alternatively, you can mix and match static names"

Ryan
  • 5,845
  • 32
  • 27
  • 7
    Note that when specifying a Lambda function name as a stage variable value, you must configure the permissions on the Lambda function manually. See also http://docs.aws.amazon.com/apigateway/latest/developerguide/stage-variables.html – MikeD at AWS Feb 18 '16 at 20:21
  • Thanks Ryan, I'll have a look at it this weekend and accept this answer when I get it going. – digitaljoel Feb 19 '16 at 17:10
  • 1
    This doesn't work for me, If I do it with just `MyLambdaFunctionName` it works, but get 500 error if i do `MyLambdaFuntionName:${stageVariables.lambdaAlias}` – Ray Oct 11 '18 at 20:13
  • 5
    Hi @Ray , don't forget to give the API method permission, just input `yourlambda:DEV` ... and the rest envs , apply them one by one, and finally you cloud just apply`yourlambda:${stageVariables.lambdaAlias}` – John Jang Feb 21 '19 at 07:00
  • If you have many lambda functions, you can use 'https://stackoverflow.com/a/59426777/4160530' script to speed up the 'add permission' process. – Bilal Ahmad Dec 20 '19 at 14:28
  • 3
    How can I achieve this using CloudFormation considering I am using AWS::Serverless::Function to create the lambda ? – Raghav Mar 27 '22 at 18:26
8

For the later googler, be careful to add permissions WITH the correct alias like yourfunc:prod not only yourfunc. That means if you'r planning to use 3 alias to invoke the lambda functions, you have to add 3 of them.

davyzhang
  • 2,419
  • 3
  • 26
  • 34
  • The great thing is the policy sticks with the alias so as you point the alias to other versions of the lambda function, it'll just work. – Tom Saleeba Apr 24 '17 at 07:49
6

On the Api Gateway console, use ARN instead of a lambda function name.

Jehong Ahn
  • 1,872
  • 1
  • 19
  • 25
  • 1
    This was the simplest solution for me... Publish a version of the Lambda function with a name like "prod" and then copy/paste it's ARN into the API Gateway's Integration value. Done. – RumbleFish Dec 29 '22 at 05:15
2

For my case, I would add directly the arn of the lambda function to replace the Lambda name. This works for me without have to to add the {} notation.

sothye
  • 21
  • 2