Securing database connections is a critical part of building production applications. Traditional methods often involve managing long-lived database credentials, which increases the risk of credential leaks. Vercel's AWS integration simplifies this by using OIDC Federation and RDS IAM authentication, allowing your Next.js application to connect to Aurora PostgreSQL without hardcoded access tokens or passwords.
This guide shows you how to set up a secure connection, implement database queries, and fetch data in a Next.js Server Component.
By the end of this guide, you will be able to:
- Connect a Next.js application to AWS Aurora PostgreSQL securely
- Use Vercel OIDC federation to authenticate with AWS
- Implement RDS IAM authentication for database access without passwords
- Fetch and display data from your database using Server Components
Understanding the security flow helps you manage your database connections effectively.
Instead of using a static username and password, this approach uses short-lived tokens:
- OIDC federation: Vercel provides an OIDC token to your serverless functions. This token identifies the Vercel project and environment.
- AWS role assumption: Your AWS IAM role is configured to trust Vercel's OIDC provider. When your function runs, it exchanges the Vercel OIDC token for temporary AWS credentials.
- RDS IAM authentication: Using these temporary credentials, your application generates an auth token specifically for Aurora PostgreSQL. This token acts as a temporary password to connect to the database.
This multi-layered approach ensures that no long-lived secrets are stored in your environment variables, significantly reducing your security surface area.
Before you begin, make sure you have:
- A Vercel account
- Vercel CLI installed (
npm i -g vercel) - Node.js 18 or later installed locally
Before connecting your application, you need a running Aurora PostgreSQL cluster.
- Go to the Vercel Marketplace AWS integration and select install for the Amazon Aurora PostgreSQL product
- Follow the setup wizard to provision a new Aurora PostgreSQL database
- The integration automatically configures IAM authentication and creates the necessary roles
The Vercel Marketplace handles the complex AWS setup, including security groups, IAM roles, and the OIDC trust relationship.
To develop locally with the same secure connection, link your project to Vercel and pull the environment variables.
Select your team and the project connected to the AWS integration.
This creates a .env.local file with the required variables:
AWS_ROLE_ARN- The IAM role your application assumesAWS_REGION- The AWS region of your Aurora clusterPGHOST- The Aurora cluster endpointPGPORT- The database port (typically 5432)PGUSER- The database userPGDATABASE- The database nameVERCEL_OIDC_TOKEN- The OIDC token for local development
Aurora Serverless is fully PostgreSQL compatible. Install the required packages:
Create a lib/db/db.ts file to handle the connection pool and IAM authentication:
The dotenv import ensures environment variables are loaded when running scripts locally. The attachDatabasePool utility optimizes connection management in serverless environments.
Create a test script to verify your database connection works before building features.
Create lib/db/test-connection.ts:
Add the script to package.json:
Run the test:
You should see output confirming a successful connection to your Aurora database.
Create a setup script to initialize your database schema and seed data.
Create lib/db/setup.ts:
Add the script to package.json:
Run the setup:
Now you can use the query function to fetch data directly in your Next.js Server Components.
In lib/db/queries.ts, implement a function to fetch movies:
In app/page.tsx, fetch and render the movies:
The dynamic = "force-dynamic" export prevents Next.js from attempting to statically generate the page at build time, which would fail without database access.
Start the development server:
Visit http://localhost:3000 to see your movies displayed from the Aurora PostgreSQL database.
To ensure your application remains secure and performant:
- Use
attachDatabasePool: This utility from@vercel/functionshelps manage database connections efficiently across multiple serverless function invocations, preventing connection exhaustion. - Use
withConnectionfor transactions: When running multiple queries that need to be atomic, use thewithConnectionhelper to ensure they run on the same client. - Set Proper SSL: Always use
ssl: { rejectUnauthorized: false }(or provide the RDS CA certificate) when connecting to AWS RDS to ensure the connection is encrypted. - Principle of Least Privilege: Ensure the IAM role assumed by your Vercel project only has the
rds-db:connectpermission for the specific database user and resource.
You have successfully connected a Next.js application to AWS Aurora PostgreSQL using Vercel's secure OIDC Federation and RDS IAM authentication. This setup eliminates the need for long-lived database passwords, providing a more secure and maintainable architecture. By using Server Components and Vercel's database utilities, you've built a performant data-fetching layer that scales automatically with your application.
- Explore the demo: View the live demo or explore the source code
- Deep dive into OIDC: Learn more about OIDC Federation on Vercel
- Database management: Set up migrations with Drizzle or Prisma
- Performance monitoring: Use Vercel Monitoring to track database query performance in production