Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to point AWS API gateway stage to specific lambda function alias?

So as per the AWS documentaion

Instead of using Amazon Resource Names (ARNs) for Lambda function in event source mappings, you can use an alias ARN. This approach means that you don't need to update your event source mappings when you promote a new version or roll back to a previous version.

I have AWS lambda function pets and i have created 2 aliases dev and prod pointing to different versions of lambda function.

Then in API Gateway i am using this lambda function in Integration Request. I have two stages of API, development and production. I want development API stage point to dev Lambda alias ARN and production needs to point to prod alias.

When i select lambda function as Integration Type then drop down list shows whatever display name i have given earlier while creating lambda function..enter image description here

I am not finding any stage specific configuration for lambda function. Based on my research on SO i have to follow these steps to deploy development stage pointing to dev alias

1> Go to Integration Request
2> Select Lambda function and change it to pets:dev
3> Deploy to development stage

Follow the same steps for production by changing Lambda function to pets:prod before deployment.

This is going to be maintenance nightmare as our lambda function grows. Is there any easier way to do it?

like image 583
LP13 Avatar asked Jan 02 '19 17:01

LP13


People also ask

How do I specify Lambda alias in API gateway?

Managing aliases with the Lambda API To create an alias using the AWS Command Line Interface (AWS CLI), use the create-alias command. To change an alias to point a new version of the function, use the update-alias command. To delete an alias, use the delete-alias command.


1 Answers

I found it
https://aws.amazon.com/blogs/compute/using-api-gateway-stage-variables-to-manage-lambda-functions/

Here are the steps I followed:

  1. After creating lambda function, create 2 aliases for lambda function. dev pointing to $latest version and prod pointing a particular version you want to use in prod
  2. Then goto API Gateway console ->Integration Request ->Lambda function, and type pets:${stageVariables.lambdaAlias} where pets is my function name and lambdaAlias is the stage variable that we will have to add in each API stage
  3. Deploy your API to new API stages development and production
  4. In each API stage add stage variable lambdaAlias with value dev and prod respectively. The stage variable value must match with lambda function's alias

enter image description here

Now we don't have to keep changing lambda alias name for each API deployment

like image 139
LP13 Avatar answered Oct 30 '22 05:10

LP13