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 配置匹配(區域/起始 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 角色(例項/任務/執行角色)或一個 CI OIDC 角色。
會話過期快得煩人? 會話時長是一個 Identity Center 設定——請你的管理員延長它。
桌面工具繼承同樣的修復:一旦 aws sso login 成功,DynoTable 就會接收你重新整理後的 SSO profile 並重新連線到你的表——它每次都從 ~/.aws 重新解析憑證,因此無需重新輸入任何內容,新會話就會得到反映。
在 DynoTable 中核對
DynoTable 執行與 CLI 相同的 IAM Identity Center 裝置流 — 程序內、不需要單獨的 aws 二進位制檔案 (Connect an AWS account)。一旦aws sso login成功,應用程式將從中讀取重新整理的令牌
~/.aws/sso/cache 在下一個連線上。當 SSO 會話失效時,個人資料晶片顯示一個帶有登入的紅點;單擊它重新開啟瀏覽器流程無需重新啟動DynoTable。按⌘P驗證個人資料名稱與你在終端中登入的名稱相匹配。重新整理後,開啟一個表確認 DynamoDB 呼叫成功 — 視覺query builder
一旦憑證再次生效,就會從你的過濾器中派生 Query-vs-Scan。
相關錯誤
- The security token included in the request is expired——過期的_角色_憑證,而非 SSO 會話。
- The security token included in the request is invalid
- 找不到憑證(boto3)