Multiple API Gateway instances, one lambda function

Viewed 1004

I am trying to build a system where multiple APIs gateway instances should execute the same lambda function.

My problem is that I would like to change only the lambda configuration in function of the API gateway used.

Let's take the name of a database as an example that should change if the lambda is being triggered from one API or the other.

Example:

API Gateways:
https://my-first-api-gateway.execute-api.eu-west-1.amazonaws.com
https://my-second-api-gateway.execute-api.eu-west-1.amazonaws.com

I then have one lambda function being called by the two APIs: say_hello.

This function has to retrieve a quote from a database. If the function has been called from my-first-api-gateway the lambda function has to use my_first_database and if the function has been called from my-second-api-gateway, it has to use my_second_database.

The only solution I came with is to deploy as many lambda functions as I have API Gateways. And then use environment variables to store my database name.

I don't like my solution because when I update a single line of code I'll have to redeploy all of my lambda functions.. (if I have 300 different databases to use, that would mean to update 300 lambda functions at once..)

Thank you for your ideas on this subject!

1 Answers

If you use a Lambda-Proxy integration you should be fine.

Full details of the event can be found here, however critically the headers Host, origin and Referer all point the the API Gateway uri:

"headers": {
  "Host": "j3ap25j034.execute-api.eu-west-2.amazonaws.com",
  "origin": "https://j3ap25j034.execute-api.eu-west-2.amazonaws.com",
  "Referer": "https://j3ap25j034.execute-api.eu-west-2.amazonaws.com/dev/"
}

Therefore it should be fairly trivial for you to branch/look-up your database behaviour from this information.

Related