Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Deploy a Next.js App to AWS Amplify
AWS profile imageAli Spittel
Ali Spittel forAWS

Posted on • Originally published atwelearncode.com on

     

Deploy a Next.js App to AWS Amplify

June 27, 2024: This blog post uses Amplify Gen 1, if you're starting a new Amplify app I recommend trying outGen 2!

AWS Amplify just announced server-side rendering deployment support for Next.js! Here's a quick guide on how to deploy both anSSR and an SSG Next.js app.

Note: if you're new to Next.js check outthis tutorial!

Please note that I work as a Developer Advocate on the AWS Amplify team, if you have any feedback or questions about it, please reach out to me or ask on our discord - discord.gg/amplify!

SSG

For a statically generated Next.js app, you'll first need to edit yourpackage.json file. You'll need to change yourbuild tonext build && next export instead of justnext build.

"scripts": {  "dev": "next dev",+ "build": "next build && next export",  "start": "next start"},
Enter fullscreen modeExit fullscreen mode

SSR

You don't need to change anything in yourpackage.json for a server-side rendered app! Just keep the one that was generated bycreate-next-app.

Hybrid SSG + SSR

If you have an app with both SSR and SSG pages, also keep the defaultpackage.json, same as a fully SSR app!

For Both

Then, create a repository on your git provider of choice, and push your code to it.

  1. Create an AWS account if you don't already have one.

  2. Navigate tothe Amplify Console

  3. Click on the orangeconnect app button.

  4. ChooseGitHub in theFrom your existing code menu, and click continue

Amplify interface with different remotes

  1. Type in the name of your GitHub repo you just created (it should autofill!) and then clicknext

Amplify interface with name of repo

  1. The build settings will auto-populate, and so you can just clicknext on theConfigure build settings
  2. ClickSave and deploy.

Pricing

Behind the scenes, Amplify creates AWS resources used to deploy your app -- first anAmazon S3 bucket to store your app's static assets, then anAmazon CloudFront to serve your app itself, finally aLambda@Edge function to SSR pages. The links to each service in this paragraph leads to info about the pricing for it.

Multi-branch Deployments

In order to deploy multiple branches to AWS Amplify, you can click the orange "Connect branch" button on theAmplify Console page for your app. So, if you want to test deployments of features before they go live to themain branch, you can do so in clicks!

The connect branch button on the AWS Amplify Console

Pull Request Previews

You can also enable automatic pull request preview deployments. This will enable Amplify to deploy a preview of each pull request to a project so that you can click a link and see what the pull request does to the site!

First click onpreviews on the left side bar.

Enable previews in the AWS Amplify Console

Then, clickEnable previews.

Enable pull request previews in aws amplify

Add a Custom Domain

You can also connect your domain name to your site by visitingDomain management and thenadd domain -- you'll see instructions for different domain providers or be able to buy one through Amazon Route53.

Conclusion

These are just some of the things you can do when you deploy an app to AWS Amplify Hosting! There are also ways to add testing, monitoring, custom headers, access control and more. I hope this guide was helpful for those of you looking to deploy a Next.js app to Amplify.

Top comments(17)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
marcostreng profile image
Marco Streng
❤️ coding and cooking
  • Location
    Bavaria, Germany
  • Work
    codecentric AG
  • Joined
• Edited on• Edited

Great article, thanks!

I've just created a new app from the Next.js starter and added a page with some server-side data fetching (SSR).
Build and deployment are running successfully. Amplify detects the framework correct with "Next.js SSR". Also the Lambda@Edge exists after the deployment. But there is no trigger connected as far as I can see.

But when I navigate to the SSR Page in the browser I always receive a "503 ERROR
The request could not be satisfied."

Maybe anyone got an idea?

CollapseExpand
 
aspittel profile image
Ali Spittel
Passionate about education, Python, JavaScript, and code art.
  • Email
  • Location
    Denver, CO
  • Education
    Hamilton College
  • Work
    Developer things at AWS
  • Joined

Hey, thanks for bubbling this up! The engineering team is checking into it and I'll let you know when I hear back!

CollapseExpand
 
marcostreng profile image
Marco Streng
❤️ coding and cooking
  • Location
    Bavaria, Germany
  • Work
    codecentric AG
  • Joined
• Edited on• Edited

After some investigation I found out that I have to set webpack to version 4 in my next.config.js.
The latest Next.js uses webpack 5 as default which is not working with AWS Amplify Hosting right now.

Thread Thread
 
sohammondal profile image
Soham Mondal
Passionate about coding. Currently looking for work in Node.js Backend Roles.
  • Location
    Kolkata, India
  • Education
    Techno India College of Technology
  • Work
    Software Engineer at Openhouse
  • Joined

Yes, you're right. Downgrading Next.js to v11.1.3 and settingwebpack5: false in next.config.js fixed the issue.

CollapseExpand
 
alacret profile image
Angel Lacret
  • Work
    CEO
  • Joined
CollapseExpand
 
ianwijma profile image
Ian Wijma
Software Developer | Tinkerer | Linux | Gamer
  • Location
    Canberra
  • Education
    Mostly self thought with some basic development education
  • Pronouns
    He/him
  • Work
    Software Developer at Atlassian
  • Joined

I've had issue with prediction not working in the NextJS api routes. Where in the end I used LAMDA functions instead of API routes. I also number into other hurdles with Amplify. Love the idea. But not 100% sure about using it in my next big project.

CollapseExpand
 
aspittel profile image
Ali Spittel
Passionate about education, Python, JavaScript, and code art.
  • Email
  • Location
    Denver, CO
  • Education
    Hamilton College
  • Work
    Developer things at AWS
  • Joined

Hey! Thanks for the reply -- I just chatted with the engineers and for now SSR only works with Data and Auth not predictions yet. It could be a good thing to create an issue for to show the team you'd be interested in that as a feature!github.com/aws-amplify/amplify-js/...

CollapseExpand
 
ninan_phillip profile image
Phillip Ninan
I'm a software engineer specialized in Docker, AWS, software architecture, CI/CD, and backend development for complex scalable web apps.
  • Location
    Long Island, NY
  • Education
    Stony Brook University
  • Work
    Senior Software Engineer at LBi Software
  • Joined

Great read!

I just followed this and got a demo app deployed. It may be worth noting that you need to add a role to the app. I created a demo without one and it didn't seem to work. It said it was deployed, but the link did not work and I did not see an S3 bucket created.

I created a second one and created a new role on the create app screen and it worked!

Unfortunately, it looks like the Image component is not working. I'm pretty new to Next.js and I'm using SSR. I'm not sure if that is something that's not supported? 🤔

Thank you!!

CollapseExpand
 
aspittel profile image
Ali Spittel
Passionate about education, Python, JavaScript, and code art.
  • Email
  • Location
    Denver, CO
  • Education
    Hamilton College
  • Work
    Developer things at AWS
  • Joined

Hey! Thanks for the feedback, the Image component was Next 10, and we're supporting the Next 9 features for now. Have let the product and engineering teams know that this feature is requested though!

CollapseExpand
 
ninan_phillip profile image
Phillip Ninan
I'm a software engineer specialized in Docker, AWS, software architecture, CI/CD, and backend development for complex scalable web apps.
  • Location
    Long Island, NY
  • Education
    Stony Brook University
  • Work
    Senior Software Engineer at LBi Software
  • Joined

I wish I could like this more than once! 😂

Thank you Ali!

CollapseExpand
 
fwang profile image
Info Comment hidden by post author - thread only accessible via permalink
Frank Wang
CTO at Seed.run | Author of Serverless-Stack.com
  • Location
    San Francisco ⇆ Toronto
  • Work
    CTO at SEED
  • Joined

Hey Ali, great article.

We also receive a lot of requests in theSST community for deploying Next.js apps to AWS. And I just wanted to share that we created a SST construct (backed by CDK) to help with building and deploying Next.js apps to your AWS account. It uses a similar architecture behind the scene S3, CloudFront, Lambda@Edge, and Route53 for domains. Next.js 11 compatible with ISR and image optimization support.

Here's an exampleserverless-stack.com/examples/how-...

Comment hidden by post author
CollapseExpand
 
larsejaas profile image
Lars Ejaas
I love coding and what motivates me is problem-solving and preferably if it has an element of creativity. I am a self-taught developer and work full-time as a front-end developer.
  • Location
    Denmark 🇩🇰
  • Education
    Bachelor in Nutrition and health (I know not super relevant to my current line of work!)
  • Work
    Frontend developer at AccuRanker
  • Joined

It would be nice with some level of rollback functionality like Netlify. It just gives you so much mire peace in mind when updating a page. Otherwise AWS Amplify looks great!

CollapseExpand
 
aspittel profile image
Ali Spittel
Passionate about education, Python, JavaScript, and code art.
  • Email
  • Location
    Denver, CO
  • Education
    Hamilton College
  • Work
    Developer things at AWS
  • Joined

Hey! Thanks for this feature request, I passed it on to the engineering and product teams!

CollapseExpand
 
larsejaas profile image
Lars Ejaas
I love coding and what motivates me is problem-solving and preferably if it has an element of creativity. I am a self-taught developer and work full-time as a front-end developer.
  • Location
    Denmark 🇩🇰
  • Education
    Bachelor in Nutrition and health (I know not super relevant to my current line of work!)
  • Work
    Frontend developer at AccuRanker
  • Joined

Thanks Ali. Didn't realize you actually work at AWS until now! But I really liked the article! I am still short of my first project on AWS but Amplify really looks great!

CollapseExpand
 
luismanuel001 profile image
Luis Manuel
  • Location
    Madrid, Spain
  • Work
    Senior Frontend Engineer
  • Joined

Any plans for supporting ISR in the near future?

CollapseExpand
 
aspittel profile image
Ali Spittel
Passionate about education, Python, JavaScript, and code art.
  • Email
  • Location
    Denver, CO
  • Education
    Hamilton College
  • Work
    Developer things at AWS
  • Joined

I've passed along your request to the team! A few other people have asked as well, so my guess would be yes :)

CollapseExpand
 
roblescoulter profile image
Luis Robles
  • Joined

Hi!

Any idea why Next.js's Fast Refresh doesn't work when testing the application locally with Amplify?

Some comments have been hidden by the post's author -find out more

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Collection of articles by AWS Developer Advocates

More fromAWS

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp