📝

AWS CLI の default profile の名前を変更したらエラーになったので調べてみた

2022/03/02に公開

AWS CLIでaws configureを実行した際に自動生成される、default profile の名前を変更するとどうなるのか検証してみました。

前提

  • AWS CLI 情報:aws-cli/2.2.7 Python/3.8.8 Windows/10 exe/AMD64 prompt/off
  • IAMユーザーを作成済み

結論

Unable to locate credentials. You can configure credentials by running "aws configure".
というエラーが発生しました。

検証

1. aws configureを実行して default profile を自動生成

まずは、作成済みの IAM ユーザーの認証情報でaws configureを実行し、default profile を自動生成します。

aws configure
AWS Access Key ID [None]: <Access Key ID>
AWS Secret Access Key [None]: <Secret Access Key>
Default region name [None]: region
Default output format [None]: json

実行後に、credentials ファイルと config ファイルに以下の内容が記載されます。

[default]
aws_access_key_id = <Access Key ID>
aws_secret_access_key = Secret Access <Key>
[default]
region = ap-northeast-1
output = json

ここまでで default profile を自動生成できました。

2. default profile でアクセスしてみる

default profile の名前を変更する前に、default profile で S3 のバケット一覧を取得するコマンドを実行してみます。

aws s3 ls

実行結果は IAM ユーザーにアタッチしているポリシーによって変わりますが、僕はスイッチロール以外のポリシーはアタッチしていないので、アクセス拒否のエラーが返ってきました。

結果
An error occurred (AccessDenied) when calling the ListBuckets operation: Access Denied

ここまでは想定通りです。

3. default profile の名前を変更してみる

credentials ファイルの default profile の名前を test に変更してみます。

[test]
aws_access_key_id = <Access Key ID>
aws_secret_access_key = Secret Access <Key>

名前を変更した状態で再度 S3 のバケット一覧を取得するコマンドを実行してみます。

aws s3 ls
結果
`Unable to locate credentials. You can configure credentials by running "aws configure".`

クレデンシャルが見つかりません。"aws configure"を実行してクレデンシャルを設定できます。」というエラーが返ってきました。

再度 default という名前に戻してからaws s3 lsを実行すると、

An error occurred (AccessDenied) when calling the ListBuckets operation: Access Denied

のエラーが返ってきたので、default という名前を変更したことで発生したエラーのようです。

4. config ファイルを編集してみる

credentials ファイルに加えて、config ファイルの profile 名も編集してみます。

[test]
region = ap-northeast-1
output = json

profile 名を default から test に変更し、aws s3 lsを実行してみます。

aws s3 ls
結果
Unable to locate credentials. You can configure credentials by running "aws configure".

3 と同じエラーが返ってきました。
検証により、以下のことがわかりました。
・credentials ファイルの default profile がないとエラーになる
・credentials ファイルと config ファイルの profile 名を「default」以外で統一してもエラーになる

ドキュメントには記載がない

ドキュメントには、デフォルトで default profile を使用することは明記されていますが、default profile がないとどうなるのかということは記載されていませんでした。

設定ファイルと認証情報ファイルの設定 - AWS Command Line Interfaceより

ファイルは profiles に分割されます。デフォルトで、AWS CLI は default という名前のプロファイルにある設定を使用します。替わりの設定を使用するには、追加のプロファイルを作成して参照できます。

まとめ

AWS CLI でaws configureを実行した際に自動生成される、default profile の名前を変更するとどうなるのか検証してみました。
検証の結果、
Unable to locate credentials. You can configure credentials by running "aws configure"
というエラーが出るということがわかりました。
解決策として、default という profile を記載しておく必要があることもわかりました。
参考にして頂ければ幸いです。

Discussion