'How to use AWS lambdas to communicate with different databases depending on customer
I have a specific case when every customer needs to have a separate database (the AWS RDS instance with MySQL). I wanted to write a backend based on lambdas. I have big trouble with how to use specific lambda with many databases with one API gateway or AppSync.
The perfect way will be:
- Frontend makes a request to API gateway or AppSync. The request header contains information about a specific customer.
- API gateway or AppSync invokes an appropriate lambda with a database environment for a specific customer.
I am aware that the above way isn't possible. Lambda can't change its environment on demand. I think about deploying one lambda many times per customer with different stages. In this approach, I need to invoke different lambda in a resolver depending on the request header containing information about a specific customer.
I'm not glad about this way, but I don't see any different eventuality to achieve my goal.
Is it possible to make it in a different and more efficient way? Maybe exists any good practices in this case.
I will be grateful for your help and advice.
Solution 1:[1]
Rather than pre-configure your Lambda function(s) with environment variables for a given database, you could instead make your Lambda functions multi-tenant by associating your various users with a given tenant and looking up the database details for that tenant from Parameter Store or Secrets Manager.
Also, be aware of RDS Proxy, which can be a big help when building multi-tenant serverless solutions (discussion here).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jarmod |
