0
0

【AWS】aws cli実行時にYou must specify a region. You can also configure your region by running "aws configure".となる

Posted at

やったこと

こちらの記事にあるEC2の一覧を取得するaws cliコマンドを実行した

エラー内容

You must specify a region. You can also configure your region by running "aws configure".

すでに aws configure コマンドでクレデンシャルは設定していたため、原因がよくわからなかった

# ルートディレクトリで実行
: ~ $ cat .aws/credentials 
aws_access_key_id = xxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxxx

: ~ $ cat .aws/config
[profile xxxx]
region = ap-northeast-1
output = json

一度.awsフォルダを削除して再度 aws configure コマンドを実行しても結果は変わらなかった

原因

profileオプションを指定していなかったこと

aws configureコマンドを実行するときに--profile <プロファイル名> でプロファイルを指定しないとprofile名が「default」になる
また、aws cliコマンド実行時にprofile名を指定しないと、defaultプロファイルでコマンドを実行する

$ aws configure
AWS Access Key ID [None]: xxxx
AWS Secret Access Key [None]: xxxx
Default region name [None]: ap-northeast-1
Default output format [None]: json

$ cat .aws/credentials 
[default] 👈 profile名がdefaultになる
aws_access_key_id = xxxx
aws_secret_access_key = xxxx

$ cat .aws/config
[default] 👈 profile名がdefaultになる
region = ap-northeast-1
output = json

この状態でaws cliコマンドを実行すると問題なく実行できる
自分の場合、defaultを使用せず、profile名を指定してクレデンシャルを設定していた
しかし、profile名をcli実行時に指定していなかったため、コマンド実行に失敗していた

対応

aws cli実行時にprofile名も指定する

$ aws ec2 describe-instances --output=table ... --profile xxxx
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0