10
votes

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?

1
Are you using IaC tools like cloudformation or terraform? If so I don't think it's a huge pain if you have a unified naming convention.congbaoguier
I am using AWS console. Not sure about the tools you mentioned but I will look into itLP13
Generally terraform is better choice for managing you own infrastructure. If your infrastructure needs to be deployed to different aws accounts from you account, cloudformation is better.congbaoguier
Will IaC allows you to do 1 to 1 mapping between API stages and Lambda Alias?LP13
Have you looked at serverless?Imran

1 Answers

22
votes

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