📝
AWS Amplify CLI でプロファイルを削除した場合の解決方法
結論
削除したプロファイルを AWS CLI の config ファイルに再記述することで解決しました。
また、以下のコマンドでプロファイル情報を変更する方法でも解決できました。
$ amplify configure project
経緯
Amplify CLI で amplify init を行った際に設定した AWS CLI の config ファイルのプロファイルを削除した結果、既存の Amplify プロジェクトにアクセスできなくなりました。
再現
事象を再現して解決してみました。
前提
- 環境: Cloud9 (Amazon Linux 2023)
- Cloud9 の managed temporary credentials はオフ
- IAM ユーザー作成済み: AdministratorAccess 権限を付与
- IAM ユーザーから AssumeRole 可能な IAM ロール作成済み: AdministratorAccess を付与
01. 環境のセットアップ
# Amplify CLI のインストール
$ mkdir -p ~/.npm-global
$ npm config set prefix '~/.npm-global'
$ echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
$ source ~/.bashrc
$ npm install -g @aws-amplify/cli
$ amplify --version
14.2.5
# AWS CLI の認証情報の設定
$ aws configure
AWS Access Key ID [None]: xxx
AWS Secret Access Key [None]: xxx
Default region name [None]: ap-northeast-1
Default output format [json]: json
$ aws sts get-caller-identity
{
"UserId": "xxx",
"Account": "012345678901",
"Arn": "arn:aws:iam::012345678901:user/test"
}
$ cat ~/.aws/credentials
[default]
aws_access_key_id = xxx
aws_secret_access_key = xxx
$ aws sts assume-role \
--role-arn arn:aws:iam::012345678901:role/amplify-role\
--role-session-name amplify-role \
--profile default
{
"Credentials": {
"AccessKeyId": "xxx",
"SecretAccessKey": "xxx",
"SessionToken": "xxx",
"Expiration": "xxx"
},
"AssumedRoleUser": {
"AssumedRoleId": "xxx",
"Arn": "xxx"
}
}
# assume-role で取得した認証情報を設定
$ cat >> ~/.aws/credentials << 'EOF'
[amplify-role]
aws_access_key_id = assume-role で取得した AccessKeyId
aws_secret_access_key = assume-role で取得した SecretAccessKey
aws_session_token = assume-role で取得した SessionToken
EOF
$ cat >> ~/.aws/config << 'EOF'
[amplify-role]
output = json
region = ap-northeast-1
EOF
$ cat ~/.aws/config
[default]
output = json
[amplify-role]
output = json
region = ap-northeast-1
02. Amplify プロジェクトのセットアップ
$ amplify init
# 認証情報の設定で手順 01 で作成したプロファイルを指定
? Select the authentication method you want to use: (Use arrow keys)
❯ AWS profile
default
❯ amplify-role
# セットアップが完了したことを確認
✅ Initialized your environment successfully.
✅ Your project has been successfully initialized and connected to the cloud!
03. プロファイルを削除
手順 01 で設定した以下のプロファイルを削除してみます。
~/.aws/config
[amplify-role]
output = json
region = ap-northeast-1
$ sed -i.bak '/^\[amplify-role\]/,/^$/d' ~/.aws/config
$ cat ~/.aws/config
[default]
output = json
04. Amplify プロジェクトを更新
Amplify プロジェクトに認証機能を追加してプッシュを試みますが、手順 03 でプロファイルを削除したためプッシュが失敗します。
# 認証機能の追加
$ amplify add auth
Do you want to use the default authentication and security configuration? Default configuration
How do you want users to be able to sign in? Username
Do you want to configure advanced settings? No, I am done.
✅ Successfully added auth resource environment6f2d1fbe locally
# 更新の反映を試みるが失敗
$ amplify push
✖ There was an error pulling the backend environment dev.
🛑 A dynamic import callback was not specified.
05. 削除したプロファイルを復元
手順 03 で削除したプロファイルを再度 ~/.aws/config に記載します。
$ cat >> ~/.aws/config << 'EOF'
[amplify-role]
output = json
region = ap-northeast-1
EOF
$ cat ~/.aws/config
[default]
output = json
[amplify-role]
output = json
region = ap-northeast-1
06. 再度 amplify push を実行
プロファイルの復元後、再度 amplify push を実行するとエラーが解消されました。
$ amplify push
✔ Successfully pulled backend environment dev from the cloud.
Deployment state saved successfully.
補足. amplify configure project コマンドの実行
Commands - JavaScript - AWS Amplify Gen 1 Documentation
手順 06 の時点でエラーの解消はできていますが、補足として以下のコマンドを使用する方法も紹介します。
$ amplify configure project
# AWS Profile setting を選択
? Which setting do you want to configure?
Project information
❯ AWS Profile setting
Advanced: Container-based deployments
# Update AWS Profile を選択
? Do you want to update or remove the project level AWS profile?
No
❯ Update AWS Profile
Remove AWS Profile
# AWS profile を選択
? Select the authentication method you want to use: (Use arrow keys)
❯ AWS profile
AWS access keys
# プロジェクトに紐づけるプロファイルを選択
? Please choose the profile you want to use
❯ default
amplify-role
上記設定で新しいプロファイルをプロジェクトに紐づけられます。
まとめ
今回は AWS Amplify CLI でプロファイルを削除した場合の解決方法を紹介しました。
どなたかの参考になれば幸いです。
Discussion