Search code examples
typescriptamazon-web-servicesnext.jsaws-cdkcicd

File/Folder Structure For Nextjs Frontend & AWS CDK Backend


I was wondering if there were best practices regarding folder structure when creating websites with serverless backends.

Right now I have a Nextjs frontend and an AWS CDK backend. The current folder structure I am using has the cdk backend embedded in the frontend. Something like this:

.next
backend
   bin
   lib
   test
   node_modules
   .gitignore
   .npmignore
   package.json
   etc...
node_modules
public
src
styles
.gitignore
next-config.js
package.json
etc.....

As you can see, the frontend and the backend have separate node_modules (cdk and nextjs are both using typescript) and many of the packages seem to be locally duplicated. Is this ok?

Also, these are currently both within the same git repository. Is this advisable in terms of github-based CI/CD? Which folder structure (and git structure) will make development and deployment the least painful?


Solution

  • No, local duplication is not OK.

    I think you can start with AWS CDK Best Practices with special attention to Coding best practices section. Second, you can refer to AWS Solution Architect article who describes Recommended AWS CDK project structure for Python applications. By default, I understand that python is not Nextjs, though you can see find the general principles in it. Third, you can use a better way to structure AWS CDK projects around Nested Stacks and start to convert the Stacks into Libraries and re-using the code.