2

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 Answer 1

2

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.

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

4 Comments

Thank you for your response. What would you use to make this lookup then? Where would you store the map between the URL of the API gateway and databases names?
I suspect that depends a bit on how much you're looking to update it. If it's relatively infrequent update you might just store it in code. If not S3 (you can have a JSON file you read and cache), or Dynamo (key is execute-api id) are both contenters. Dynamo has an edge in that it's a bit faster (precise lookup) and presents a reasonable UI for editing in the AWS console.
Hi, just in case someone gets on this question some day: aws.amazon.com/blogs/compute/… stage variables allow passing variables from api gateway to lambda. It looks much more clean to me :) Credit to a redditer for sending me this link
How can i implement multiple API gateway with single Lambda using serverless, one gateway being Private and other being edge type?

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.