aws configureを使ってEC2からのAWSリソース(S3)認証周りを試してみたのでメモ。
(前提)
東京リージョン(ap-northeasrt-1)
EC2インスタンスx1(Amazon Linux2)
S3に1つバケット作成済 (バケット名:hogehoge)
①IAMユーザのアクセスキーをaws configureを使ったプロファイルとして作成してアクセスする方法
まず何もしないでいきなりs3にアクセスしようとしてもエラーになります。
[ec2-user@***** ~]$ aws s3 ls s3://hogehoge/
Unable to locate credentials. You can configure credentials by running "aws configure".
次に現在のプロファイル(default)を確認→何も入っていない
[ec2-user@***** ~]$ aws configure list
Name Value Type Location
---- ----- ---- --------
profile <not set> None None
access_key <not set> None None
secret_key <not set> None None
region <not set> None None
[ec2-user@***** ~]$ aws s3 ls --profile default
The config profile (default) could not be found
マネジメントコンソール側の作業は割愛しますが、あらかじめS3にアクセスできる権限(IAMポリシー)を付与した
IAMユーザを作成しました。
んでそのIAMユーザに紐づくアクセスキーを作成しておきまして、そこから各種Key情報を取ったうえで、
aws configureコマンドでプロファイルを作成。
今回は「s3access」という名前でプロファイルを作成しました。
[ec2-user@***** ~]$ aws configure --profile s3access
AWS Access Key ID [None]: **********
AWS Secret Access Key [None]: *************
Default region name [None]: ap-northeast-1
Default output format [None]: json
[ec2-user@***** ~]$ aws configure list
Name Value Type Location
---- ----- ---- --------
profile <not set> None None
access_key <not set> None None
secret_key <not set> None None
region <not set> None None
[ec2-user@***** ~]$ aws configure list --profile s3access
Name Value Type Location
---- ----- ---- --------
profile s3access manual --profile
access_key **************** shared-credentials-file
secret_key **************** shared-credentials-file
region ap-northeast-1 config-file ~/.aws/config
というところまで準備したのでs3へのアクセス可否を確認
aws s3 lsコマンド時に何も指定しないとdefalutプロファイルでのアクセスになるので、やっぱりエラー。
[ec2-user@***** ~]$ aws s3 ls s3://hogehoge/
Unable to locate credentials. You can configure credentials by running "aws configure".
次にプロファイル名「s3access」を指定して実行
[ec2-user@***** ~]$ aws s3 ls --profile s3access s3://hogehoge
2023-07-04 10:45:38 5258 xxxxxx.txt
2023-07-04 10:45:37 942 xxxxxx.txt
2023-07-04 10:45:38 539 xxxxxx.txt
2023-07-04 10:45:37 2138 xxxxxx.txt
2023-07-04 10:45:37 99 xxxxxx.txt
2023-07-04 10:45:37 375 xxxxxx.txt
はい、ちゃんと見えました。
一旦これはこれでOK。
②今さら感がありますが、IAMロールでも確認
詳細割愛しますが、あらかじめs3FullAccessのポリシーを付与したIAMロールを作り、
且つ今回のEC2インスタンスに、IAMロールをアタッチしました。
その状態でaws configureを実行すると、下記のようにdefaultに設定されるようです。
[ec2-user@***** ~]$ aws configure list
Name Value Type Location
---- ----- ---- --------
profile <not set> None None
access_key **************** iam-role
secret_key **************** iam-role
region <not set> None None
ですので、aws s3 lsコマンドもプロファイル名を指定せずに実行できるようになります。
[ec2-user@***** ~]$ aws s3 ls s3://hogehoge/
2023-07-04 10:45:38 5258 xxxxx.txt
2023-07-04 10:45:37 942 xxxxx.txt
2023-07-04 10:45:38 539 xxxxx.txt
2023-07-04 10:45:37 2138 xxxxx.txt
2023-07-04 10:45:37 99 xxxxx.txt
2023-07-04 10:45:37 375 xxxxx.txt
一方で先ほど設定したs3accessも健在。
[ec2-user@***** ~]$ aws configure list --profile s3access
Name Value Type Location
---- ----- ---- --------
profile s3access manual --profile
access_key **************** shared-credentials-file
secret_key **************** shared-credentials-file
region ap-northeast-1 config-file ~/.aws/config
IAMロールをアタッチした方が簡単ですし、アクセスキーも持たずに済むので
セキュリティリスクも運用負荷も避けられることからIAMロール運用の方が
AWSのべスプラであることは間違いないですが、、、
システムや運用によっては、IAMロールは別のものに使って、その他の用途と組み合わせる時には
こういう方法もありなのかなあ、とか思ったり。
(それでもIAMポリシー/IAMロールの方をいろいろ組み合わせればなんとかなるし、
そちらの方がよいような気もしますが、、)
(番外編)アクセスキーを入れ替えた場合のプロファイル設定変更
[ec2-user@***** ~]$ aws s3 ls s3://hogehoge/ --profile s3access
An error occurred (InvalidAccessKeyId) when calling the ListObjectsV2 operation: The AWS Access Key Id you provided does not exist in our records.
[ec2-user@***** ~]$ aws configure --profile s3access
AWS Access Key ID [****************]: **********************
AWS Secret Access Key [****************]: ***********************
Default region name [ap-northeast-1]:
Default output format [json]:
[ec2-user@***** ~]$ aws configure list --profile s3access
Name Value Type Location
---- ----- ---- --------
profile s3access manual --profile
access_key **************** shared-credentials-file
secret_key **************** shared-credentials-file
region ap-northeast-1 config-file ~/.aws/config
[ec2-user@***** ~]$ aws s3 ls s3://hogehoge/ --profile s3access
2023-07-04 10:45:38 5258 xxxx.txt
2023-07-04 10:45:37 942 xxxx.txt
2023-07-04 10:45:38 539 xxxx.txt
2023-07-04 10:45:37 2138 xxxx.txt
2023-07-04 10:45:37 99 xxxx.txt
2023-07-04 10:45:37 375 xxxx.txt