View a markdown version of this page

Deploy with GitHub Actions - AWS Amplify

Deploy with GitHub Actions

Welcome to the new AWS Amplify

Amplify now puts hosting, a Postgres database, and sign-in in one AWS account. Deploy from your terminal or from Git, check every change in a preview first, and roll back in one step if something breaks.

Looking for Amplify Hosting? See the Amplify Hosting User Guide.

GitHub Actions is the built-in continuous integration and continuous delivery (CI/CD) platform for GitHub. Use it to deploy your AWS Amplify app automatically. The workflows in this topic deploy each pull request to the preview environment and each merge to main to the production environment, from GitHub Actions runners that you control.

To have Amplify deploy from your repository without a workflow file, connect the repository to your app instead. Amplify then builds and deploys each push and each pull request itself. For more information, see Set up continuous deployment from Git and Pull requests for your Amplify app.

The recommended workflow builds your app in CI and deploys the prebuilt output. Each run authenticates to AWS, installs dependencies, downloads the environment’s variables, builds the app, and deploys the output with aws amplifygen3 deploy --prebuilt. Building in CI keeps the build environment under your control and lets you run tests against the same artifact you ship.

Prerequisites

Before you automate deployments, complete the following prerequisites:

  • Confirm that your app deploys from your machine with aws amplifygen3 deploy.

  • Confirm that your build settings, including deploy.output_directory, are in amplify.jsonc and committed to the repository.

  • Confirm that you can create an AWS Identity and Access Management (IAM) role in your AWS account, or that you have access keys for a role that can deploy. For more information about managing access keys safely, see Manage access keys for IAM users in the IAM User Guide.

Authenticate to AWS

The workflow authenticates to AWS with the aws-actions/configure-aws-credentials action. You have two options.

We recommend the GitHub OpenID Connect (OIDC) option. With OIDC, GitHub Actions requests a short-lived token for each run and exchanges it for temporary AWS credentials. No long-lived secrets are stored in GitHub. Create an IAM role in your AWS account that trusts the GitHub OIDC provider. Scope its trust policy to your repository so that only workflows from that repository can assume the role, and grant it permission to deploy your app. For the actions a deploying role needs, see Backend identity-based policy examples. For the provider setup, the trust policy, and example repository conditions, see Configuring a role for GitHub OIDC identity provider in the IAM User Guide. Then reference the role by ARN:

- name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::<account-id>:role/<amplifygen3-deploy-role> aws-region: us-east-2

OIDC requires the job to request an identity token, so grant the id-token: write permission on the job (shown in the following workflow).

The alternative is a long-lived access key stored as a repository secret. Use access keys only when your organization cannot use OIDC. This works, but the credentials do not expire on their own and must be rotated manually:

- name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v4 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: us-east-2

Prefer OIDC for production repositories.

Deploy to production on push to main

This workflow deploys to production when you push to main. It configures AWS credentials, installs dependencies, downloads the production environment’s variables, builds the app, and deploys the built output:

.github/workflows/deploy.yml

name: Deploy to Amplify on: push: branches: [main] permissions: id-token: write contents: read jobs: deploy: runs-on: ubuntu-latest 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/<amplifygen3-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: Download environment variables run: aws amplifygen3 var download --env production - name: Build application run: npx @aws-amplifygen3/adapter-nextjs - name: Deploy to production run: aws amplifygen3 deploy --env production --prebuilt

In the previous workflow, you download the variables before building the application. This is because frameworks bake build-time variables into the output. aws amplifygen3 var download --env production writes the environment’s variables to a local .env file, so the build can access values such as NEXT_PUBLIC_*. Download the same environment you deploy to.

The build step runs the command that deploy.build names in amplify.jsonc. For a Next.js app that aws amplifygen3 create set up, this is npx @aws-amplifygen3/adapter-nextjs, which builds your app and writes the output to .amplify. Don’t run npm run build alone: it writes only .next, and deploy --prebuilt then fails with Output directory not found. If your deploy.build is a different command, run that command instead.

aws amplifygen3 var download writes each secret’s name with an empty value, so the file holds no secret value. Amplify injects secrets at deploy time, so they reach your application at runtime. A secret is not available to the build that runs in GitHub Actions. A build that reads one through process.env sees an empty string, not an undefined value.

If your build itself reads a secret, such as a private API key used during static generation, define that secret again in your CI/CD service and pass it to the build step. You maintain this build-time secret in GitHub, separately from the secret you set in Amplify for runtime. In GitHub Actions, store it as a repository or environment secret and expose it with env:

- name: Build application env: MY_BUILD_SECRET: ${{ secrets.MY_BUILD_SECRET }} run: npx @aws-amplifygen3/adapter-nextjs

aws amplifygen3 deploy --prebuilt uploads the built output and skips the service-side build. With no argument, it deploys the directory that deploy.output_directory names in amplify.jsonc. To deploy a specific location instead, point it at a directory or a zip archive, for example aws amplifygen3 deploy --prebuilt build.zip. Replace build.zip with the path to your own directory or archive.

The --env production flag is required on both the download and the deploy. Without it, the commands target the preview environment, which protects production from accidental deploys.

Two merges in quick succession start two deployments that race each other. To run them one at a time, add a concurrency block to the deploy job with a fixed group and cancel-in-progress: false.

Migrations in a prebuilt deployment

If your app has a backend, aws amplifygen3 deploy --prebuilt also sends your migrations, and the deployment applies them. For a directory, the command adds the migrations/ folder that sits next to amplify.jsonc in your checkout. For a zip file, the command sends the zip unchanged, so put migrations/ at the root of the zip. A zip without migrations/ applies no migrations, and the deployment refuses a zip that leaves out migrations that the environment already applied.

Pull request previews share one database

These workflows run aws amplifygen3 deploy, so each run is a CLI deployment. Outside production, every CLI deployment uses the environment’s source-upload backend branch, so all of your pull request previews share one database. If the app has no backend yet, the first run creates it from the backend block in amplify.jsonc. For which branch a deployment uses, see Use environments for your apps.

Deploy previews for pull requests

To review changes before they reach production, deploy each pull request to the preview environment. Add a second workflow that triggers on pull_request. The following Amplify commands default to the preview environment, so the deploy and download steps need no environment flag:

.github/workflows/preview.yml

name: Deploy preview on: pull_request: branches: [main] permissions: id-token: write contents: read jobs: preview: runs-on: ubuntu-latest 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/<amplifygen3-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: Download environment variables run: aws amplifygen3 var download - name: Build application run: npx @aws-amplifygen3/adapter-nextjs - name: Deploy preview run: aws amplifygen3 deploy --prebuilt

Each deployment prints a unique URL, in the form https://<deployment-id>--<app-name>.amplify.app, that you can share for review. Sharing it does not affect the environment’s leader URL, the stable URL that points at whichever deployment is active. With auto-promote on, which is the default, each deployment also becomes the one that preview serves. For more information, see Deployments. Post the deployment’s URL as a comment on the pull request so reviewers can open the change directly.

Note

These workflows run the pull request’s code with credentials that can deploy. Run them 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.

A deploy role reaches every backend in the account

A deploy grant reaches further than one app. Backend deployment actions carry no resource-level permissions, so a role that can deploy reaches every backend in the account. Its trust policy limits who can assume the role, not what the role can deploy to. Keep environments that you trust differently in separate AWS accounts.

Let Amplify build instead

We recommend building in CI if you want to run tests against your build output, or if your pipeline already produces the artifact. If you don’t need to do this, omit the var download, build, and --prebuilt steps and run aws amplifygen3 deploy on its own. Amplify then performs a service-side build, using the commands in amplify.jsonc, and injects the environment’s variables during the build. A service-side build also receives the environment’s secrets unless you store them with --scope BACKEND. A build that reads a secret needs no GitHub secret of its own.

Set environment variables outside the workflow

Store your application’s variables and secrets with aws amplifygen3 var set, not in the workflow file. Amplify keeps them for each environment. The workflow downloads the variables at build time with aws amplifygen3 var download, which writes every name but no secret value. Amplify injects the full set, including secrets, at deploy time. No values are hardcoded in the workflow or committed to the repository. The --secret flag prompts for the value so it stays out of shell history. For non-interactive use, such as a script, pipe the value through stdin:

# Prompts for the value and keeps it out of shell history aws amplifygen3 var set STRIPE_KEY --secret --env production # Non-interactive (for example, in a script): pipe the value through stdin echo "$STRIPE_KEY" | aws amplifygen3 var set STRIPE_KEY --secret --env production

A secret that backend.auth in amplify.jsonc references as ${env:NAME} also needs --scope BACKEND. With that scope, only your app’s backend gets the value. Without --scope, a new variable gets the scope ALL and the deployment refuses the reference. For more information, see Variables that sign-in uses.

Limit GitHub repository secrets to what the workflow needs directly, such as AWS access keys when you cannot use OIDC and any secret your build reads. For the full model, see Environment variables and secrets.

Next steps