2

I am running a Next.js app using the custom server options and the AWS CodePipeline for CI/CD.

My pipeline uses code build to build the app and AWS CodeDeploy to deploy to all instances within an autoscaling group. Configured with CodeDeployDefault.OneAtATime

I would like the cache and incrementally generated static pages to be shared between the ec2 instances.

Is there a way to do this and if so how would deployment of a new version to multiple instances work without affecting the shared files?

Stack:

  • EC2 Instances running AMAZON LINUX 2
  • Autoscaling group connected to an ELB
Costas
  • 109
  • 8
  • the static pages are part of the application deploy? Or artifacts the application creates while running? What kind of cache are you describing? – erik258 Aug 31 '22 at 21:17
  • Some static pages are created in the deploy stage and others are artifacts the application creates while running. The cache in the .next folder also changes while the application is running. – Costas Aug 31 '22 at 21:21
  • Maybe try deploying NextJS like Vercel does: https://github.com/milliHQ/terraform-aws-next-js – Matt Timmermans Aug 31 '22 at 22:17

1 Answers1

1

Please, consider using AWS EFS (Elastic File System) connected to all of your instances. It is a convenient solution for scenarios where application hosted on multiple machines needs access to the shared files. It works with machines operating under amazon linux. It means you next.js app on every instance will have access (read and write) to the same folder/files other instances have access to.

Google "mount EFS to instances" in order to find understandable and helpful guide.

Artem Arkhipov
  • 7,025
  • 5
  • 30
  • 52
  • Thank you for the answer. Regarding deployment how would that work ? If I deploy a new version and change the files on the EFS that would affect all the instances at once. Could I use some sort of versioning in the EFS and gradually move instances to the new version after their own deployment is complete ? – Costas Aug 31 '22 at 22:46
  • 2
    @Costas I think you can have a separate folder on EFS for files for the new version and make sure your application uses files from a corresponding folder. Then when all machines are updated you can remove the old directory. It is just an idea, you may improve this process and it depends on the overall architecture you have. For example, you may also consider blue-green deployments - spin up new instances with another efs mounted, direct app traffic there and then shut down old infrastructure. – Artem Arkhipov Sep 01 '22 at 11:06
  • Thank you that sounds like a good idea – Costas Sep 01 '22 at 22:35