當我使用 AWS CLI 擔任 IAM 角色時,如何對 AWS STS 錯誤 "the security token included in the request is expired" 進行疑難排解?
我試著透過 AWS Command Line Interface (AWS CLI) 來擔任 AWS Identity and Access Management (IAM) 角色。我收到 "security token included in the request is expired" 錯誤。
簡短說明
使用 AWS Security Token Service (AWS STS) 為 IAM 使用者請求臨時安全憑證。您使用 AssumeRole API 呼叫所建立的臨時憑證可持續 1 至 12 小時。臨時憑證的持續時間取決於您為 IAM 角色設定的角色工作階段持續時間。臨時憑證過期後,無法重複使用。如需更多資訊,請參閱 Temporary security credentials in IAM (IAM 中的臨時安全性憑證)。
解決方法
注意: 如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤訊息,請參閱 Troubleshooting errors for the AWS CLI (對 AWS CLI 錯誤進行疑難排解)。此外,請務必使用最新版 AWS CLI。
確保您的臨時安全憑證請求可以到達 AWS 端點
若要為角色建立憑證,您需要存取金鑰 ID、私密存取金鑰和工作階段權杖。請求必須在請求時間戳記後的五分鐘內到達 AWS 端點,否則請求會遭到拒絕。如需更多資訊,請參閱 Why requests are signed (為什麼要簽署請求)。
使用設定檔來擔任 IAM 角色
具名設定檔是一組設定和憑證,您可以將其套用至 AWS CLI 命令。請確保您使用正確的憑證。
下列 AWS CLI 命令使用預設設定檔憑證:
aws s3 ls
此範例命令使用在 .config 檔案中設定的 project1 設定檔憑證:
aws s3 ls --profile project1
具有過期憑證的輸出範例:
"An error occurred (ExpiredToken) when calling the ListBuckets operation: The provided token has expired."
這些設定檔定義於 .aws 資料夾中的 .credentials 和 .config 檔案中。
Linux/macOS 的組態檔案位於 ~/.aws/config,而 Windows 的則位於 C:\Users\%USERPROFILE%\.aws\config。Linux/macOS 的憑證檔案位於 ~/.aws/credentials,而 Windows 的則位於 C:\Users\%USERPROFILE%\.aws\credentials。
若要檢查您的預設設定檔憑證,請執行下列命令:
aws configure list --profile default
輸出結果範例:
Name Value Type Location ---- ----- ---- -------- profile default manual —profile access_key TGN7 shared-credentials-file secret_key SbXb shared-credentials-file region us-east-1 config-file ~/.aws/config
若要確認針對設定檔 project1 使用相同的憑證,請執行下列命令:
aws configure list --profile project1
輸出結果範例:
Name Value Type Location ---- ----- ---- -------- profile project1 manual —profile access_key QN2X config-file secret_key LPYI config-file region eu-west-1 config-file ~/.aws/config
請注意,在輸出結果範例中,預設和 project1 設定檔可能設有不同的憑證。
您可以使用下列格式在 .aws/config 檔案中建立設定檔:
[profile project1] region = eu-west-1 aws_access_key_id = aws_secret_access_key = <secret-access-Key-for-an-IAM-role> aws_session_token = <session-token>
您可以在 .aws/credentials 檔案中建立具備下列格式的設定檔:
[project1]region = eu-west-1 aws_access_key_id = <access-Key-for-an-IAM-role> aws_secret_access_key = <secret-access-Key-for-an-IAM-role> aws_session_token = <session-token>
當您執行 AWS STS assume-role 命令時,系統會提供這些憑證,類似如下:
aws sts assume-role --role-arn arn:aws:iam::<account-number>:role/Prod-Role --role-session-name environment-prod
輸出結果範例:
{ "AssumedRoleUser": { "AssumedRoleId": "AROAXXXXXXXXXXXX:environment-prod", "Arn": "arn:aws:sts::<account-number>:assumed-role/Prod-Role/environment-prod" }, "Credentials": { "SecretAccessKey": "<secret-access-Key-for-an-IAM-role>, "SessionToken": "<session-token>", "Expiration": "2020-03-31T17:17:53Z", "AccessKeyId": "<access-Key-for-an-IAM-role>" }
注意: 您可以使用 DurationSeconds 參數,來增加 IAM 角色臨時憑證的最長工作階段持續時間的到期時間。但是,如果您使用角色鏈,則角色工作階段的持續時間上限為 1 小時。
然後,新的 assume-role API 呼叫會擷取一組新的有效憑證。在 API 呼叫後,您必須使用新的臨時憑證手動更新 ~/.aws/config 檔案。
您可以避免在每次工作階段過期時更新組態檔案。請透過類似下列的方式定義 IAM 角色的設定檔,以及在 ~/.aws/config 或 ~/.aws/credentials 檔案中擔任角色的使用者:
[profile project1] role_arn = <arn-of-IAM-role> source_profile = user1 region = <region>
請注意,您的 ~/.aws/credentials 檔案中定義了類似於下列內容的 user1:
[user1] aws_access_key_id=AKIAIOSFODNN7EXAMPLE aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
定義 source_profile 代表您不必更新 ~/.aws/config 或 ~/.aws/credentials 檔案中的臨時憑證。
下列 AWS CLI 命令列出 Amazon Simple Storage Service (Amazon S3) 儲存貯體,且 user1 的憑證位於 ~/.aws/credentials 檔案中。
aws s3 ls --profile project1
如果您使用具有 source_profile 元素的 AWS CLI,則 API 呼叫 Assume-role 會將憑證放入 .aws/cli/cache 檔案中。過期的憑證會自動在 .aws/cli/cache 檔案中更新。如果收到有關過期憑證的錯誤,您可以使用下列命令清除快取:
Linux/macOS:
rm -r ~/.aws/cli/cache
Windows:
C:\> del /s /q %UserProfile%\.aws\cli\cache
AWS CLI 會在快取中建立新的憑證。
建立環境變數以擔任 IAM 角色並驗證存取權
您可以使用 IAM 角色憑證來建立三個環境變數,以擔任類似下列項目的 IAM 角色:
Linux/macOS:
export AWS_ACCESS_KEY_ID=RoleAccessKeyID export AWS_SECRET_ACCESS_KEY=RoleSecretKey export AWS_SESSION_TOKEN=RoleSessionToken
Windows:
C:\> setx AWS_ACCESS_KEY_ID RoleAccessKeyID C:\> setx AWS_SECRET_ACCESS_KEY RoleSecretKey C:\> setx AWS_SESSION_TOKEN RoleSessionToken
若要驗證是否擔任正確的 IAM 角色,請執行下列命令:
aws sts get-caller-identity
get-caller-identity 命令會顯示用於驗證請求的 IAM 身分識別相關資訊。如需更多資訊,請參閱如何使用 AWS CLI 擔任 IAM 角色?
環境變數即使在過期後也會保留臨時快取憑證,而且不會自動續訂。請使用下列命令來檢查是否設定憑證環境變數:
Linux/macOS:
printenv | grep AWS
Windows:
C:\>set AWS
您可以使用下列命令移除過期的環境變數:
Linux/macOS:
unset AWS_ACCESS_KEY_ID unset AWS_SECRET_ACCESS_KEY unset AWS_SESSION_TOKEN
Windows:
C:\>set AWS_ACCESS_KEY_ID= C:\>set AWS_SECRET_ACCESS_KEY= C:\>set AWS_SESSION_TOKEN=
您現在可以再次使用 Assume-role API 呼叫來取得新的有效憑證,並再次設定環境變數。
注意: .aws/credentials 和 .aws/config 檔案包含您 IAM 實體的憑證詳細資訊。當您管理憑證時,請務必遵循 IAM 中的安全性最佳實務。
相關資訊
Request temporary security credentials (請求臨時安全憑證)
Update the maximum session duration for a role (更新角色的最大工作階段持續時間)
- 語言
- 中文 (繁體)

This article was reviewed and updated on 2026-04-01.
相關內容
已提問 1 年前
AWS 官方已更新 7 個月前