How to use AWS RDS Database with your Nextjs Prisma app

As a Nextjs app developer, you must have thought about figuring out the best deployment strategy for your Nextjs app at some point. There are many options if you want to deploy your app to production. You can either choose to go self-hosting mode, where you can just purchase a shared or dedicated VPS and have full control over your app infrastructure. Or, you can opt for server-less / cloud deployment where most of the operations are managed by the Cloud hosting provider.

Of course, both options have their own pros and cons. For example, if you host the app on your own VPS, you will have to manage everything, the resources, database replications, backups, security, CI/CD etc. The advantage is having full control over the architecture and cost optimization.

Whereas cloud deployments can be expensive. Once you reach a certain threshold, you bills start stacking up. It’s a good option for users who are not infrastructure wizards and only want to be responsible for the codebase of the application.

Which Database to choose for your Nextjs App?

There are a few options if you want to host your database on the cloud. If we talk about relational databases, I personally liked PlanetScale before they ended their free-tier. Then there’s Neon Tech which is gaining some popularity. Some users have reported positive feedback with Supabase.

However, my personal favorite for now is AWS RDS database. AWS has a generous free-tier if you want to start small and it can scale very well. It would also be a good option if you choose to deploy your app to AWS infrastructure in the future.

Now, we’ll see how we can configure and integrate AWS RDS with our Nextjs Prisma app.

How to create an AWS RDS Database

Follow these step-by-step instructions on how to create and configure AWS RDS MySQL Database for your app:

  1. Login to your Amazon Web Services console using your AWS credentials. If you don’t have an account, you can easily create an AWS account in a matter of minutes.
  2. Type ‘RDS’ in the AWS console search bar and click on the RDS option.
  3. In the RDS dashboard, scroll down and click on the ‘Create Database’ button.
  4. Now choose the the Database system you want to work with. For this example, we are going to configure MySQL database for our project.
  5. In the templates section, you can choose the ‘Free Tier’ if you are just experimenting with small projects. If you want full performance benefits then choose the ‘Production’ option.
  6. In the Settings section, choose a database name that you want for your project. Also, choose your database master username and password carefully. The password should fulfill all of the complexity requirements.
  7. In the Storage section, choose the storage options that suit the needs of your app. If you want to start with the basic options then go with the default General Purpose SSD with 20 GB storage.
  8. The Connectivity section is of super importance while configuring your Database. You need to carefully plan the VPC settings depending upon the network architecture you want to have for your AWS services.
    As a rule of thumb, it’s always a good practice to create a new VPC. Deploying to the default VPC is not recommended because your database will be impacted by any changes you make to the default networking environment.
  9. While configuring VPC, you need to decide if you want to expose your database to the Public Internet. For example, if your app is hosting on another platform or outside the current VPC then you might have to enable public access to access your database from the app.
    If your app is hosted on the same VPC then you can opt for private access. This way your database will not be exposed to the internet.
  10. You can also create your own VPC Security group to manage the security settings for your database. For example, if you want it so that your database cannot be accessed except from a select ip range then you can configure your VPC security group like so.
  11. The next step is to setup your Database Authentication. You can either go with your a new custom username / password or use the IAM user password.
  12. Once you are done with configuring the database settings, you can now click on the Create Database button to create your new RDS database.

Steps for Connecting RDS database to your Nextjs App

Considering that you have you Nextjs app setup with Prisma ORM. Here are the steps you need to follow in order to connect your AWS RDS MySQL database to your app:

  1. The first step is to construct a connection URI for your new MySQL database. You will need to add the connection URL to your env file in your Next app. Here’s the format for the URI:
    For example, in your case, it will be something like:
  2. Copy the Connection URI and put it in your project’s env file. For example:
  3. The final step is to go your schema.prisma file and add the DATABASE_URI as a Data Source like so:
    datasource db {
    provider = "mysql"
    url = env("DATABASE_URL")
    relationMode = "prisma"

Now all you need to do is to restart your Nextjs app server and test the connection. If you have done everything according to this guide, the connection should be successful. Otherwise you might have to re-check your database configuration and the Database URL.

Note: If you are getting this error while trying to connect to your database via Prisma client:
P1001: Can’t reach database server

The reason might be that you haven’t allowed your IP address in the VPC security group Inbound traffic settings.

Spread the Word

You May Also Like

About the Author: Umair

A self-learned Javascript developer specializing in Frontend and Backend frameworks including React.js, Redux, Node.js, Express, MongoDB. He has extensive industry experience as a Tech Support lead and System Administrator. Currently learning Web3, (Solidity, Hardhat, Ethers.js) Smart contracts development, testing and auditing.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.