Architecting solutions using AWS Lambda means understanding many nuances in how AWS works, and a main consideration for our application architecture is the database. So we’ll need to need to know whether AWS Lambda can connect to a database at all, and what our options are.
Can AWS lambda access a database? Yes. AWS Lambda can connect to an AWS hosted databases such as RDS or DynamoDB. AWS Lambda can also connect to external databases which are public or grant network access.
Dependent on the database you’re using (or intending to use) there are some considerations you should address. Let’s now go through the different options you have for integrating a database with AWS Lambda, and address some of those considerations so you can make an informed decision.
Connect to DynamoDB
First up, let’s discuss DynamoDB. DynamoDB is a popular database choice in the world of AWS Lambda due to it’s connectionless nature (more on this later) and because it has integrations with a feature called DynamoDB streams.
The simplest way to connect your Lambda to DynamoDB is by creating a client via the AWS SDK (source). Luckily for you the AWS SDK comes pre-installed on all AWS Lambda environments ready for you to use.
In addition to directly connecting to DynamoDB with a client, AWS Lambda function can integrate with DynamoDB using streams (Source). Streams are events which DynamoDB emits when a change is made. An AWS Lambda function can then listen and perform some actions based on these events.
Connect to RDS
Our next option is connecting to RDS. A straight-forward way to connect a database with AWS Lambda is just as you would with any other application. You can add your database credentials to your lambda’s environment variables (Source) and connect to your database using it’s typical connection method.
Connect to External DB
Sometimes though, you have an existing database that exists outside of AWS. In this scenario, what can / should you do?
Similar to connecting to RDS connection, connecting to an external database can be as simple as adding credentials to your AWS Lambda and connecting as you would in a typical server application.
Be aware though: connecting to an external database requires your database is publically accessible, or your AWS Lambda is granted network access.
Using VPC Access
Another consideration when connecting to a relational database such as RDS is your networking setup. You’ll want to ensure that your AWS Lambda can see your database on the network so that it can make a connection.
If you are using AWS RDS, your database will be within a VPC network. In order to access the database you’ll have to provide your Lambda access to the VPC where your database is hosted (Source).
It’s important to note also that when granting VPC access your Lambda may experience additional cold-start time, these issues have reportedly been improved, but you may still want to investigate first (source).
Using RDS Proxy
Lastly we need to discuss the issue of relational database connections and AWS Lambda. Because of the way AWS Lambda works—it creates a new container for each new request —it’s possible that you’ll end up with many concurrent executions of your AWS Lambda (lots of Lambda running at the same time).
If you’re using a relational database that relies on connections, each of these new AWS Lambda instances will require a database connection, which can soon exhaust your database connection pool, resulting in failed requests.
AWS, however introduced a service, called RDS Proxy (Source) which helps you to prevent this issue by creating a pool of database connections that can be shared throughout your concurrent executions.
Choosing The Right Lambda DB Setup
And that’s all for today, a quick run through of the different options and considerations when connecting a database from within AWS Lambda.
If you’ve not chosen a database yet, but you’re definitely using AWS Lambda, take a look at DynamoDB. If your database is an external relational database, ensure that you can have the right networking setup to access it. And finally, if you choose AWS RDS you’ll want to establish if you require a database pool, and if you do, be sure to check out RDS Proxy.
- Open Up The Cloud Newsletter #23 (April Recap 2021) - May 6, 2021
- Cloud Software Engineering Newsletter #22 (March Recap 2021) - April 7, 2021
- Is An AWS Certification Enough To Get You A Job? (Spoiler Alert: No) - March 30, 2021