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.
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.
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.
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.
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).
If you’re new to networking in AWS, check out: AWS networking fundamentals: A simple guide for software engineers.
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.
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.
The post Can AWS Lambda Access A Database? And The Considerations You Should Be Taking. appeared first on The Dev Coach.
Lou is the editor of The Cloud Native Software Engineering Newsletter a Newsletter dedicated to making Cloud Software Engineering more accessible and easy to understand. Every month you’ll get a digest of the best content for Cloud Native Software Engineers right in your inbox.