2

I have setup a Amazon Lambda function that has different Aliases for development, test and production and an API gateway that has several stages pointing to the respective Alias versions.

Now I want to access a database table and obviously each Alias should access it's own table. In the documentation I couldn't find a possibility to pass an Alias specific configuration to my function. Is there a best practice for this?

1 Answer 1

1

Setup Stage Variables in API Gateway, and add something like the following to your mapping template(s) to pass those stage variables to the Lambda functions.

  "stage-variables" : {
    #foreach($key in $stageVariables.keySet())
      "$key" : "$util.escapeJavaScript($stageVariables.get($key))"
      #if($foreach.hasNext),#end
    #end
  }

The Lambda functions can then pull things like DB table names from the event object.

Sign up to request clarification or add additional context in comments.

4 Comments

That sounds like a good solution for functions that are exposed via API gateway. Is there a similar best practice for functions which are triggered by S3 or SNS?
Unfortunately those are more difficult. You could possibly use a DynamoDB table to store settings specific to different function aliases. Then you could check the context object to get the functionVersion value and then lookup the settings from DynamoDB.
It looks like you can also get the alias from process.env.AWS_LAMBDA_FUNCTION_VERSION
That sounds like a good idea. Instead of using a DynamoDB table I use a config file, but otherwise I use your suggestion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.