EC2にてAWS CLIコマンドの初期セットアップについての備忘録になります。
Table of Contents
AWSCLI アクセスキーID, シークレットアクセスキーの作成
AWSマネジメントコンソールにてIAMを検索します。

左ペインのユーザーから対象のユーザー名を選択します。

認証情報タブからアクセスキーの作成をします。

アクセスキー情報が作成されるため、その情報をもとに、今度はEC2を立ち上げ、以下コマンドを打ち込みます
aws configure
作成されたアクセスキーID, シークレットアクセスキーを入力し、
Default regionはap-northeast-1
Default output formatはtext形式, json形式, table形式を選択できるので今回はjson形式を入力します。
AWS Access Key ID [None]:
AWS Secret Access Key [None]:
Default region name [None]: ap-northeast-1
Default output format [None]: json
ここまで実行すると~/.awsディレクトリにconfigファイルとcredentialsファイルが作成されます。
catコマンドで中身を確認すると
[profile default]
region = ap-northeast-1
output = json
[default]
aws_access_key_id = xxxxxxxxxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxxxxxxx
上記のようなファイルが出来上がっているかと思います。
configファイルとは
AWS CLIコマンドを使用するためのプロファイル情報です
credentialsファイルとは
credentials = 資格 等の意味
AWS CLIで使うcredentials情報とはAWSのAPI操作のために必要なキー情報となる。
credentialsファイルをもう一度確認すると
[default]
aws_access_key_id = xxxxxxxxxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxxxxxxx
キー情報が記載されています。
APIリクエスト時に、このcredentialsに記載されたアクセスキーとシークレットアクセスキー情報を渡して認証しているようです。
configファイルとcredentialsファイルのプロファイルを指定
aws configureコマンドでprofile情報を指定しないとdefaultの状態で設定されます。
これを–profileでプロファイル情報を指定してやると、指定した情報がファイルに書き込まれます。
aws configure --profile ec2-user
などと指定するとconfigファイルとcredentialsファイルにec2-userのプロファイル情報が書き込まれます。
AWS CLI 設定したプロファイル情報で確認
aws ec2 --profile ec2-user describe-instances
上記コマンドで–profile ec-2userで明示的にプロファイル情報をしてして、EC2のdescribe = 説明を確認できます。
以下のようなjson形式で情報が表示されていれば成功です。

以上でAWS CLIコマンドの設定備忘録となります。