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:
- 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.
- Type ‘RDS’ in the AWS console search bar and click on the RDS option.
- In the RDS dashboard, scroll down and click on the ‘Create Database’ button.
- Now choose the the Database system you want to work with. For this example, we are going to configure MySQL database for our project.
- 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.
- 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.
- 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.
- 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. - 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. - 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.
- 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.
- 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:
- 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:
mysql://USER:PASSWORD@HOST:PORT/DATABASE
For example, in your case, it will be something like:mysql://user:password@production-db1.cddm7tgh3j5j.us-east-1.rds.amazonaws.com:3306/
mysql
- Copy the Connection URI and put it in your project’s env file. For example:
DATABASE_URL=mysql://user:password@production-db1.cddm7tgh3j5j.us-east-1.rds.amazonaws.com:3306/mysq
l
- 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.