使用 設定 IAM Identity Center 身分驗證 AWS CLI - AWS Command Line Interface

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 設定 IAM Identity Center 身分驗證 AWS CLI

本主題說明如何 AWS CLI 使用 AWS IAM Identity Center (IAM Identity Center) 設定 以擷取登入資料來執行 AWS CLI 命令。使用 IAM Identity Center 驗證使用者,以取得登入資料以透過 config 檔案執行 AWS CLI 命令的方式主要有兩種:

  • (建議) SSO 權杖提供者組態。

  • 舊版不可重新整理的配置。

如需有關使用不使用帳戶 ID 和角色的承載身分驗證的資訊,請參閱《Amazon CodeCatalyst 使用者指南》中的設定 以 AWS CLI 搭配 CodeCatalyst 使用 CodeCatalyst

注意

如需搭配 AWS CLI 命令使用 IAM Identity Center 的引導程序,請參閱 教學課程:使用 IAM Identity Center 在 中執行 Amazon S3 命令 AWS CLI

主題

先決條件

  • 安裝 AWS CLI。如需詳細資訊,請參閱安裝或更新至 AWS CLI 的最新版本

  • 您必須先有辦法存取 IAM Identity Center 中的 SSO 身分驗證。選擇下列其中一種方法來存取您的 AWS 登入資料。

請遵循 AWS IAM Identity Center 使用者指南入門說明。此程序可以啟動 IAM Identity Center、建立管理使用者,並新增適當的最低權限許可集。

注意

建立套用最低權限許可的許可集。建議您使用預先定義的 PowerUserAccess 許可集,除非您的雇主已為此目的建立了自訂許可集。

結束入口網站並再次登入,以查看 AWS 帳戶或 Administrator 的程式設計存取詳細資訊和選項PowerUserAccess。在使用 SDK 時選取 PowerUserAccess

AWS 透過身分提供者的入口網站登入 。如果您的雲端管理員已授予您 PowerUserAccess(開發人員) 許可,您會看到您有權存取 AWS 帳戶 的 和許可集。您會在許可集名稱旁,看到使用該許可集手動或以程式設計方式存取帳戶的選項。

若您自訂實作,可能會產生不同體驗,例如不同的許可集名稱。若您不確定要使用哪個許可集,請聯絡您的 IT 團隊尋求協助。

透過 AWS 您的 AWS 存取入口網站登入 。如果您的雲端管理員已授予您 PowerUserAccess(開發人員) 許可,您會看到您有權存取 AWS 帳戶 的 和許可集。您會在許可集名稱旁,看到使用該許可集手動或以程式設計方式存取帳戶的選項。

請聯絡您的 IT 團隊尋求協助。

取得 IAM Identity Center 的存取權後,請執行下列動作來收集您的 IAM Identity Center 資訊:

  1. 收集您需要執行 aws configure ssoSSO Start URLSSO Region

    1. 在您的 AWS 存取入口網站中,選取您用於開發的許可集,然後選取存取金鑰連結。

    2. 取得憑證對話方塊中,選擇符合您作業系統的索引標籤。

    3. 選擇 IAM Identity Center 憑證方法,以取得 SSO Start URLSSO Region 值。

  2. 或者,從 2.22.0 版起,您可以使用發行者 URL,而不是啟動 URL。發行者 URL 位於 AWS IAM Identity Center 主控台中下列其中一個位置:

    • 儀表板頁面上,發行者 URL 位於設定摘要中。

    • 設定頁面上,發行者 URL 位於身分來源設定中。

  3. 如需要在哪些範圍註冊值的詳細資訊,請參閱《IAM Identity Center 使用者指南》中的 OAuth 2.0 存取範圍

使用 aws configure sso 精靈設定您的設定檔

若要為您的 AWS CLI設定 IAM Identity Center 設定檔:
  1. 在您偏好的終端機中執行 aws configure sso 命令。

    (Recommended) IAM Identity Center

    建立工作階段名稱、提供您的 IAM Identity Center 啟動 URL 或發行者 URL、託管 IAM Identity Center 目錄 AWS 區域 的 ,以及註冊範圍。

    $ aws configure sso SSO session name (Recommended): my-sso SSO start URL [None]: https://my-sso-portal.awsapps.com/start SSO region [None]: us-east-1 SSO registration scopes [None]: sso:account:access

    對於雙堆疊支援,請使用雙堆疊 SSO 啟動 URL:

    $ aws configure sso SSO session name (Recommended): my-sso SSO start URL [None]: https://ssoins-1234567890abcdef.portal.us-east-1.app.aws SSO region [None]: us-east-1 SSO registration scopes [None]: sso:account:access

    根據預設,從 2.22.0 版 AWS CLI 開始的 會使用 Code Exchange (PKCE) 授權的驗證金鑰,並且必須在具有瀏覽器的裝置上使用。 若要繼續使用裝置授權,請附加 --use-device-code 選項。

    $ aws configure sso --use-device-code
    Legacy IAM Identity Center

    略過工作階段名稱,並提供您的 IAM Identity Center 啟動 URL,以及託管 Identity Center 目錄的 AWS 區域。

    $ aws configure sso SSO session name (Recommended): SSO start URL [None]: https://my-sso-portal.awsapps.com/start SSO region [None]:us-east-1

    對於雙堆疊支援:

    $ aws configure sso SSO session name (Recommended): SSO start URL [None]: https://ssoins-1234567890abcdef.portal.us-east-1.app.aws SSO region [None]:us-east-1
  2. 會 AWS CLI 嘗試開啟預設瀏覽器,以進行 IAM Identity Center 帳戶的登入程序。此程序可能會提示您允許 AWS CLI 存取您的資料。由於 AWS CLI 建置在適用於 Python 的 SDK 之上,因此許可訊息可能包含botocore名稱的變化。

    • 如果 AWS CLI 無法開啟瀏覽器,系統會根據您使用的授權類型,顯示手動啟動登入程序的指示。

      PKCE authorization

      根據預設,從 2.22.0 版 AWS CLI 開始的 會使用 Code Exchange (PKCE) 授權的驗證金鑰。顯示的 URL 是唯一的 URL,開頭為:

      • IPv4:https://https://oidc.us-east-1.amazonaws.com/authorize

      • 雙堆疊:https://https://oidc.us-east-1.api.aws/authorize

      PKCE 授權 URL 必須在您登入的同一個裝置上開啟,且必須用於具有瀏覽器的裝置。

      Attempting to automatically open the SSO authorization page in your default browser. If the browser does not open or you wish to use a different device to authorize the request, open the following URL: https://oidc.us-east-1.amazonaws.com/authorize?<abbreviated>
      Device authorization

      2.22.0 版之前的 AWS CLI 使用 OAuth 2.0 裝置授權。您可以使用 --use-device-code 選項在較新版本上啟用此方法。

      裝置授權 URL 不需要在您登入的同一個裝置上開啟,且可用於有或無瀏覽器的裝置。端點格式取決於您的組態:

      • IPv4:https://https://device.sso.us-west-2.amazonaws.com/

      • 雙堆疊:https://https://device.sso.us-west-2.api.aws/

      If the browser does not open or you wish to use a different device to authorize this request, open the following URL: https://device.sso.us-west-2.amazonaws.com/ Then enter the code: QCFK-N451
  3. 從顯示的清單中選擇要使用 AWS 的帳戶。如果您有權僅使用一個帳戶,則 AWS CLI 會自動選取該帳戶並略過提示。

    There are 2 AWS accounts available to you. > DeveloperAccount, developer-account-admin@example.com (123456789011) ProductionAccount, production-account-admin@example.com (123456789022)
  4. 從顯示的清單中選擇要使用的 IAM 角色。如果只有一個角色可用,則 AWS CLI 會自動選取該角色並略過提示。

    Using the account ID 123456789011 There are 2 roles available to you. > ReadOnly FullAccess
  5. 指定預設輸出格式、要將命令傳往的預設 AWS 區域,以及設定檔名稱。如果您指定 default 做為設定檔名稱,則此設定檔會成為使用的預設設定檔。在下列範例中,使用者會輸入預設區域、預設輸出格式和設定檔的名稱。

    Default client Region [None]: us-west-2<ENTER> CLI default output format (json if not specified) [None]: json<ENTER> Profile name [123456789011_ReadOnly]: my-dev-profile<ENTER>
  6. 最後一則訊息說明完成的設定檔組態。您現在可以使用此設定檔來請求憑證。使用 aws sso login 命令來請求和擷取執行命令所需的憑證。如需說明,請參閱登入 IAM Identity Center 工作階段

這些步驟會造成在 config 檔案中建立 sso-session 區段及具名設定檔,如下所示:

IAM Identity Center
[profile my-dev-profile] sso_session = my-sso sso_account_id = 123456789011 sso_role_name = readOnly region = us-west-2 output = json [sso-session my-sso] sso_region = us-east-1 sso_start_url = https://my-sso-portal.awsapps.com/start sso_registration_scopes = sso:account:access

對於雙堆疊支援:

[profile my-dev-profile] sso_session = my-sso sso_account_id = 123456789011 sso_role_name = readOnly region = us-west-2 output = json [sso-session my-sso] sso_region = us-east-1 sso_start_url = https://ssoins-1234567890abcdef.portal.us-east-1.app.aws sso_registration_scopes = sso:account:access
Legacy IAM Identity Center
[profile my-dev-profile] sso_start_url = https://my-sso-portal.awsapps.com/start sso_region = us-east-1 sso_account_id = 123456789011 sso_role_name = readOnly region = us-west-2 output = json

對於雙堆疊支援:

[profile my-dev-profile] sso_start_url = https://ssoins-1234567890abcdef.portal.us-east-1.app.aws sso_region = us-east-1 sso_account_id = 123456789011 sso_role_name = readOnly region = us-west-2 output = json

使用 aws configure sso-session 精靈只設定 sso-session 區段

注意

此組態與舊版 IAM Identity Center 不相容。

aws configure sso-session 命令會更新 ~/.aws/config 檔案中的 sso-session 區段。執行 aws configure sso-session命令並提供您的 IAM Identity Center 啟動 URL 或發行者 URL,以及託管 AWS IAM Identity Center 目錄的區域。

$ aws configure sso-session SSO session name: my-sso SSO start URL [None]: https://my-sso-portal.awsapps.com/start SSO region [None]: us-east-1 SSO registration scopes [None]: sso:account:access

對於雙堆疊支援,請使用雙堆疊 SSO 啟動 URL:

$ aws configure sso-session SSO session name: my-sso SSO start URL [None]: https://ssoins-1234567890abcdef.portal.us-east-1.app.aws SSO region [None]: us-east-1 SSO registration scopes [None]: sso:account:access

使用 config 檔案手動設定

IAM Identity Center 組態資訊會存放在 config 檔案中,且可以使用文字編輯器進行編輯。若要手動將 IAM Identity Center 支援新增至具名設定檔,您必須將索引鍵和值新增至 config 檔案。

config 檔案的 sso-session區段用於將用於取得 SSO 存取權杖的組態變數分組,然後可用於取得 AWS 登入資料。使用下列的設定:

您可以定義 sso-session 區段,並將其與設定檔建立關聯。sso_regionsso_start_url 設定必須在 sso-session 區段中設定。一般而言,sso_account_idsso_role_name 必須在 profile 區段中設定,以讓開發套件請求 SSO 憑證。

下列範例會將開發套件設定為請求 SSO憑認證,並支援自動字符重新整理:

[profile dev] sso_session = my-sso sso_account_id = 111122223333 sso_role_name = SampleRole [sso-session my-sso] sso_region = us-east-1 sso_start_url = https://my-sso-portal.awsapps.com/start

對於雙堆疊支援,請使用雙堆疊 SSO 啟動 URL 格式:

[profile dev] sso_session = my-sso sso_account_id = 111122223333 sso_role_name = SampleRole [sso-session my-sso] sso_region = us-east-1 sso_start_url = https://ssoins-1234567890abcdef.portal.us-east-1.app.aws

這也讓 sso-session 組態能在多個設定檔中重複使用:

[profile dev] sso_session = my-sso sso_account_id = 111122223333 sso_role_name = SampleRole [profile prod] sso_session = my-sso sso_account_id = 111122223333 sso_role_name = SampleRole2 [sso-session my-sso] sso_region = us-east-1 sso_start_url = https://my-sso-portal.awsapps.com/start

對於雙堆疊支援,請使用雙堆疊 SSO 啟動 URL 格式:

[profile dev] sso_session = my-sso sso_account_id = 111122223333 sso_role_name = SampleRole [profile prod] sso_session = my-sso sso_account_id = 111122223333 sso_role_name = SampleRole2 [sso-session my-sso] sso_region = us-east-1 sso_start_url = https://ssoins-1234567890abcdef.portal.us-east-1.app.aws

然而,SSO 字符組態並非所有情況都需要 sso_account_idsso_role_name。如果您的應用程式只使用支援承載身分驗證 AWS 的服務,則不需要傳統 AWS 登入資料。承載身分驗證是一種 HTTP 身分驗證結構描述,使用稱為承載字符的安全字符。在這種情況下,sso_account_idsso_role_name 並非必要資訊。請參閱您 AWS 服務的個別指南,以判斷其是否支援承載字符授權。

此外,註冊範圍也可以設定為 sso-session 的一部分。範圍是 OAuth 2.0 中的一種機制,用於限制應用程式對使用者帳戶的存取。應用程式可以請求一個或多個範圍,則合法給應用程式的存取字符將僅限於授予的範圍。這些範圍定義為已註冊 OIDC 用戶端和用戶端擷取的存取字符進行授權所需請求的許可。下列範例設定 sso_registration_scopes 以提供列出帳戶/角色的存取權限:

[sso-session my-sso] sso_region = us-east-1 sso_start_url = https://my-sso-portal.awsapps.com/start sso_registration_scopes = sso:account:access

對於雙堆疊支援:

[sso-session my-sso] sso_region = us-east-1 sso_start_url = https://ssoins-1234567890abcdef.portal.us-east-1.app.aws sso_registration_scopes = sso:account:access

身分驗證字符會以基於工作階段名稱的檔案名稱快取至 sso/cache 目錄下的磁碟。

注意

使用舊版不可重新整理的組態,不支援自動字符重新整理。我們建議使用 SSO 字符組態。

若要手動將 IAM Identity Center 支援新增至具名設定檔,您必須將下列索引鍵和值新增至 config 檔案中的設定檔定義。

您可以在 .aws/config 檔案中包含有效的任何其他索引鍵和值。以下範例是 IAM Identity Center 設定檔:

[profile my-sso-profile] sso_start_url = https://my-sso-portal.awsapps.com/start sso_region = us-west-2 sso_account_id = 111122223333 sso_role_name = SSOReadOnlyRole region = us-west-2 output = json

對於雙堆疊支援:

[profile my-sso-profile] sso_start_url = https://ssoins-1234567890abcdef.portal.us-east-1.app.aws sso_region = us-west-2 sso_account_id = 111122223333 sso_role_name = SSOReadOnlyRole region = us-west-2 output = json

若要執行命令,您必須先進行 登入 IAM Identity Center 工作階段 來請求和擷取暫時憑證。

如需 configcredentials 檔案的詳細資訊,請參閱 AWS CLI中的組態與憑證檔案設定

登入 IAM Identity Center 工作階段

注意

登入程序可能會提示您允許 AWS CLI 存取您的資料。由於 AWS CLI 建置在適用於 Python 的 SDK 之上,因此許可訊息可能包含botocore名稱的變化。

若要擷取和快取 IAM Identity Center 憑證集,請為 AWS CLI 執行下列命令,以開啟預設瀏覽器並驗證您的 IAM Identity Center 登入。

$ aws sso login --profile my-dev-profile SSO authorization page has automatically been opened in your default browser. Follow the instructions in the browser to complete this authorization request. Successfully logged into Start URL: https://my-sso-portal.awsapps.com/start

您的 IAM Identity Center 工作階段登入資料會快取,而 AWS CLI 會使用它們來安全地擷取設定檔中指定之 IAM 角色的 AWS 登入資料。

如果 AWS CLI 無法自動開啟您的瀏覽器,系統會根據您使用的授權類型,顯示手動啟動登入程序的指示。

PKCE authorization

根據預設,從 2.22.0 版 AWS CLI 開始的 會使用 Code Exchange (PKCE) 授權的驗證金鑰。顯示的 URL 是唯一的 URL,開頭為:

  • IPv4:https://https://oidc.us-east-1.amazonaws.com/authorize

  • 雙堆疊:https://https://oidc.us-east-1.api.aws/authorize

PKCE 授權 URL 必須在您登入的同一個裝置上開啟,且必須用於具有瀏覽器的裝置。

Attempting to automatically open the SSO authorization page in your default browser. If the browser does not open or you wish to use a different device to authorize the request, open the following URL: https://oidc.us-east-1.amazonaws.com/authorize?<abbreviated>
Device authorization

2.22.0 之前的 AWS CLI 版本使用 OAuth 2.0 裝置授權。您可以使用 --use-device-code 選項在較新版本上啟用此方法。

裝置授權 URL 不需要在您登入的同一個裝置上開啟,且可用於有或無瀏覽器的裝置。

If the browser does not open or you wish to use a different device to authorize this request, open the following URL: https://device.sso.us-west-2.amazonaws.com/ Then enter the code: QCFK-N451

您也可以指定使用 aws sso login 指令的 --sso-session 參數登入時要使用的 sso-session 設定檔。sso-session 選項不適用於舊版 IAM Identity Center。

$ aws sso login --sso-session my-dev-session

自 2.22.0 版起,PKCE 授權為預設值。若要使用裝置授權登入,請新增 --use-device-code 選項。

$ aws sso login --profile my-dev-profile --use-device-code

身分驗證權杖會以基於 sso_start_url 的檔案名稱快取至 ~/.aws/sso/cache 目錄下的磁碟。

使用您的 IAM Identity Center 設定檔執行命令

登入後,您可以使用您的登入資料,使用相關聯的具名設定檔叫用 AWS CLI 命令。下列範例使用設定檔顯示命令:

$ aws sts get-caller-identity --profile my-dev-profile

只要您已登入 IAM Identity Center,且這些快取的登入資料尚未過期, 就會視需要 AWS CLI 自動續約過期的 AWS 登入資料。但是,如果您的 IAM Identity Center 憑證過期,必須重新登入您的 IAM Identity Center 帳戶,以明確更新它們。

登出您的 IAM Identity Center 工作階段

完成使用 IAM Identity Center 設定檔後,您可以讓憑證過期,或執行下列命令來刪除快取的憑證。

$ aws sso logout Successfully signed out of all SSO profiles.

疑難排解

如果您使用 遇到問題 AWS CLI,請參閱 故障診斷 的錯誤 AWS CLI 以取得疑難排解步驟。

相關資源

其他資源如下所示。