LoginSignup
2
2

More than 1 year has passed since last update.

AWS CLIのコマンドを実行しても認証エラーで失敗する

Last updated at Posted at 2022-08-19

はじめに

この現象に当てはまる人はレアでしょうが、数時間詰まったので備忘録を残します。

環境

  • AWS Cloud9
  • Amazon EC2
  • Amazon Linux2

発生するエラーメッセージ

AWS CLIのコマンドで認証エラーが発生する
※ ECR・EC2・S3のコマンドで全滅

:~/environment $ aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.ap-northeast-1.amazonaws.com

An error occurred (UnrecognizedClientException) when calling the GetAuthorizationToken operation: The security token included in the request is invalid
Error: Cannot perform an interactive login from a non TTY device
:~/environment $ aws ec2 describe-instances

An error occurred (AuthFailure) when calling the DescribeInstances operation: AWS was not able to validate the provided access credentials
:~/environment $ aws s3 ls

An error occurred (InvalidToken) when calling the ListBuckets operation: The provided token is malformed or otherwise invalid.

試したこと

通常はaws configureコマンドでAWS CLIのプロフィールを設定しなおせば直るが、
本件ではaws configureで再設定を行った上でも、上記のエラーが再発したので下記手順を行った。

AWS CLIの設定ディレクトリー(~/.aws)を削除する

:~ $ cd ~
:~ $ rm -r .aws
:~ $ ls -la
合計 96
drwx------ 15 ec2-user ec2-user  4096  8月 19 17:05 .
drwxr-xr-x  4 root     root        38  7月 12 17:35 ..
-rw-------  1 ec2-user ec2-user 13519  8月 19 17:05 .bash_history
-rw-r--r--  1 ec2-user ec2-user    18  7月 15  2020 .bash_logout
-rw-r--r--  1 ec2-user ec2-user  1294  7月 22 10:34 .bash_profile
:~ $ ls -la
合計 96
drwx------ 16 ec2-user ec2-user  4096  8月 19 17:05 .
drwxr-xr-x  4 root     root        38  7月 12 17:35 ..
drwxr-xr-x  2 ec2-user ec2-user    25  8月 19 17:05 .aws ←削除したはずなのに復活している
-rw-------  1 ec2-user ec2-user 13526  8月 19 17:05 .bash_history
-rw-r--r--  1 ec2-user ec2-user    18  7月 15  2020 .bash_logout
-rw-r--r--  1 ec2-user ec2-user  1294  7月 22 10:34 .bash_profile

lsコマンドを2回打ってわかったのだが、なんと削除したはずの.~/.awsディレクトリーが数十秒経つと復活してしまう。

SSMサービスを停止

これには驚いたが、サービスが悪さをしているのだと予想。

:~ $ systemctl list-units --type=service
UNIT                                  LOAD   ACTIVE SUB     DESCRIPTION
acpid.service                         loaded active running ACPI Event Daemon
amazon-ssm-agent.service              loaded active running amazon-ssm-agent
atd.service                           loaded active running Job spooling tools
auditd.service                        loaded active running Security Auditing Service
chronyd.service                       loaded active running NTP client/server

amazon-ssm-agent.serviceが怪しいということで停止する

※ 注意
Session Managerが切断されるので下記コマンドはSession ManagerでなくSSH経由で接続し試すこと

:~ $ sudo systemctl stop amazon-ssm-agent.service

~/.awsディレクトリーを削除

あとは普通に.awsディレクトリーを削除する

:~ $ ls -la
合計 96
drwx------ 16 ec2-user ec2-user  4096  8月 19 17:05 .
drwxr-xr-x  4 root     root        38  7月 12 17:35 ..
drwxr-xr-x  2 ec2-user ec2-user    25  8月 19 17:05 .aws
-rw-------  1 ec2-user ec2-user 13607  8月 19 17:08 .bash_history
-rw-r--r--  1 ec2-user ec2-user    18  7月 15  2020 .bash_logout
-rw-r--r--  1 ec2-user ec2-user  1294  7月 22 10:34 .bash_profile
:~ $ rm -r ~/.aws
:~ $ ls -la
合計 96
drwx------ 15 ec2-user ec2-user  4096  8月 19 17:08 .
drwxr-xr-x  4 root     root        38  7月 12 17:35 ..
-rw-------  1 ec2-user ec2-user 13627  8月 19 17:08 .bash_history
-rw-r--r--  1 ec2-user ec2-user    18  7月 15  2020 .bash_logout
-rw-r--r--  1 ec2-user ec2-user  1294  7月 22 10:34 .bash_profile
:~ $ ls -la
合計 96
drwx------ 15 ec2-user ec2-user  4096  8月 19 17:08 .
drwxr-xr-x  4 root     root        38  7月 12 17:35 ..
-rw-------  1 ec2-user ec2-user 13627  8月 19 17:08 .bash_history
-rw-r--r--  1 ec2-user ec2-user    18  7月 15  2020 .bash_logout
-rw-r--r--  1 ec2-user ec2-user  1294  7月 22 10:34 .bash_profile

.awsディレクトリーが復活する現象は止められた。

AWS CLIを再設定する

あとは普通にaws configureコマンドを使ってAWS CLIのプロフィールを再設定する

:~ $ aws configure
AWS Access Key ID [None]: [あなたのアクセスキー]
AWS Secret Access Key [None]: [あなたのシークレットキー]
Default region name [None]: [あなたが利用しているAWSのリージョン]
Default output format [None]: [任意のフォーマット]

結果

冒頭のコマンドが正常に叩けることを確認。お疲れさまでした。

2
2
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
2
2