The SSO session associated with this profile has expired or is otherwise invalid
TL;DR — 你的 IAM Identity Center(AWS SSO)存取 token 失效了,因此 profile 無法為 DynamoDB 呼叫鑄造憑證。執行 aws sso login --profile <profile> 重新驗證。若登入後錯誤仍持續,你登入的是與程式碼所使用不同的 profile/工作階段,或 ~/.aws/sso/cache 下的快取 token 過時了 — 清除它並重新登入。
這是什麼意思
UnauthorizedSSOTokenError: The SSO session associated with this profile has
expired or is otherwise invalid. To refresh this SSO session run aws sso login
with the corresponding profile.以 SSO 為基礎的 profile 不儲存長效金鑰。aws sso login 在本地快取一個存取 token,而 CLI/SDK 按需將它換成短效的角色憑證。兩層都會過期:角色憑證在數分鐘到數小時後,SSO token 在你組織設定的工作階段長度後。當 token 過期(或刷新失敗)時,每個簽署的呼叫 — 包括 DynamoDB 讀寫 — 都會在抵達 AWS 前以此錯誤失敗。
為什麼會發生
- SSO 工作階段就是逾時了 — 工作階段持續時間由你的 Identity Center 管理員設定;隔夜過期很典型。
- 你登入了不同的 profile —
aws sso login對某個 profile 執行,而你的應用/工具解析到另一個(AWS_PROFILE不符,或兩個 profile 指向不同的sso_session設定)。 - 過時或損壞的 token 快取 —
~/.aws/sso/cache下的 JSON 檔不再符合 profile 的 SSO 設定(region/start URL 改變),因此工具即使在登入後仍看到「無效」的工作階段。 - 第三方工具自行解析憑證 — 直接讀取 SSO 快取的 SDK 與工具可能與 CLI 對有效性有異議(某些將 token 視為略早於其實際過期時間就過期)。
如何修正
重新驗證正確的 profile:
aws sso login --profile my-profile aws sts get-caller-identity --profile my-profile # confirm it worked確保你的程式碼使用相同的 profile — 為與 DynamoDB 通訊的行程設定
AWS_PROFILE=my-profile,並檢查~/.aws/config中重複/舊的 profile 定義。登入後仍「過期」?清除 token 快取並重新登入 —
aws sso logout是刪除快取憑證的記載方式(它們位於~/.aws/sso/cache):aws sso logout rm -rf ~/.aws/sso/cache # only if a stale cache file still lingers aws sso login --profile my-profile對長時間執行或無頭的工作負載,不要使用 SSO profile — 互動式瀏覽器登入無法無人值守地刷新。改用附掛到運算的 IAM 角色(instance/task/execution 角色)或 CI OIDC 角色。
工作階段過期得惱人地快? 工作階段持續時間是 Identity Center 設定 — 請你的管理員延長它。
桌面工具繼承相同的修正:一旦 aws sso login 成功,DynoTable 桌面應用程式便取用你刷新後的 SSO profile 並重新連到你的表格。在工作階段存活期間,DynamoDB Expression Builder 協助你將想查詢的內容轉為正確的請求。
相關錯誤
- The security token included in the request is expired — 過期的_角色_憑證,而非 SSO 工作階段。
- The security token included in the request is invalid
- Unable to locate credentials (boto3)
References
- Configuring IAM Identity Center authentication with the AWS CLI — AWS CLI User Guide
- sso login — AWS CLI Command Reference
- Set session duration for AWS accounts — IAM Identity Center User Guide
最後驗證於 2026-07-13,對照上方連結的 AWS 官方文件。