Page updated Mar 12, 2024

Monorepo setup

Some teams choose a monorepo approach, or single repositories that contain multiple packages or components to simplify the deployment process for shared libraries and components. Without a monorepo, you have to deploy each package individually, keep track of package versions and dependencies across packages, and ensure version compatibility. This can become exponentially more complex as the number of packages grows. With a monorepo, all packages and dependencies are contained within a single repository.

Amplify Gen 2 supports monorepo workflows for fullstack builds with monorepo tools such as Nx and yarn workspaces. When building with Gen 2, we recommend creating the amplify/ folder in a shared workspace. We will use the following example for this guide:

1├── apps/
2│ ├── admin-dashboard/
3│ │ ├── next.config.mjs
4│ │ └── package.json
5│ └── marketing-site/
6│ ├── astro.config.mjs
7│ └── package.json
8├── packages/
9│ └── my-shared-backend/
10│ ├── amplify/
11│ │ ├── auth/
12│ │ │ └── resource.ts
13│ │ ├── data/
14│ │ │ └── resource.ts
15│ │ └── backend.ts
16│ |── package.json
17 └── tsconfig.json
18└── package.json

Monorepos require a slightly different setup. We are going to deploy 3 Amplify apps:

  1. my-shared-backend
  2. admin-dashboard
  3. marketing-site

Deploy backend app

The first app, my-shared-backend, will be the only app that updates changes to the backend. The other apps will only run frontend builds that point to the shared backend.

  1. To get started, deploy the shared backend Amplify app. With Gen 2, you can now setup backend-only CI/CD apps. Navigate to the console and select New app > Build an app (Gen 2).

  2. Once you connect your repository, select your monorepo project. Check the box that says My app is a monorepo and enter the path to your amplify backend.

monorepo

  1. Your build settings should be automatically detected. Save and deploy

Deploy frontend apps

  1. For the frontend apps, connect the frontend projects in the Amplify console separately, and update the build commands to include:
1npx amplify generate config --branch main --app-id BACKEND-APP
  • BACKEND-APP is the app-id for your backend app. You can find this by going to the Amplify console and navigating to backend-app > App settings > App ARN and copying the ID located at the end of the string arn:aws:amplify:<region>:<account-id>:apps/app-id.