How to Deploy a Project with Private Git Submodules to AWS Amplify
Here's the English translation of the provided Japanese blog article:
The AWS Amplify version of the How to Deploy a Project to Vercel with Private Git Submodules that I wrote earlier. The process is almost the same.
1. Create a Fine-grained Personal Access Token
Go to the following page: https://github.com/settings/tokens?type=beta
Click on "Generate new token".

For Repository access, select "Only select repositories" and choose the minimum necessary repositories.
For Repository permissions, grant Read-only access to Contents only.

Click the "Generate token" button to create the token.
2. Register the Token as an Environment Variable in Amplify
From the left menu, go to "Hosting" and select "Environment variables", then click on "Manage variables".

Add a new variable named GITHUB_PAT and set the token value.

3. Create a Build Script
Create a script named sh/pre-build-for-amplify.sh.
#!/usr/bin/env bash
cd $(dirname $0)/../ || exit
if [ -z "${GITHUB_PAT}" ]; then
echo "The environment variable GITHUB_PAT is not set. Please regenerate the GitHub Fine-grained token and register it as the GITHUB_PAT environment variable in Amplify."
echo "https://github.com/settings/tokens?type=beta"
echo "https://ap-northeast-1.console.aws.amazon.com/amplify/apps/"
exit 1
fi
echo "If updating the submodule fails, please regenerate the GitHub Fine-grained token and register it as the GITHUB_PAT environment variable in Amplify."
echo "https://github.com/settings/tokens?type=beta"
echo "https://ap-northeast-1.console.aws.amazon.com/amplify/apps/"
git submodule set-url <my-submodule> "https://${GITHUB_PAT}@github.com/ytyng/<my-submodule>.git"
git submodule sync
git submodule update --init
4. Use This Build Script in Amplify
On the Amplify page, go to Hosting → Build settings, and either modify the amplify.yml file in the browser or place the amplify.yml file in the root directory of your project.
Add sh/pre-build-for-amplify.sh to preBuild.commands.
version: 1
frontend:
phases:
preBuild:
commands:
- 'sh/pre-build-for-amplify.sh' # Add this line
- 'npm ci --cache .npm --prefer-offline'
build:
commands:
- 'npm run build'
artifacts:
baseDirectory: build
files:
- '**/*'
cache:
paths:
- '.npm/**/*'
We look forward to discussing your development needs.