跳至內容

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

1 分的閱讀內容
0

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

簡短說明

若要產生臨時憑證,您必須執行 get-role-credentials 命令。當您設定命名設定檔以使用 IAM Identity Center時,AWS Command Line Interface (AWS CLI) 會在 cd ~/.aws/sso/cache 目錄中建立 JSON 檔案。JSON 檔案包含 JSON Web 權杖 (JWT),用於透過 get-role-credentials 命令取得臨時安全憑證。存取權杖有效期為 8 小時。JSON 檔案中的 expiresAt 時間戳記顯示到期時間。

解決方法

**注意:**如果您在執行 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. 複製 accessToken 值。

  3. 執行以下 get-role-credentials 命令以取得 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

    **注意:**將 enter_the_same_sso_region_same_in_the_JSON_file 替換為 JSON 檔案中的 AWS 區域,將 permission-set-name 替換為您的角色名稱。如果使用錯誤的角色名稱,那麼您會收到錯誤訊息:「An error occurred (ForbiddenException) when calling the GetRoleCredentials operation: No access.」 如果工作階段權杖或區域與 JSON 檔案輸出不符,那麼您會收到錯誤訊息:「An error occurred (UnauthorizedException) when calling the GetRoleCredentials operation: Session token not found or invalid.」
    輸出範例:

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

相關資訊

如何使用 IAM Identity Center 權限集?