Search code examples
javascriptamazon-web-servicesamazon-ec2next.jsaws-code-deploy

Deploy next js app to aws ec2 through codedeploy


I created a new NextJS app, no changes just the basic boilerplate files, code, and folders. and pushed it to a CodeCommit repo. Already had a working CodePipeline and just swapped Source stages. Deployments are failing on Step 3, where a replacement instance is created, and Target Group shows the original instance is healthy but new instances are given a 403 error code.

For the appspec.yml file, I've set the source to / b/c that's what the Next JS docs said. I also tried setting it to pages/_app.js and /app.js b/c that's what Fireship mentioned as the entry. None have worked and codedeploy keeps failing. I talked to AWS Support and we narrowed it to Next JS causing the issue.

If you're also running a NextJS app and deploying it to EC2 through CodeDeploy and CodePipeline, what did you set the source path to? For both the appspec and Target Group health check. Do I need to use Docker and Kubernetes like this SO Thread? Or are the failures caused by something else I should check?


Solution

  • Instead of using EC2 and CodeDeploy, follow this AWS Doc and use Amplify to build and deploy your NextJS app. This is the link for using a custom domain: https://docs.aws.amazon.com/amplify/latest/userguide/to-add-a-custom-domain-managed-by-amazon-route-53.html