AWS CloudShell でコマンドを実行した際に待っても応答がなく、処理がタイムアウトになる原因を教えてください

2023.03.01

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

困っていること

実施するコマンドの内容として、●●●-log-bucket/ 配下の「sample-1234567890」とついた非常に多くあるオブジェクトを、●●●-log-bucket/sample-1234567890/ 配下に移動させたいです。
AWS CloudShell を利用して"aws s3 mv"コマンドを実行したのですが待っても応答がなく処理がタイムアウトになってしまい原因が解らず困っています。

  • 実行するコマンドの構文に問題はない
  • 権限に問題はない

上記を踏まえ、原因と対処法を教えてください。

※CloudShell でのコマンド例

$ aws s3 mv s3://●●●-log-bucket/ s3://●●●-log-bucket/sample-1234567890/ --exclude "*" --include "sample-1234567890*" --recursive --page-size 50

どう対応すればいいの?

調査

"--debug"を付与して aws s3 mv コマンドを実行した際の出力結果を確認してください。

※--debug を付与しての aws s3 mv コマンド例

$ aws s3 mv s3://●●●-log-bucket/ s3://●●●-log-bucket/sample-1234567890/ --exclude "*" --include "sample-1234567890*" --recursive --page-size 50 --debug

原因

"--debug"付与したコマンド実行結果より、下記のエラー表示を確認。

botocore.exceptions.ClientError: An error occurred (ExpiredToken) when calling the ListObjectsV2 operation: The provided token has expired.
fatal error: An error occurred (ExpiredToken) when calling the ListObjectsV2 operation: The provided token has expired.

上記エラー発生の原因として、Expiredtoken は一時認証情報の有効期限が切れた場合に発生いたします。
"aws s3 mv"コマンドは内部的に複数回のリクエストが行われますが、対象のオブジェクトを全て移動する前に一時認証情報の有効期限が切れたため、当該エラーが発生した状況と見受けられます。

コマンドを実行された CloudShell 環境について、ドキュメント 記載の通り、無操作時間が 20 ~ 30 分経過するとシェルセッションが終了します。
また、12 時間を超えると、操作の有無に依らずシェルセッションが終了します。

Shell sessions
Inactive sessions: AWS CloudShell is an interactive shell environment—if you don't interact with it using your keyboard or pointer for 20–30 minutes, your shell session ends. Running processes don't count as interactions.
Long-running sessions: A shell session that runs continuously for approximately 12 hours automatically end even if the user is regularly interacting with it during that period.

補足

S3 エンドポイントとの接続がタイムアウトする場合は"Connect timeout on endpoint URL"といったエラーメッセージが表示されることが想定されます。
その際には 弊社ブログ を参考にご対応ください。

対処法

冒頭記載の通り、「非常に多くあるオブジェクトを、●●●-log-bucket/sample-1234567890/ 配下に移動させたい」場合、前途の仕様上の理由から CloudShell では長時間かかるコマンドの実行には不向きかと思います。
本ケースの場合、有効期限を長くした一時的な認証情報 もしくは IAM 認証情報 を利用し、CloudShell 以外のコマンドプロンプト等から該当のコマンドを実行する方法をご検討ください。

参考資料