Deploy Next.js to AWS Amplify
How to deploy your Next.js Server-Side Rendered (SSR) websites to AWS Amplify.
How to deploy your Next.js Server-Side Rendered (SSR) websites to AWS Amplify.
In this guide, you will learn how to deploy a Next.js SSR project to AWS Amplify. You will go through the process of setting up a new Next.js project, enabling server-side rendering using the AWS Amplify adapter, and finally deploying it to AWS Amplify.
High Quality Starter Kits with built-in authentication flow (Auth.js), object uploads (AWS, Clouflare R2, Firebase Storage, Supabase Storage), integrated payments (Stripe, LemonSqueezy), email verification flow (Resend, Postmark, Sendgrid), and much more. Compatible with any database (Redis, Postgres, MongoDB, SQLite, Firestore).
Get all 3 kits$147$99One-time license · Lifetime updates
You’ll need the following:
Let’s get started by creating a new Next.js project. Open your terminal and run the following command:
npx create-next-app@latest my-appWhen prompted, choose:
Yes when prompted to use TypeScript.No when prompted to use ESLint.Yes when prompted to use Tailwind CSS.No when prompted to use src/ directory.Yes when prompted to use App Router.No when prompted to customize the default import alias (@/*).Once that is done, move into the project directory and start the app in development mode by executing the following command:
cd my-appnpm run devThe app should be running on localhost:3000.
Then, you’ll need to make the following change to your next.config.js file. Open the file and add the following code:
module.exports = { output: 'standalone',}Then, create an amplify.mjs file at the root of the repository with the following code:
import { join } from 'node:path';import { writeFileSync, mkdirSync, existsSync, cpSync, rmSync } from 'node:fs';
// Define all the Amplify related directoriesconst amplifyDirectories = [ join(process.cwd(), '.amplify-hosting'), join(process.cwd(), '.amplify-hosting', 'static'), join(process.cwd(), '.amplify-hosting', 'static', '_next'), join(process.cwd(), '.amplify-hosting', 'compute'), join(process.cwd(), '.amplify-hosting', 'compute', 'default'), join(process.cwd(), '.amplify-hosting', 'compute', 'default', 'node_modules'),]
// Create directories if they do no exist alreadyif (existsSync(amplifyDirectories[0])) rmSync(amplifyDirectories[0], { force: true, recursive: true })
// Create directories if they do no exist alreadyamplifyDirectories.forEach((i => mkdirSync(i)))
// A general default configuration to fallback to compute if no matching static assets foundconst deployManifestConfig = { version: 1, routes: [ { path: `/assets/*`, target: { kind: "Static", }, }, { path: `/*.*`, target: { kind: "Static", }, fallback: { kind: "Compute", src: "default", }, }, { path: "/*", target: { kind: "Compute", src: "default", }, }, ], computeResources: [ { name: "default", entrypoint: "server.js", runtime: "nodejs18.x", }, ], framework: { name: "next", version: "13.5.6", },};
// Write the config to .amplify-hosting/deploy-manifest.jsonwriteFileSync( join(process.cwd(), ".amplify-hosting", "deploy-manifest.json"), JSON.stringify(deployManifestConfig),);
// Copy the static assets generated in .next/static and public to .amplify-hosting/static directorycpSync(join(process.cwd(), 'public'), amplifyDirectories[1], { recursive: true })cpSync(join(process.cwd(), '.next', 'static'), amplifyDirectories[2], { recursive: true })
// Copy the static assets generated in .next/standalone to .amplify-hosting/compute directorycpSync(join(process.cwd(), '.next', 'standalone'), amplifyDirectories[4], { recursive: true })
// Remove .next/static and public from .amplify-hosting/compute/defaultrmSync(join(amplifyDirectories[4], '.next', 'static'), { force: true, recursive: true })rmSync(join(amplifyDirectories[4], 'public'), { force: true, recursive: true })Then, create an amplify.yml file at the root of the repository with the following code:
version: 1frontend: phases: preBuild: commands: - npm ci build: commands: - env >> .env - npm run build - node amplify.mjs artifacts: baseDirectory: .next files: - '**/*' cache: paths: - node_modules/**/*The code above does the following:
preBuild commands to install the dependencies of your Next.js project.build commands to:
.env file at the root of the project.node_modules directory and .env file to Amplify’s compute directory.The code is now ready to deploy to AWS Amplify. Use the following steps to deploy:
Start by creating a GitHub repository containing your app’s code.
Then, navigate to the AWS Amplify Dashboard and click on Get Started under the Host your web app section.
.env file, and PORT as 3000. Click Next to proceed.
Yay! You now have a Next.js project that automatically deploys to AWS Amplify upon Git push.
If you have any questions or comments, feel free to reach out to me on Twitter.
Learn how to deploy a server-side rendered Angular 20+ application to Cloudflare Workers, serving static assets from the edge and running SSR.
LaunchFast works with Backblaze B2 out of the box. Same STORAGE_PROVIDER=s3, same pre-signed upload flow. Point AWS_ENDPOINT_URL_S3 at your regional B2 S3 endpoint and add your application key.
LaunchFast works with Tigris out of the box. Same STORAGE_PROVIDER=s3, same pre-signed upload flow. Point AWS_ENDPOINT_URL_S3 at https://fly.storage.tigris.dev and add your credentials.