Deploy with an Amplify Gen 2 backend
Amplify Gen 2 defines your backend (authentication, data, and storage) in TypeScript with the Amplify backend tooling (ampx). This page shows how to host your application on AWS Amplify while keeping that backend. You deploy the backend and the application from one continuous integration (CI) pipeline, and connect them with your Amplify Gen 2 app ID and branch.
Amplify builds your application and reads your Gen 2 backend’s outputs. It does not deploy your Gen 2 backend. Keep ampx pipeline-deploy in your CI pipeline, where the deploy role already carries the permissions it needs. Running the backend deploy inside the Amplify build means you must attach the AmplifyBackendDeployFullAccess managed policy to the environment’s AWS Identity and Access Management (IAM) role yourself. This widens the build role’s permissions and ties backend changes to every application deploy.
How the two deployments fit together
An Amplify app and an Amplify Gen 2 backend are separate deployments, and the order matters:
-
Your backend deploys with
ampx pipeline-deploy, targeting an Amplify Gen 2 app ID and branch. -
During the Amplify build,
ampx generate outputsreads the deployed backend’s configuration for the same app ID and branch and writesamplify_outputs.json. Your application loads that file through the Amplify client libraries and calls the provisioned backend resources at runtime.
The backend must finish deploying before the Amplify build reads its outputs. Deploy both from the same workflow, backend first, so aws amplifygen3 deploy runs only after ampx pipeline-deploy succeeds. Do not split deployment across separate triggers. This risks a race condition. If both run in parallel on a push, the Amplify build can read the backend before the new deploy completes. It then deploys the application against stale or missing outputs.
The app ID and branch are the link between the two. Map each Amplify Gen 2 branch to the matching Amplify environment. Your Amplify Gen 2 production branch should match your Amplify production environment, and your Amplify Gen 2 shared preview branch should match your Amplify preview environment. Keep the names aligned to make it clear which backend the Amplify environment should consume.
Prerequisites
Before you connect the two deployments, confirm the following:
-
You have an existing Amplify Gen 2 backend with an Amplify Gen 2 app ID and at least one branch.
-
Your application deploys to Amplify with
aws amplifygen3 deploy. -
You have a CI pipeline. This page uses GitHub Actions.
-
You can create an IAM role in your AWS account that the pipeline assumes, carrying the
AmplifyBackendDeployFullAccesspolicy for the backend and permission to runaws amplifygen3 deploy.
Copy your Amplify Gen 2 app ID and branch
Find your Amplify Gen 2 app ID (for example, d1234abcd) and the branch name you deploy the backend to. You reuse both values in two places: the pipeline that runs ampx pipeline-deploy, and the Amplify build that runs ampx generate outputs.
Record the pair for each environment. A common setup uses one Amplify Gen 2 app with a main branch for production and a sandbox branch for preview.
| Amplify environment | Amplify Gen 2 app ID | Amplify Gen 2 branch |
|---|---|---|
|
|
|
|
|
|
|
|
Configure the Amplify build to generate outputs
Your application needs amplify_outputs.json to connect to the deployed backend. Generate it during the Amplify build so the file reflects the backend for the environment you are deploying to.
First, store the app ID and branch as variables in each Amplify environment. These are not secrets, so set them without --secret:
aws amplifygen3 var set AMPLIFY_APP_ID d1234abcd --env preview aws amplifygen3 var set AMPLIFY_BRANCH sandbox --env preview aws amplifygen3 var set AMPLIFY_APP_ID d1234abcd --env production aws amplifygen3 var set AMPLIFY_BRANCH main --env production
Then run ampx generate outputs in the build command in amplify.jsonc, before your app’s build command. The same command works for every environment because it reads the app ID and branch from the environment’s variables. For a Next.js app, the build command is the Amplify adapter, and the output goes to .amplify:
amplify.jsonc
{ "name": "my-app", "deploy": { "install": "npm ci", "build": "npx ampx generate outputs --app-id $AMPLIFY_APP_ID --branch $AMPLIFY_BRANCH && npx @aws-amplifygen3/adapter-nextjs", "output_directory": ".amplify" } }
This runs during the service-side build, so the pipeline deploys with aws amplifygen3 deploy and not --prebuilt. The prebuilt path skips the service-side build, so ampx generate outputs would not run on Amplify. If you prefer to build in CI and deploy prebuilt output, run ampx generate outputs in the pipeline before the app’s build command instead, after the backend deploy step.
ampx generate outputs reads the deployed backend’s configuration for the given app ID and branch, so the Amplify build must be able to read that deployment. If the command fails with an access error, grant the environment’s IAM role read access to the backend’s deployment for that app and branch.
Load the generated file where you configure Amplify in your application:
amplify-config.ts
import { Amplify } from "aws-amplify" import outputs from "./amplify_outputs.json" Amplify.configure(outputs)
Deploy the backend and application from one workflow
Deploy the backend and the application from a single job so they run in order. This example uses two GitHub environments named production and preview to align with Amplify’s environment names, and stores the app ID and branch as variables scoped to each environment.
In your repository settings, create the production and preview environments. For each one, add two environment variables:
-
AMPLIFY_APP_ID -
Your Amplify Gen 2 app ID.
-
AMPLIFY_BRANCH -
The branch that environment deploys to.
The workflow selects the GitHub environment from the event, deploys the backend with ampx pipeline-deploy, and then deploys the application with aws amplifygen3 deploy. Because both steps run in the same job, the application deploy starts only after the backend deploy succeeds:
.github/workflows/deploy.yml
name: Deploy backend and application on: push: branches: [main] pull_request: branches: [main] permissions: id-token: write contents: read jobs: deploy: runs-on: ubuntu-latest environment: ${{ github.event_name == 'push' && 'production' || 'preview' }} env: AMPLIFYGEN3_ENV: ${{ github.event_name == 'push' && 'production' || 'preview' }} steps: - uses: actions/checkout@v4 - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::<account-id>:role/<deploy-role> aws-region: us-east-2 - name: Setup Node.js uses: actions/setup-node@v4 with: node-version: "22" - name: Install dependencies run: npm ci - name: Deploy backend run: npx ampx pipeline-deploy --app-id ${{ vars.AMPLIFY_APP_ID }} --branch ${{ vars.AMPLIFY_BRANCH }} - name: Deploy application run: aws amplifygen3 deploy --env $AMPLIFYGEN3_ENV
The job assumes a single role that deploys both the backend and the application. Grant it the AWS managed policy AmplifyBackendDeployFullAccess (arn:aws:iam::aws:policy/service-role/AmplifyBackendDeployFullAccess) for ampx pipeline-deploy, plus permission to run aws amplifygen3 deploy. Backend deploy permissions stay on this CI role, not on Amplify.
This workflow runs the pull request’s code with a role that can deploy your backend. Run it only for pull requests from your own repository, or gate the deploy behind a protected GitHub environment that requires manual approval, so untrusted code cannot deploy on its own.
Next steps
-
Amplify Gen 2 documentation on the Amplify website