MeWrite Docs

ExpiredToken: The security token included in the request is expired

AWSの一時的な認証情報が期限切れの場合に発生するエラー

概要

ExpiredToken: The security token included in the request is expired は、AWSの一時的なセキュリティ認証情報(セッショントークン)が期限切れになった場合に発生するエラーです。

エラーメッセージ

An error occurred (ExpiredToken) when calling the ListBuckets operation: The security token included in the request is expired
ExpiredTokenException: The security token included in the request is expired

原因

  1. セッショントークンの期限切れ: STS で取得したトークンが期限切れ
  2. AssumeRole の有効期限: ロールの一時認証情報が期限切れ
  3. MFA セッションの期限切れ: MFA で取得したセッションが期限切れ
  4. Lambda の認証情報キャッシュ: 古い認証情報がキャッシュされている
  5. 長時間実行タスク: バッチ処理中に認証情報が期限切れ

解決策

1. 新しいセッショントークンを取得

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 現在のトークン状態を確認
aws sts get-caller-identity

# セッショントークンを再取得
aws sts get-session-token

# 出力を環境変数に設定
export AWS_ACCESS_KEY_ID=ASIAIOSFODNN7EXAMPLE
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
export AWS_SESSION_TOKEN=FwoGZXIvYXdzEBYaD...

2. AssumeRole で新しい認証情報を取得

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# ロールを assume
aws sts assume-role \
    --role-arn arn:aws:iam::123456789012:role/MyRole \
    --role-session-name my-session \
    --duration-seconds 3600

# 出力を環境変数に設定
export AWS_ACCESS_KEY_ID=$(echo $CREDS | jq -r '.Credentials.AccessKeyId')
export AWS_SECRET_ACCESS_KEY=$(echo $CREDS | jq -r '.Credentials.SecretAccessKey')
export AWS_SESSION_TOKEN=$(echo $CREDS | jq -r '.Credentials.SessionToken')

3. MFA でセッショントークンを取得

1
2
3
4
5
# MFA 付きでセッショントークンを取得
aws sts get-session-token \
    --serial-number arn:aws:iam::123456789012:mfa/user \
    --token-code 123456 \
    --duration-seconds 43200  # 最大12時間

4. プロファイルで自動更新を設定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# ~/.aws/config
[profile production]
role_arn = arn:aws:iam::123456789012:role/ProductionRole
source_profile = default
duration_seconds = 3600

# MFA 必須の場合
[profile production-mfa]
role_arn = arn:aws:iam::123456789012:role/ProductionRole
source_profile = default
mfa_serial = arn:aws:iam::123456789012:mfa/user
1
2
# プロファイルを使用(自動的に認証情報を更新)
aws s3 ls --profile production

5. SDK での自動更新

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Python (boto3) - 自動更新される
import boto3

# IAM ロールを使用(EC2, Lambda等)
# 自動的に更新される
client = boto3.client('s3')

# AssumeRole で取得(手動更新が必要)
sts_client = boto3.client('sts')
assumed_role = sts_client.assume_role(
    RoleArn='arn:aws:iam::123456789012:role/MyRole',
    RoleSessionName='my-session',
    DurationSeconds=3600
)
credentials = assumed_role['Credentials']

# 新しい認証情報でクライアントを作成
s3_client = boto3.client(
    's3',
    aws_access_key_id=credentials['AccessKeyId'],
    aws_secret_access_key=credentials['SecretAccessKey'],
    aws_session_token=credentials['SessionToken']
)

6. Lambda での対処

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Lambda では環境の認証情報が自動更新される
# ただし、長時間実行の場合は注意

import boto3

def lambda_handler(event, context):
    # 新しいクライアントを作成すると最新の認証情報を使用
    s3 = boto3.client('s3')

    # 長時間処理の場合は定期的にクライアントを再作成
    for item in long_list:
        if should_refresh():
            s3 = boto3.client('s3')  # 再作成
        process(item, s3)

7. 環境変数のクリア

1
2
3
4
5
6
7
# 期限切れの認証情報をクリア
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_SESSION_TOKEN

# IAM ロールまたは credentials ファイルの認証情報を使用
aws sts get-caller-identity

8. GitHub Actions での対処

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# OIDC を使用(推奨)- 自動的に新しいトークンを取得
jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
    steps:
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::123456789012:role/GitHubActionsRole
          aws-region: ap-northeast-1
          role-duration-seconds: 3600

      # 長時間ジョブの場合、途中で再認証
      - name: Long running task - Part 1
        run: ./part1.sh

      - name: Refresh credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::123456789012:role/GitHubActionsRole
          aws-region: ap-northeast-1

      - name: Long running task - Part 2
        run: ./part2.sh

9. 有効期限の延長

1
2
3
4
5
# AssumeRole の最大有効期限を設定(IAM ロール側)
# ロールの最大セッション期間を確認・変更
aws iam update-role \
    --role-name MyRole \
    --max-session-duration 43200  # 最大12時間

デバッグのコツ

認証情報の有効期限を確認

1
2
3
4
5
6
7
8
# 認証情報の詳細
aws sts get-caller-identity

# セッションの有効期限を確認(Python)
import boto3
session = boto3.Session()
credentials = session.get_credentials()
print(credentials.get_frozen_credentials())

関連エラー

AWS の他のエラー

最終更新: 2025-12-08