如何使用 AWS CLI 取得 IAM Identity Center 使用者的暫時憑證?

1 分的閱讀內容
0

我想取得 AWS IAM Identity Center 使用者的暫時憑證。

簡短說明

在設定已命名設定檔使用 IAM Identity Center 時,會在 $ cd ~/.aws/sso/cache 目錄中建立 JSON 檔案。JSON 檔案包含 JSON Web 權杖 (JWT),用於透過 get-role-credentials API 等效命令取得暫時安全憑證。存取權杖有效期為 8 小時。您可以在 JSON 檔案的 expiresAt 時間標記中查看到期時間。必須使用 get-role-credentials 命令來重新驗證過期的權杖。

解決方法

使用 AWS Command Line Interface (AWS CLI) 取得 IAM Identity Center 使用者的暫時憑證。

**注意:**如果您在執行 AWS CLI 命令時收到錯誤,請參閱對 AWS CLI 錯誤進行疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本

取得暫時憑證

在 AWS CLI 中,完成下列步驟:

  1. 開啟 JSON 檔案並複製存取權杖:

    $ cat 535a8450b05870c9045c8a7b95870.json
    {"startUrl": "https://my-sso-portal.awsapps.com/start", "region": "us-east-1", "accessToken": "eyJlbmMiOiJBM….", "expiresAt": "2020-06-17T10:02:08UTC"}
  2. 執行 get-role-credentials AWS CLI 命令,以取得 IAM Identity Center 使用者的憑證:

    $ aws sso get-role-credentials --account-id 123456789012 --role-name permission-set-name --access-token eyJlbmMiOiJBM…. --region enter_the_same_sso_region_same_in_the_JSON_file

    範例輸出:

    {    "roleCredentials": {
            "accessKeyId": "ASIA*************",
            "secretAccessKey": "**********************************",
            "sessionToken": "****************************************",
            "expiration": 1592362463000
        }
    }
  3. 將憑證設定為環境變數

對錯誤訊息進行疑難排解

"An error occurred (ForbiddenException) when calling the GetRoleCredentials operation: No access."

由於 AWS CLI 中的角色名稱不正確,可能會發生此錯誤。檢查角色名稱以確認它是否正確。

"An error occurred (UnauthorizedException) when calling the GetRoleCredentials operation: Session token not found or invalid."

發生此錯誤是因為工作階段權杖或 AWS 區域不正確。確定 AWS CLI 命令中的區域與 JSON 檔案輸出中的區域相同。

相關資訊

如何使用 IAM Identity Center 許可集?

AWS 官方
AWS 官方已更新 1 年前