Can AWS Lambda Access A Database? And The Considerations You Should Be Taking.
Tags: serverless, aws, cloud, devops
Published: 2020-10-14 11:56:17

---
title: Can AWS Lambda Access A Database? And The Considerations You Should Be Taking.
published: true
date: 14/10/2020 12:55:00
tags: serverless, aws, cloud, devops
canonical_url: https://www.thedevcoach.co.uk/can-aws-lambda-access-database/
---

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](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/dynamodb-example-document-client.html)). 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](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.Lambda.Tutorial.html)). 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

![AWS RDS](https://i0.wp.com/www.thedevcoach.co.uk/wp-content/uploads/2020/10/rds.png?resize=760%2C475&ssl=1)

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](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html)) and connect to your database using it’s typical connection method.

But, even safer and more robust than using environment variables would be to store your credentials in [AWS Secrets Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html), or [AWS Parameter store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html).

## 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

![](https://i1.wp.com/www.thedevcoach.co.uk/wp-content/uploads/2019/09/AWS-Networking-Example.png?resize=760%2C428&ssl=1)

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](https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html)).

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](https://aws.amazon.com/blogs/compute/announcing-improved-vpc-networking-for-aws-lambda-functions/)).

 If you’re new to networking in AWS, check out: [AWS networking fundamentals: A simple guide for software engineers.](https://www.thedevcoach.co.uk/engineers-guide-aws-networking-fundamentals/) 

## 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](https://aws.amazon.com/rds/proxy/)) 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.

---

The post [Can AWS Lambda Access A Database? And The Considerations You Should Be Taking.](https://www.thedevcoach.co.uk/can-aws-lambda-access-database/) appeared first on [The Dev Coach](https://www.thedevcoach.co.uk).

Lou is the editor of [The Cloud Native Software Engineering Newsletter](https://newsletter.thedevcoach.co.uk) 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.