

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

# 的身分驗證和存取憑證 AWS CLI
<a name="cli-chap-authentication"></a>

當您使用 服務進行開發 AWS 時，您必須建立 的 AWS CLI AWS 身分驗證方式。若要為 設定程式設計存取的登入資料 AWS CLI，請選擇下列其中一個選項。選項是按照建議順序排列的。


****  

| 身分驗證類型 | 用途 | 指示 | 
| --- | --- | --- | 
|  AWS 管理主控台登入資料  | **（建議）** 使用您的主控台登入資料登入 AWS CLI，以使用短期登入資料。如果您使用根、IAM 使用者或聯合身分搭配 IAM 進行 AWS 帳戶存取，則建議使用 | [使用主控台登入資料登入 AWS 進行本機開發](cli-configure-sign-in.md) | 
|  IAM Identity Center 人力使用者短期憑證  | 使用 IAM Identity Center 人力資源使用者的短期憑證。安全最佳實務是 AWS Organizations 搭配 IAM Identity Center 使用 。它結合了短期憑證與使用者目錄，例如內建的 IAM Identity Center 目錄或 Active Directory。 | [使用 設定 IAM Identity Center 身分驗證 AWS CLI](cli-configure-sso.md) | 
| IAM 使用者短期憑證 | 使用比長期憑證更安全的 IAM 使用者短期憑證。如果憑證遭到入侵，憑證在過期前被利用的時間有限。 | [使用 的短期登入資料進行驗證 AWS CLI](cli-authentication-short-term.md) | 
| Amazon EC2 執行個體上的 IAM 或 IAM Identity Center 使用者。 | 使用 Amazon EC2 執行個體中繼資料，以使用指派給 Amazon EC2 執行個體的角色來查詢臨時憑證。 | [在 中使用 Amazon EC2 執行個體中繼資料做為登入資料 AWS CLI](cli-configure-metadata.md) | 
| 擔任許可的角色 | 配對另一個憑證方法，並擔任可暫時存取您的使用者可能無法存取之 AWS 服務 的角色。 | [在 中使用 IAM 角色 AWS CLI](cli-configure-role.md) | 
| IAM 使用者長期憑證 | (不建議) 使用不會過期的長期憑證。 | [使用 的 IAM 使用者登入資料進行驗證 AWS CLI](cli-authentication-user.md) | 
| IAM 或 IAM Identity Center 人力使用者外部儲存裝置  | (不建議) 配對另一個憑證方法，但將憑證值存放於 AWS CLI之外的位置。此方法的安全性僅與存放憑證的外部位置一樣安全。 | [在 中使用外部程序來採購登入資料 AWS CLI](cli-configure-sourcing-external.md) | 

## 組態和憑證優先順序
<a name="cli-chap-authentication-precedence"></a>

登入資料和組態設定位於多個位置，例如系統或使用者環境變數、本機 AWS 組態檔案，或在命令列上明確宣告為參數。某些身分驗證優先於其他身分驗證。 AWS CLI 身分驗證設定優先順序如下：

1. **[命令列選項](cli-configure-options.md)** – 覆寫任何其他位置 (例如 `--region`、`--output` 和 `--profile` 參數) 的設定。

1. **[環境變數](cli-configure-envvars.md)** – 您可以將數值存放在環境變數中。

1. **[擔任角色](cli-configure-role.md)**—透過組態或 [https://docs.aws.amazon.com/cli/latest/reference/sts/assume-role.html](https://docs.aws.amazon.com/cli/latest/reference/sts/assume-role.html) 命令，獲得 IAM 角色許可。

1. **[擔任具有 Web 身分的角色](cli-configure-role.md)**—透過組態或 [https://docs.aws.amazon.com/cli/latest/reference/sts/assume-role-with-web-identity.html](https://docs.aws.amazon.com/cli/latest/reference/sts/assume-role-with-web-identity.html) 命令，使用 Web 身分獲得 IAM 角色許可。

1. **[AWS IAM Identity Center](cli-configure-files.md)** – 存放在 `config` 檔案中的 IAM Identity Center 組態設定會在您執行 `aws configure sso` 命令時更新。之後當您執行 `aws sso login` 命令時，系統會驗證憑證。`config` 檔案在 Linux 或 macOS 上位於 `~/.aws/config`，在 Windows 上位於 `C:\Users\USERNAME\.aws\config`。

1. **[憑證檔案](cli-configure-files.md)** – 當您執行 `aws configure` 命令時，會更新 `credentials` 和 `config` 檔案。`credentials` 檔案在 Linux 或 macOS 上位於 `~/.aws/credentials`，在 Windows 上位於 `C:\Users\USERNAME\.aws\credentials`。

1. **[自訂程序](cli-configure-sourcing-external.md)**—從外部來源取得憑證。

1. **[組態檔](cli-configure-files.md)** – 當您執行 `aws configure` 命令時，會更新 `credentials` 和 `config` 檔案。`config` 檔案在 Linux 或 macOS 上位於 `~/.aws/config`，在 Windows 上位於 `C:\Users\USERNAME\.aws\config`。

1. **[容器憑證](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html)** – 您可以將 IAM 角色與您的每一個 Amazon Elastic Container Service (Amazon ECS) 任務定義關聯起來。然後，該角色的臨時憑證就可供該任務的容器使用。如需詳細資訊，請參閱《Amazon Elastic Container Service 開發人員指南》**中的[任務 IAM 角色](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html)。

1. **[Amazon EC2 執行個體設定檔憑證](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html)** – 您可以將 IAM 角色與您的每一個 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體關聯起來。然後，該角色的臨時憑證就可供執行個體中執行的程式碼使用。憑證是透過 Amazon EC2 中繼資料服務傳遞。如需詳細資料，請參閱《Amazon EC2 使用者指南》**中的 [Amazon EC2 IAM 角色](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html)，以及《IAM 使用者指南》**中的[使用執行個體設定檔](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html)。

## 本區段的其他主題
<a name="cli-chap-authentication-topics"></a>
+ [使用 設定 IAM Identity Center 身分驗證 AWS CLI](cli-configure-sso.md)
+ [使用 的短期登入資料進行驗證 AWS CLI](cli-authentication-short-term.md)
+ [在 中使用 IAM 角色 AWS CLI](cli-configure-role.md)
+ [使用 的 IAM 使用者登入資料進行驗證 AWS CLI](cli-authentication-user.md)
+ [在 中使用 Amazon EC2 執行個體中繼資料做為登入資料 AWS CLI](cli-configure-metadata.md)
+ [在 中使用外部程序來採購登入資料 AWS CLI](cli-configure-sourcing-external.md)

# 使用主控台登入資料登入 AWS 進行本機開發
<a name="cli-configure-sign-in"></a>

您可以使用現有的 AWS 管理主控台登入憑證，以程式設計方式存取 AWS 服務。在瀏覽器型身分驗證流程之後， AWS 會產生臨時登入資料，可用於 CLI、 AWS Tools for PowerShell AWS 和 AWS SDKs等本機開發工具。此功能可簡化設定和管理 AWS CLI 登入資料的程序，尤其是如果您偏好互動式身分驗證，而不是管理長期存取金鑰。

透過此程序，您可以使用在初始帳戶設定期間建立的根登入資料、IAM 使用者或身分提供者的聯合身分進行身分驗證，而 AWS CLI 會自動為您管理臨時登入資料。這種方法無需在本機存放長期登入資料，可增強安全性。

當您執行 `aws login`命令時，您可以從作用中主控台工作階段中選取 ，或透過瀏覽器型身分驗證流程登入，這會自動產生臨時登入資料。CLI 最多會自動重新整理這些登入資料 12 小時。

設定完成後，您的工作階段就可以在 AWS CLI 和其他 AWS SDKs和工具中使用。

**Topics**
+ [先決條件](#cli-configure-sign-in-prerequisites)
+ [使用 AWS `aws login`命令登入 CLI。](#cli-configure-sign-in-login-command)
+ [使用您的設定檔執行命令](#cli-configure-sign-in-with-profile)
+ [使用 aws 登出命令登出您的工作階段](#cli-configure-sign-in-sign-out)
+ [疑難排解](#cli-configure-sign-in-troubleshooting)
+ [相關資源](#cli-configure-sign-in-resources)

## 先決條件
<a name="cli-configure-sign-in-prerequisites"></a>
+ 安裝 AWS CLI。如需詳細資訊，請參閱[安裝或更新至最新版本的 AWS CLI](getting-started-install.md)。使用 `aws login`命令需要 2.32.0 的最低版本。
+ 以根使用者、IAM 使用者或透過 IAM 聯合身分登入 AWS 管理主控台的存取權。如果您使用 IAM Identity Center，請[使用 設定 IAM Identity Center 身分驗證 AWS CLI](cli-configure-sso.md)改為前往 。
+ 確保 IAM 身分具有適當的許可。將 [SignInLocalDevelopmentAccess](https://docs.aws.amazon.com/signin/latest/userguide/security-iam-awsmanpol.html) 受管政策連接至您的 IAM 使用者、角色或群組。如果您以根使用者身分登入，則不需要額外的許可。

## 使用 AWS `aws login`命令登入 CLI。
<a name="cli-configure-sign-in-login-command"></a>

執行 `aws login`命令，以使用您現有的 AWS 管理主控台登入資料進行身分驗證。如果您先前尚未設定設定檔，系統會提示您提供其他資訊。若要登入或設定設定檔，請遵循下列步驟。

1. 在您偏好的終端機中執行 `aws login` 命令。

   ```
   $ aws login
   ```

   若要登入具名設定檔或建立新的設定檔，請使用 `--profile`選項。

   ```
   $ aws login --profile my-dev-profile
   ```
   + 如果這是新的設定檔或未指定 AWS 區域，CLI AWS 會提示您提供區域。

     ```
     No AWS region has been configured. The AWS region is the geographic location of 
     your AWS resources. 
     
     If you've used AWS before and already have resources in your account, tell us 
     which region they were created in. If you haven't created resources in your account 
     before, you can pick the region closest to you: 
     https://docs.aws.amazon.com/global-infrastructure/latest/regions/aws-regions.html. 
     You are able to change the region in the CLI at any time with the command 
     `aws configure set region NEW_REGION`.
     AWS Region [us-east-1]:
     ```
   + 如果使用 CLI AWS 的裝置沒有瀏覽器，您可以使用 `--remote`選項提供 URL，供您在已啟用瀏覽器的裝置上開啟。

     ```
     $ aws login --remote
     ```
**注意**  
您可以控制對相同裝置 (`aws login`) 和跨裝置 (`aws login --remote`) 身分驗證的存取。在任何相關的 IAM 政策中使用下列資源 ARNs。  
`arn:aws:signin:region:account-id:oauth2/public-client/localhost` — 使用此 ARN 搭配 進行相同的裝置身分驗證`aws login`。
`arn:aws:signin:region:account-id:oauth2/public-client/remote` — 使用此 ARN 與 進行跨裝置身分驗證`aws login --remote`。

1. CLI AWS 會嘗試開啟您 AWS 帳戶登入程序的預設瀏覽器。

   ```
   Attempting to open the login page for `us-east-1` in your default browser. 
   If the browser does not open, use the following URL to complete your login:
   https://signin.us-east-1.amazonaws.com/authorize?<abbreviated>
   
   If you cannot connect to this URL, make sure that you have specified a valid region.
   ```
   + 如果您使用 `--remote`選項，系統會根據您使用的授權類型，顯示手動啟動登入程序的指示。顯示的 URL 是從 https://us-east-1.signin.amazonaws.com/authorize 開始的唯一 URL。完成瀏覽器登入後，您需要將產生的授權碼複製並貼回 CLI。

     ```
     Browser will not be automatically opened.
     Please visit the following URL:
     https://region.signin.amazonaws.com/authorize?<abbreviated>
     
     Please enter the authorization code displayed in the browser:
     ```

1. 在瀏覽器中，從顯示的清單中選取要使用的登入資料，然後返回您的終端機。
   + 如果您要設定的設定檔具有先前設定的登入工作階段，其不符合您的新工作階段，CLI AWS 會提示您確認您正在切換對應至現有設定檔的工作階段。

     ```
     Profile signin is already configured to use session arn:aws:iam::0123456789012:user/ReadOnly. 
     Do you want to overwrite it to use arn:aws:iam::0123456789012:user/Admin instead? (y/n):.
     ```

1. 最後一則訊息說明完成的設定檔組態。您現在可以使用此設定檔來請求憑證。使用 `aws login` 命令來請求和擷取執行命令所需的憑證。

   身分驗證字符會快取至`.aws/login/cache`目錄下的磁碟，並以已解析設定檔為基礎的檔案名稱。

### 產生的組態檔案
<a name="cli-configure-sign-in-generated-config"></a>

這些步驟會在 `config` 檔案中建立如下所示的`default`設定檔：

```
[default]
login_session = arn:aws:iam::0123456789012:user/username
region = us-east-1
```

## 使用您的設定檔執行命令
<a name="cli-configure-sign-in-with-profile"></a>

登入後，您可以使用 登入資料來叫用具有相關聯設定檔的 AWS CLI 命令。下列範例使用 `default`設定檔呼叫 `get-caller-identity`命令：

```
$ aws sts get-caller-identity
```

若要登入特定工作階段，請使用 `--profile`選項。

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

CLI AWS 和 SDKs 會視需要每 15 分鐘自動重新整理快取的登入資料。整體工作階段的有效期最長為 IAM 主體的設定工作階段持續時間 （最長 12 小時），之後您必須`aws login`再次執行。

## 使用 aws 登出命令登出您的工作階段
<a name="cli-configure-sign-in-sign-out"></a>

使用工作階段完成後，您可以讓登入資料過期，或執行 `aws logout`命令來刪除快取的登入資料。如果命令列或`AWS_PROFILE`環境變數中未指定設定檔，命令會將您登出預設設定檔。下列範例會將您登出預設設定檔。

```
$ aws logout
```

若要登出特定工作階段，請使用 `--profile`選項。

```
$ aws logout --profile my-dev-profile
```

若要登出使用登入憑證的所有設定檔，請使用 `--all`選項。

```
$ aws logout --all
```

### 快取的登入資料
<a name="cli-configure-sign-in-sharing-login-creds"></a>

根據預設，臨時快取憑證以及重新整理憑證所需的中繼資料會存放在 `~/.aws/login/cache` Linux 和 macOS 或 Windows `%USERPROFILE%\.aws\login\cache`上的 。

若要將短期更新快取存放在替代位置，請設定 `AWS_LOGIN_CACHE_DIRECTORY`環境變數。

#### 共用登入憑證做為程序憑證
<a name="cli-configure-sign-in-cached-credentials"></a>

舊版的 AWS SDKs或其他開發工具可能尚不支援主控台登入資料。做為解決方法，您可以設定 AWS CLI 做為程序登入資料提供者。CLI 會視需要繼續重新整理登入資料，同時與設定為使用 Credential\$1process 描述檔的工具共用登入資料。

在此範例中，使用 AWS CLI 先登入設定檔登入：

```
$ aws login --profile signin
```

然後，使用 Credential\$1process 選項手動設定設定檔，該選項會指向登入設定檔。現在，您可以設定 SDKs或工具來使用程序描述檔，這會叫用 CLI 從登入描述檔共用登入資料。

```
[profile signin]
login_session = arn:aws:iam::0123456789012:user/username
region = us-east-1

[profile process] 
credential_process = aws configure export-credentials --profile signin --format process
region = us-east-1
```

## 疑難排解
<a name="cli-configure-sign-in-troubleshooting"></a>

此頁面包含針對使用 CLI AWS 主控台登入資料進行 AWS 本機開發的登入問題進行疑難排解的建議。

**注意**  
若要疑難排解使用 CLI AWS 可能遇到的其他問題，請參閱 [故障診斷 的錯誤 AWS CLI](cli-chap-troubleshooting.md)。

### 使用「aws 登入」後發生 ExpiredToken 或 AccessDeniedException 錯誤
<a name="expired-token-login-troubleshooting"></a>

在`aws login`為指定設定檔執行 之後執行 AWS CLI 命令時，您可能會遇到過期或無效的登入資料錯誤。

```
$ aws s3 ls

An error occurred (ExpiredToken) when calling the ListBuckets operation: The provided token has expired.
```

**可能原因：**您在該設定檔中混合了現有的登入資料和新的登入資料

執行 `aws configure list`或 `aws configure list --profile <profile name>`以列印 CLI 從 解析預設或指定設定檔憑證的位置。

如果 TYPE 資料欄不是登入，這表示目標設定檔中仍有不同類型的登入資料。

在此範例中，憑證會從主目錄中的共用憑證檔案解析，其優先順序高於登入憑證。

```
$  aws configure list
NAME       : VALUE                    : TYPE             : LOCATION
profile    : <not set>                : None             : None
access_key : ****************MPLE     : shared-credentials-file :
secret_key : ****************EKEY     : shared-credentials-file :
region     : us-east-1                : config-file      : ~/.aws/config
```

若要解決此問題，請從您的組態和目標設定檔的登入資料檔案手動移除任何現有的登入資料。執行後，您應該會在`aws configure list`再次執行時看到登入憑證。

```
$  aws configure list
NAME       : VALUE                    : TYPE             : LOCATION
profile    : <not set>                : None             : None
access_key : ****************MPLE     : login            :
secret_key : ****************EKEY     : login            :
region     : us-east-1                : config-file      : ~/.aws/config
```

或者，使用 `--debug`選項會顯示 CLI 從何處解析登入資料。

### 執行「aws login」時封鎖網路存取的防火牆
<a name="firewall-blocking-login-troubleshooting"></a>

執行時`aws login`，您可能會遇到來自防火牆軟體的快顯視窗或訊息，以防止 CLI AWS 存取您的網路

**可能原因：**您的防火牆或安全軟體阻止 AWS CLI 開啟用於處理 OAuth 回呼的連接埠。

若要避免此問題，請改用 `--remote`選項。這將提示您複製並貼上授權碼，而不是使用 OAuth 回呼。

```
$ aws login --remote
```

## 相關資源
<a name="cli-configure-sign-in-resources"></a>

其他資源如下所示。
+ [安裝或更新至最新版本的 AWS CLI](getting-started-install.md)
+ [`aws login` CLI AWS 第 2 版參考中的](https://docs.aws.amazon.com/cli/latest/reference/)
+ [`aws logout` CLI AWS 第 2 版參考中的](https://docs.aws.amazon.com/cli/latest/reference/)

# 使用 設定 IAM Identity Center 身分驗證 AWS CLI
<a name="cli-configure-sso"></a>

本主題說明如何 AWS CLI 使用 AWS IAM Identity Center (IAM Identity Center) 設定 以擷取登入資料來執行 AWS CLI 命令。使用 IAM Identity Center 對使用者進行身分驗證，以取得登入資料以透過 `config` 檔案執行 AWS CLI 命令的方式主要有兩種：
+ **(建議)** SSO 權杖提供者組態。
+ 舊版不可重新整理的配置。

如需有關使用不使用帳戶 ID 和角色的承載身分驗證的資訊，請參閱《Amazon [ CodeCatalyst 使用者指南》中的設定 以 AWS CLI 搭配 CodeCatalyst 使用](https://docs.aws.amazon.com/codecatalyst/latest/userguide/set-up-cli.html) 。 * CodeCatalyst *

**注意**  
如需搭配 AWS CLI 命令使用 IAM Identity Center 的引導程序，請參閱 [教學課程：使用 IAM Identity Center 在 中執行 Amazon S3 命令 AWS CLI](cli-configure-sso-tutorial.md)。

**主題**
+ [先決條件](#cli-configure-sso-prereqs)
+ [使用 `aws configure sso` 精靈設定您的設定檔](#cli-configure-sso-configure)
+ [使用 `aws configure sso-session` 精靈只設定 `sso-session` 區段](#cli-configure-sso-session)
+ [使用 `config` 檔案手動設定](#cli-configure-sso-manual)
+ [登入 IAM Identity Center 工作階段](#cli-configure-sso-login)
+ [使用您的 IAM Identity Center 設定檔執行命令](#cli-configure-sso-use)
+ [登出您的 IAM Identity Center 工作階段](#cli-configure-sso-logout)
+ [疑難排解](#cli-configure-sso-tshoot)
+ [相關資源](#cli-configure-sso-resources)

## 先決條件
<a name="cli-configure-sso-prereqs"></a>
+ 安裝 AWS CLI。如需詳細資訊，請參閱[安裝或更新至最新版本的 AWS CLI](getting-started-install.md)。
+ 您必須先有辦法存取 IAM Identity Center 中的 SSO 身分驗證。選擇下列其中一種方法來存取您的 AWS 登入資料。

### 我沒有透過 IAM Identity Center 建立存取權
<a name="idc-access"></a>

請遵循 *AWS IAM Identity Center 使用者指南*的[入門](https://docs.aws.amazon.com/singlesignon/latest/userguide/getting-started.html)說明。此程序可以啟動 IAM Identity Center、建立管理使用者，並新增適當的最低權限許可集。

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

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

### 我已經 AWS 可以透過我的雇主管理的聯合身分提供者 （例如 Azure AD 或 Okta) 存取
<a name="federated-access"></a>

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

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

### 我已經 AWS 可以透過我的雇主管理的 AWS 存取入口網站存取
<a name="accessportal-access"></a>

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

### 我已經能夠 AWS 透過由我的雇主管理的聯合身分自訂身分提供者存取
<a name="customfederated-access"></a>

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

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

1. 收集您需要執行 `aws configure sso` 的 `SSO Start URL` 和 `SSO Region` 值

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

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

   1. 選擇 **IAM Identity Center 憑證**方法，以取得 `SSO Start URL` 和 `SSO Region` 值。

1. 或者，從 2.22.0 版起，您可以使用發行者 URL，而不是啟動 URL。發行者 URL 位於 AWS IAM Identity Center 主控台中下列其中一個位置：
   + 在**儀表板**頁面上，發行者 URL 位於設定摘要中。
   + 在**設定**頁面上，發行者 URL 位於**身分來源**設定中。

1. 如需要在哪些範圍註冊值的詳細資訊，請參閱《IAM Identity Center 使用者指南》**中的 [OAuth 2.0 存取範圍](https://docs.aws.amazon.com/singlesignon/latest/userguide/customermanagedapps-saml2-oauth2.html#oidc-concept)。

## 使用 `aws configure sso` 精靈設定您的設定檔
<a name="cli-configure-sso-configure"></a>

**若要為您的 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
   ```

------

1. 會 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
     ```

------

1. 從顯示的清單中選擇要使用 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)
   ```

1. 從顯示的清單中選擇要使用的 IAM 角色。如果只有一個角色可用， AWS CLI 會自動選取該角色並略過提示。

   ```
   Using the account ID 123456789011
   There are 2 roles available to you.
   > ReadOnly
     FullAccess
   ```

1. 指定[預設輸出格式](cli-configure-files.md#cli-config-output)、要將命令傳往的[預設 AWS 區域](cli-configure-files.md#cli-config-region)，以及[設定檔名稱](cli-configure-files.md)。如果您指定 `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>
   ```

1. 最後一則訊息說明完成的設定檔組態。您現在可以使用此設定檔來請求憑證。使用 `aws sso login` 命令來請求和擷取執行命令所需的憑證。如需說明，請參閱[登入 IAM Identity Center 工作階段](#cli-configure-sso-login)。

### 產生的組態檔案
<a name="cli-configure-sso-generated"></a>

這些步驟會造成在 `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` 區段
<a name="cli-configure-sso-session"></a>

**注意**  
此組態與舊版 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` 檔案手動設定
<a name="cli-configure-sso-manual"></a>

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

### IAM Identity Center 使用者組態
<a name="cli-configure-sso-manual-config"></a>

`config` 檔案的 `sso-session`區段用於將用於取得 SSO 存取權杖的組態變數分組，然後可用於取得 AWS 登入資料。使用下列的設定：
+ **(必要)** `sso\$1start\$1url`
+ **(必要)** `sso\$1region`
+ `sso\$1account\$1id`
+ `sso\$1role\$1name`
+ `sso\$1registration\$1scopes`

您可以定義 `sso-session` 區段，並將其與設定檔建立關聯。`sso_region` 和 `sso_start_url` 設定必須在 `sso-session` 區段中設定。一般而言，`sso_account_id` 和 `sso_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_id` 和 `sso_role_name`。如果您的應用程式只使用支援承載身分驗證 AWS 的服務，則不需要傳統 AWS 登入資料。承載身分驗證是一種 HTTP 身分驗證結構描述，使用稱為承載字符的安全字符。在這種情況下，`sso_account_id` 和 `sso_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` 目錄下的磁碟。

### 舊版 IAM Identity Center 組態檔案
<a name="cli-configure-sso-manual-legacy"></a>

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

若要手動將 IAM Identity Center 支援新增至具名設定檔，您必須將下列索引鍵和值新增至 `config` 檔案中的設定檔定義。
+ `sso\$1start\$1url`
+ `sso\$1region`
+ `sso\$1account\$1id`
+ `sso\$1role\$1name`

您可以在 `.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 工作階段](#cli-configure-sso-login) 來請求和擷取暫時憑證。

如需 `config` 和 `credentials` 檔案的詳細資訊，請參閱 [中的組態和登入資料檔案設定 AWS CLI](cli-configure-files.md)。

## 登入 IAM Identity Center 工作階段
<a name="cli-configure-sso-login"></a>

**注意**  
登入程序可能會提示您允許 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 無法開啟您的瀏覽器
<a name="cli-configure-sso-login-browser"></a>

如果 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 設定檔執行命令
<a name="cli-configure-sso-use"></a>

登入後，您可以使用 登入資料來呼叫具有相關聯具名設定檔的 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 工作階段
<a name="cli-configure-sso-logout"></a>

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

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

## 疑難排解
<a name="cli-configure-sso-tshoot"></a>

如果您使用 遇到問題 AWS CLI，請參閱 [故障診斷 的錯誤 AWS CLI](cli-chap-troubleshooting.md) 以取得疑難排解步驟。

## 相關資源
<a name="cli-configure-sso-resources"></a>

其他資源如下所示。
+ [AWS CLI 的 AWS IAM Identity Center 概念](cli-configure-sso-concepts.md)
+ [教學課程：使用 IAM Identity Center 在 中執行 Amazon S3 命令 AWS CLI](cli-configure-sso-tutorial.md)
+ [安裝或更新至最新版本的 AWS CLI](getting-started-install.md)
+ [中的組態和登入資料檔案設定 AWS CLI](cli-configure-files.md)
+ 《AWS CLI 第 2 版參考》**中的 [https://docs.aws.amazon.com/cli/latest/reference/configure/sso.html](https://docs.aws.amazon.com/cli/latest/reference/configure/sso.html)
+ 《AWS CLI 第 2 版參考》**中的 [https://docs.aws.amazon.com/cli/latest/reference/configure/sso-session.html](https://docs.aws.amazon.com/cli/latest/reference/configure/sso-session.html)
+ 《AWS CLI 第 2 版參考》**中的 [https://docs.aws.amazon.com/cli/latest/reference/sso/login.html](https://docs.aws.amazon.com/cli/latest/reference/sso/login.html)
+ 《AWS CLI 第 2 版參考》**中的 [https://docs.aws.amazon.com/cli/latest/reference/sso/logout.html](https://docs.aws.amazon.com/cli/latest/reference/sso/logout.html)
+ 《Amazon[ CodeCatalyst 使用者指南》中的設定 以 AWS CLI 搭配](https://docs.aws.amazon.com/codecatalyst/latest/userguide/set-up-cli.html) CodeCatalyst 使用 * CodeCatalyst *
+ 《IAM Identity Center 使用者指南》**中的 [OAuth 2.0 存取範圍](https://docs.aws.amazon.com/singlesignon/latest/userguide/customermanagedapps-saml2-oauth2.html#oidc-concept)
+ 《IAM Identity Center 使用者指南》**中的[入門教學課程](https://docs.aws.amazon.com/singlesignon/latest/userguide/tutorials.html)

# AWS CLI 的 AWS IAM Identity Center 概念
<a name="cli-configure-sso-concepts"></a>

本主題說明 AWS IAM Identity Center (IAM Identity Center) 的主要概念。IAM Identity Center 是一種雲端型 IAM 服務，透過與現有身分提供者 (IdP) 整合，簡化跨多個 AWS 帳戶、應用程式、SDK 和工具的使用者存取管理。它透過集中式使用者入口網站啟用安全的單一登入、許可管理和稽核，簡化組織的身分和存取控管。

**Topics**
+ [什麼是 IAM Identity Center](#cli-configure-sso-concepts-what)
+ [條款](#cli-configure-sso-terms)
+ [IAM Identity Center 的運作方式](#cli-configure-sso-concepts-process)
+ [其他資源](#cli-configure-sso-concepts-resources)

## 什麼是 IAM Identity Center
<a name="cli-configure-sso-concepts-what"></a>

IAM Identity Center 是一種雲端型身分和存取管理 (IAM) 服務，可讓您集中管理對多個 AWS 帳戶 和商業應用程式的存取。

它提供使用者入口網站，在此授權使用者可以使用其現有的公司憑證，來存取他們獲授予許可的 AWS 帳戶 和應用程式。這可讓組織強制執行一致的安全政策，並簡化使用者存取管理。

無論您使用哪種 IdP，IAM Identity Center 都會將這些區別擷取出來。例如，您可以按照部落格文章 [IAM Identity Center 的下一個演變](https://aws.amazon.com/blogs/aws/the-next-evolution-in-aws-single-sign-on/)中所述的方法連線 Microsoft Azure AD。

**注意**  
如需使用不需帳戶 ID 和角色之承載驗證的相關資訊，請參閱 *Amazon CodeCatalyst 使用者指南*中的[設定以將 AWS CLI 搭配 CodeCatalyst 使用](https://docs.aws.amazon.com/codecatalyst/latest/userguide/set-up-cli.html)。

## 條款
<a name="cli-configure-sso-terms"></a>

使用 IAM Identity Center 時的常用術語如下：

**身分提供者 (IdP)**  
身分管理系統，例如 IAM Identity Center、Microsoft Azure AD、Okta 或您自己的公司目錄服務。

**AWS IAM Identity Center**  
IAM Identity Center 是 AWS 擁有的 IdP 服務。SDK 以前稱為 AWS 單一登入，其與工具會保留 `sso` API 命名空間以確保回溯相容性。如需詳細資訊，請參閱《AWS IAM Identity Center 使用者指南》**中的 [IAM Identity Center 重新命名](https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html#renamed)。

**AWS 存取入口網站 URL、SSO 啟動 URL、啟動 URL**  
您組織的唯一 IAM Identity Center URL，以存取授權的 AWS 帳戶、服務和資源。

**發行者 URL**  
您組織的唯一 IAM Identity Center 發行者 URL，以程式設計方式存取授權的 AWS 帳戶、服務和資源。從 AWS CLI 的 2.22.0 版開始，發行者 URL 可以與啟動 URL 交替使用。

**聯合**  
在 IAM Identity Center 與身分提供者之間建立信任的程序，以啟用單一登入 (SSO)。

**AWS 帳戶**  
您透過 AWS IAM Identity Center 為使用者提供其存取權的 AWS 帳戶。

**許可集、AWS 憑證、憑證、sigv4 憑證**  
可指派給使用者或群組以授予 AWS 服務 存取權的預先定義許可集合。

**註冊範圍、存取範圍、範圍**  
範圍是 OAuth 2.0 中的一種機制，用於限制應用程式對使用者帳戶的存取。應用程式可以請求一個或多個範圍，則核發給應用程式的存取權杖僅限於授予的範圍。如需範圍的相關資訊，請參閱《IAM Identity Center 使用者指南》**中的[存取範圍](https://docs.aws.amazon.com/singlesignon/latest/userguide/customermanagedapps-saml2-oauth2.html#oidc-concept)。

**權杖、重新整理權杖、存取權杖**  
權杖是在身分驗證時核發給您的臨時安全憑證。這些權杖包含有關您的身分和已授予您之許可的資訊。  
當您透過 IAM Identity Center 入口網站存取 AWS 資源或應用程式時，會向 AWS 呈現您的權杖以進行身分驗證和授權。這可讓 AWS 驗證您的身分，並確保您擁有執行所請求動作的必要許可。  
系統會根據工作階段名稱將身分驗證權杖快取至以 JSON 檔案名稱為名之 `~/.aws/sso/cache` 目錄下的磁碟。

**Session (工作階段)**  
IAM Identity Center 工作階段是指驗證使用者身分並授權使用者存取 AWS 資源或應用程式的一段時間。當使用者登入 IAM Identity Center 入口網站時，會建立工作階段，且使用者的權杖在指定的持續時間內有效。如需設定工作階段持續時間的詳細資訊，請參閱《AWS IAM Identity Center 使用者指南》**中的[設定工作階段持續時間](https://docs.aws.amazon.com/singlesignon/latest/userguide/howtosessionduration.html)。  
在工作階段期間，您可以在不同的 AWS 帳戶和應用程式之間瀏覽，無需重新驗證身分，只要其工作階段保持作用中狀態即可。當工作階段過期時，請再次登入以續約您的存取權。  
IAM Identity Center 工作階段有助於提供無縫使用者體驗，同時透過限制使用者存取憑證的有效性來強制執行安全最佳實務。

**使用 PKCE、PKCE、代碼交換證明金鑰授予的授權碼**  
自 2.22.0 版起，代碼交換證明金鑰 (PKCE) 是具有瀏覽器之裝置的 OAuth 2.0 身分驗證授予流程。PKCE 是一種簡單且安全的方式，可讓您透過 Web 瀏覽器來驗證身分和取得從桌面和行動裝置存取您 AWS 資源的同意。這是預設的授權行為。如需 PKCE 的詳細資訊，請參閱《AWS IAM Identity Center 使用者指南》**中的[使用 PKCE 授予授權碼](https://docs.aws.amazon.com/singlesignon/latest/userguide/customermanagedapps-saml2-oauth2.html#auth-code-grant-pkce)。

**裝置授權授予**  
具有或不具有 Web 瀏覽器之裝置的 OAuth 2.0 身分驗證授予流程。如需設定工作階段持續時間的詳細資訊，請參閱《AWS IAM Identity Center 使用者指南》**中的[裝置授權授予](https://docs.aws.amazon.com/singlesignon/latest/userguide/customermanagedapps-saml2-oauth2.html#device-auth-grant)。

## IAM Identity Center 的運作方式
<a name="cli-configure-sso-concepts-process"></a>

IAM Identity Center 會與您組織的身分提供者整合，例如 IAM Identity Center、Microsoft Azure AD 或 Okta。使用者對此身分提供者進行身分驗證，然後 IAM Identity Center 將這些身分映射到您 AWS 環境中的適當許可和存取權。

下列 IAM Identity Center 工作流程假設您已將 AWS CLI 設定為使用 IAM Identity Center：

1. 在您偏好的終端機中執行 `aws sso login` 命令。

1. 登入您的 AWS 存取入口網站 以啟動新的工作階段。
   + 當您啟動新的工作階段時，會收到重新整理權杖和已快取的存取權杖。
   + 如果您已經有作用中的工作階段，則現有的工作階段會重複使用，並在現有的工作階段過期時過期。

1. IAM Identity Center 會根據您在 `config` 檔案中設定的設定檔，取得適當的許可集，並授予相關 AWS 帳戶 和應用程式的存取權。

1. AWS CLI、SDK 和工具會使用您擔任的 IAM 角色來呼叫 AWS 服務，例如建立 Amazon S3 儲存貯體，直到該工作階段過期為止。

1. 每小時會檢查 IAM Identity Center 的存取權杖一次，並自動使用重新整理權杖重新整理。
   + 如果存取權杖已過期，SDK 或工具會使用重新整理權杖來取得新的存取權杖。然後會比較這些權杖的工作階段持續時間，如果重新整理權杖未過期，則 IAM Identity Center 會提供新的存取權杖。
   + 如果重新整理權杖已過期，則不會提供新的存取權杖，且您的工作階段已結束。

1. 工作階段會在重新整理權杖過期後結束，或在您使用 `aws sso logout` 命令手動登出時結束。快取的憑證會移除。若要繼續使用 IAM Identity Center 來存取服務，您必須使用 `aws sso login` 命令啟動新的工作階段。

## 其他資源
<a name="cli-configure-sso-concepts-resources"></a>

其他資源如下所示。
+ [使用 設定 IAM Identity Center 身分驗證 AWS CLI](cli-configure-sso.md)
+ [教學課程：使用 IAM Identity Center 在 中執行 Amazon S3 命令 AWS CLI](cli-configure-sso-tutorial.md)
+ [安裝或更新至最新版本的 AWS CLI](getting-started-install.md)
+ [中的組態和登入資料檔案設定 AWS CLI](cli-configure-files.md)
+ 《AWS CLI 第 2 版參考》**中的 [https://docs.aws.amazon.com/cli/latest/reference/configure/sso.html](https://docs.aws.amazon.com/cli/latest/reference/configure/sso.html)
+ 《AWS CLI 第 2 版參考》**中的 [https://docs.aws.amazon.com/cli/latest/reference/configure/sso-session.html](https://docs.aws.amazon.com/cli/latest/reference/configure/sso-session.html)
+ 《AWS CLI 第 2 版參考》**中的 [https://docs.aws.amazon.com/cli/latest/reference/sso/login.html](https://docs.aws.amazon.com/cli/latest/reference/sso/login.html)
+ 《AWS CLI 第 2 版參考》**中的 [https://docs.aws.amazon.com/cli/latest/reference/sso/logout.html](https://docs.aws.amazon.com/cli/latest/reference/sso/logout.html)
+ 《Amazon CodeCatalyst 使用者指南》**中的[設定以將 AWS CLI 搭配 CodeCatalyst 使用](https://docs.aws.amazon.com/codecatalyst/latest/userguide/set-up-cli.html)
+ 《AWS IAM Identity Center 使用者指南》**中的 [IAM Identity Center 重新命名](https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html#renamed)
+ *IAM Identity Center 使用者指南*中的 [OAuth 2.0 存取範圍](https://docs.aws.amazon.com/singlesignon/latest/userguide/customermanagedapps-saml2-oauth2.html#oidc-concept)
+ 《AWS IAM Identity Center 使用者指南》**中的[設定工作階段持續時間](https://docs.aws.amazon.com/singlesignon/latest/userguide/howtosessionduration.html)
+ 《IAM Identity Center 使用者指南》**中的[入門教學課程](https://docs.aws.amazon.com/singlesignon/latest/userguide/tutorials.html)

# 教學課程：使用 IAM Identity Center 在 中執行 Amazon S3 命令 AWS CLI
<a name="cli-configure-sso-tutorial"></a>

本主題說明如何設定 AWS CLI 以使用目前的 AWS IAM Identity Center (IAM Identity Center) 驗證使用者，以擷取 Amazon Simple Storage Service (Amazon S3) 的登入資料以執行 AWS Command Line Interface (AWS CLI) 命令。

**Topics**
+ [步驟 1：在 IAM Identity Center 中驗證身分](#cli-configure-sso-tutorial-authentication)
+ [步驟 2：收集您的 IAM Identity Center 資訊](#cli-configure-sso-tutorial-gather)
+ [步驟 1：建立 Amazon S3 儲存貯體](#cli-configure-sso-tutorial-buckets)
+ [步驟 4：安裝 AWS CLI](#cli-configure-sso-tutorial-install)
+ [步驟 5：設定您的 AWS CLI 設定檔](#cli-configure-sso-tutorial-configure)
+ [步驟 6：登入 IAM Identity Center](#cli-configure-sso-tutorial-login.title)
+ [步驟 7：執行 Amazon S3 命令](#cli-configure-sso-tutorial-commands)
+ [步驟 8：登出 IAM Identity Center](#cli-configure-sso-tutorial-logout)
+ [步驟 9：清除資源](#cli-configure-sso-tutorial-cleanup)
+ [疑難排解](#cli-configure-sso-tutorial-tshoot)
+ [其他資源](#cli-configure-sso-tutorial-resources.title)

## 步驟 1：在 IAM Identity Center 中驗證身分
<a name="cli-configure-sso-tutorial-authentication"></a>

取得 IAM Identity Center 內 SSO 身分驗證的存取權。選擇下列其中一種方法來存取您的 AWS 登入資料。

### 我沒有透過 IAM Identity Center 建立存取權
<a name="idc-access"></a>

請遵循 *AWS IAM Identity Center 使用者指南*的[入門](https://docs.aws.amazon.com/singlesignon/latest/userguide/getting-started.html)說明。此程序可以啟動 IAM Identity Center、建立管理使用者，並新增適當的最低權限許可集。

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

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

### 我已經 AWS 可以透過我的雇主管理的聯合身分提供者 （例如 Azure AD 或 Okta) 存取
<a name="federated-access"></a>

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

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

### 我已經 AWS 可以透過我的雇主管理的 AWS 存取入口網站存取
<a name="accessportal-access"></a>

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

### 我已經能夠 AWS 透過由我的雇主管理的聯合身分自訂身分提供者存取
<a name="customfederated-access"></a>

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

## 步驟 2：收集您的 IAM Identity Center 資訊
<a name="cli-configure-sso-tutorial-gather"></a>

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

1. 收集您需要執行 `aws configure sso` 的 `SSO Start URL` 和 `SSO Region` 值

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

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

   1. 選擇 **IAM Identity Center 憑證**方法，以取得 `SSO Start URL` 和 `SSO Region` 值。

1. 或者，從 2.22.0 版起，您可以使用新發行者 URL，而不是啟動 URL。發行者 URL 位於 AWS IAM Identity Center 主控台中下列其中一個位置：
   + 在**儀表板**頁面上，發行者 URL 位於設定摘要中。
   + 在**設定**頁面上，發行者 URL 位於**身分來源**設定中。

1. 如需要在哪些範圍註冊值的詳細資訊，請參閱《IAM Identity Center 使用者指南》**中的 [OAuth 2.0 存取範圍](https://docs.aws.amazon.com/singlesignon/latest/userguide/customermanagedapps-saml2-oauth2.html#oidc-concept)。

## 步驟 1：建立 Amazon S3 儲存貯體
<a name="cli-configure-sso-tutorial-buckets"></a>

登入 AWS 管理主控台 ，並在 [https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/)：// 開啟 Amazon S3 主控台。

在本教學課程中，建立數個儲存貯體以供稍後在清單中擷取。

## 步驟 4：安裝 AWS CLI
<a name="cli-configure-sso-tutorial-install"></a>

為您的作業系統安裝 AWS CLI 下列指示。如需詳細資訊，請參閱[安裝或更新至最新版本的 AWS CLI](getting-started-install.md)。

安裝完成後，您可以開啟偏好的終端並執行下列命令來驗證安裝。這應該會顯示您已安裝的 版本 AWS CLI。

```
$ aws --version
```

## 步驟 5：設定您的 AWS CLI 設定檔
<a name="cli-configure-sso-tutorial-configure"></a>

使用下列其中一種方法來設定您的設定檔

### 使用 `aws configure sso` 精靈設定您的設定檔
<a name="li-configure-sso-tutorial-configure-wizard"></a>

`config` 檔案的 `sso-session`區段用於將用於取得 SSO 存取權杖的組態變數分組，然後可用於取得 AWS 登入資料。使用下列的設定：
+ **(必要)** `sso\$1start\$1url`
+ **(必要)** `sso\$1region`
+ `sso\$1account\$1id`
+ `sso\$1role\$1name`
+ `sso\$1registration\$1scopes`

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

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

```
$ 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
```

代碼交換證明金鑰 (PKCE) 授權自 2.22.0 版起為 AWS CLI 的預設值，且必須在具有瀏覽器的裝置上使用。若要繼續使用裝置授權，請附加 `--use-device-code` 選項。

```
$ aws configure sso --use-device-code
```

### 使用 `config` 檔案手動設定
<a name="cli-configure-sso-tutorial-configure-manual"></a>

`config` 檔案的 `sso-session`區段用於將用於取得 SSO 存取權杖的組態變數分組，然後可用於取得 AWS 登入資料。使用下列的設定：
+ **(必要)** `sso\$1start\$1url`
+ **(必要)** `sso\$1region`
+ `sso\$1account\$1id`
+ `sso\$1role\$1name`
+ `sso\$1registration\$1scopes`

定義 `sso-session` 區段並將其與設定檔相關聯。`sso_region` 和 `sso_start_url` 必須在 `sso-session` 區段內設定。一般而言，`sso_account_id` 和 `sso_role_name` 必須在 `profile` 區段中設定，以讓開發套件請求 SSO 憑證。

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

```
[profile my-dev-profile]
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_registration_scopes = sso:account:access
```

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

```
[profile my-dev-profile]
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_registration_scopes = sso:account:access
```

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

## 步驟 6：登入 IAM Identity Center
<a name="cli-configure-sso-tutorial-login.title"></a>

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

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

```
$ aws sso login --profile my-dev-profile
```

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

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

## 步驟 7：執行 Amazon S3 命令
<a name="cli-configure-sso-tutorial-commands"></a>

若要列出您先前建立的儲存貯體，請使用 [https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html](https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html) 命令。以下範例列出您的所有 Amazon S3 儲存貯體。

```
$ aws s3 ls
2018-12-11 17:08:50 my-bucket
2018-12-14 14:55:44 my-bucket2
```

## 步驟 8：登出 IAM Identity Center
<a name="cli-configure-sso-tutorial-logout"></a>

完成使用 IAM Identity Center 設定檔後，請執行下列命令來刪除快取的憑證。

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

## 步驟 9：清除資源
<a name="cli-configure-sso-tutorial-cleanup"></a>

完成本教學課程後，請清除任何您在本教學課程中建立但不再需要的資源，包括 Amazon S3 儲存貯體。

## 疑難排解
<a name="cli-configure-sso-tutorial-tshoot"></a>

如果您使用 遇到問題 AWS CLI，請參閱 [故障診斷 的錯誤 AWS CLI](cli-chap-troubleshooting.md) 以取得常見的故障診斷步驟。

## 其他資源
<a name="cli-configure-sso-tutorial-resources.title"></a>

其他資源如下所示。
+ [AWS CLI 的 AWS IAM Identity Center 概念](cli-configure-sso-concepts.md)
+ [使用 設定 IAM Identity Center 身分驗證 AWS CLI](cli-configure-sso.md)
+ [安裝或更新至最新版本的 AWS CLI](getting-started-install.md)
+ [中的組態和登入資料檔案設定 AWS CLI](cli-configure-files.md)
+ 《AWS CLI 第 2 版參考》**中的 [https://docs.aws.amazon.com/cli/latest/reference/configure/sso.html](https://docs.aws.amazon.com/cli/latest/reference/configure/sso.html)
+ 《AWS CLI 第 2 版參考》**中的 [https://docs.aws.amazon.com/cli/latest/reference/configure/sso-session.html](https://docs.aws.amazon.com/cli/latest/reference/configure/sso-session.html)
+ 《AWS CLI 第 2 版參考》**中的 [https://docs.aws.amazon.com/cli/latest/reference/sso/login.html](https://docs.aws.amazon.com/cli/latest/reference/sso/login.html)
+ 《AWS CLI 第 2 版參考》**中的 [https://docs.aws.amazon.com/cli/latest/reference/sso/logout.html](https://docs.aws.amazon.com/cli/latest/reference/sso/logout.html)
+ 《Amazon[ CodeCatalyst 使用者指南》中的設定 以 AWS CLI 搭配](https://docs.aws.amazon.com/codecatalyst/latest/userguide/set-up-cli.html) CodeCatalyst 使用 * CodeCatalyst *
+ 《IAM Identity Center 使用者指南》**中的 [OAuth 2.0 存取範圍](https://docs.aws.amazon.com/singlesignon/latest/userguide/customermanagedapps-saml2-oauth2.html#oidc-concept)
+ 《IAM Identity Center 使用者指南》**中的[入門教學課程](https://docs.aws.amazon.com/singlesignon/latest/userguide/tutorials.html)

# 使用 的短期登入資料進行驗證 AWS CLI
<a name="cli-authentication-short-term"></a>

建議您設定 SDK 或工具，以延長的工作階段持續時間選項使用 [IAM Identity Center 驗證](https://docs.aws.amazon.com/sdkref/latest/guide/access-sso.html)。不過，您可以複製並使用 AWS 存取入口網站中提供的臨時登入資料。若這些憑證過期，便需要複製新憑證。您可以在設定檔中使用臨時憑證，或用作系統屬性和環境變數的值。

1. [登入 AWS 存取入口網站](https://docs.aws.amazon.com/singlesignon/latest/userguide/howtosignin.html)。

1. 請依照[這些指示](https://docs.aws.amazon.com/singlesignon/latest/userguide/howtogetcredentials.html)，從 AWS 存取入口網站複製 IAM 角色登入資料。

   1. 針對連結指示中的步驟 2，選擇針對您的開發需求授予存取權 AWS 的帳戶和 IAM 角色名稱。此角色的名稱通常如 **PowerUserAccess** 或 **Developer**。

   1. 針對步驟 4，請選取**將設定檔新增至 AWS 憑證檔案**，然後複製該內容。

1. 建立或開啟共用的 `credentials` 檔案。這個檔案是位於 Linux 和 macOS 系統上的 `~/.aws/credentials` 和 Windows 上的 `%USERPROFILE%\.aws\credentials`。如需詳細資訊，請參閱 [中的組態和登入資料檔案設定 AWS CLI](cli-configure-files.md)。

1. 將以下文字新增至共用的 `credentials` 檔案。以您複製的憑證取代範例值。

   ```
   [default] 
   aws_access_key_id = AKIAIOSFODNN7EXAMPLE 
   aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
   aws_session_token = IQoJb3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZ2luX2IQoJb3JpZVERYLONGSTRINGEXAMPLE
   ```

1. 將您偏好的預設區域和格式新增至共用的 `config` 檔案。

   ```
   [default]
   region=us-west-2
   output=json
   
   [profile user1]
   region=us-east-1
   output=text
   ```

SDK 在建立服務用戶端時，會存取這些臨時憑證並用於每個要求。在步驟 2a 中選擇的 IAM 角色設定會決定[臨時憑證的有效時長](https://docs.aws.amazon.com/singlesignon/latest/userguide/howtosessionduration.html)。最長持續時間為 12 小時。

每當您的憑證過期時，請重複這些步驟。

# 在 中使用 IAM 角色 AWS CLI
<a name="cli-configure-role"></a>

[AWS Identity and Access Management (IAM) 角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles.html)是一種授權工具，可讓使用者取得額外的 （或不同的） 許可，或取得在不同 AWS 帳戶中執行動作的許可。

**Topics**
+ [先決條件](#cli-role-prereqs)
+ [使用 IAM 角色概觀](#cli-role-overview)
+ [設定和使用角色](#cli-role-prepare)
+ [使用多重要素驗證](#cli-configure-role-mfa)
+ [跨帳戶角色和外部 ID](#cli-configure-role-xaccount)
+ [指定角色工作階段名稱以更輕鬆地稽核](#cli-configure-role-session-name)
+ [擔任具有 Web 身分的角色](#cli-configure-role-oidc)
+ [清除快取的憑證](#cli-configure-role-cache)

## 先決條件
<a name="cli-role-prereqs"></a>

若要執行 `iam` 命令，您需要安裝和設定 AWS CLI。這包括設定已設定的設定檔，因為假設角色與另一個憑證方法配對。如需詳細資訊，請參閱 [安裝或更新至最新版本的 AWS CLI](getting-started-install.md)。

## 使用 IAM 角色概觀
<a name="cli-role-overview"></a>

您可以透過在 `~/.aws/config` 檔案中定義角色的描述檔，將 AWS Command Line Interface (AWS CLI) 設定為使用 IAM 角色。

以下範例顯示名為 `marketingadmin` 的角色描述檔。如果您使用 `--profile marketingadmin`（或使用 [AWS\$1PROFILE 環境變數](cli-configure-envvars.md)指定） 執行命令， AWS CLI 會使用在個別設定檔中定義的登入資料`user1`來擔任具有 Amazon Resource Name (ARN) 的角色`arn:aws:iam::123456789012:role/marketingadminrole`。您可以執行指派給該角色的許可所允許的任何操作。

```
[profile marketingadmin]
role_arn = arn:aws:iam::123456789012:role/marketingadminrole
source_profile = user1
```

接著，您就可以指定 `source_profile` 來指向另一個具名描述檔，其中包含有許可使用此角色的使用者登入資料。在上述範例中，`marketingadmin` 描述檔使用 `user1` 描述檔中的憑證。當您指定 AWS CLI 命令要使用設定檔 時`marketingadmin`， AWS CLI 會自動查詢連結`user1`設定檔的登入資料，並使用它們來請求指定 IAM 角色的臨時登入資料。CLI 在背景中使用 [sts:AssumeRole](https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html) 操作實現這一點。然後，這些臨時憑證就會用來執行所要求的 AWS CLI 命令。指定的角色必須已連接允許請求的 AWS CLI 命令執行的 IAM 許可政策。

若要從 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體或 Amazon Elastic Container Service (Amazon ECS) 容器內執行 AWS CLI 命令，您可以使用連接至執行個體設定檔或容器的 IAM 角色。如果您未指定描述檔或未設定環境變數，則會直接使用該角色。這可讓您避免將長時間存留的存取金鑰存放在您的執行個體。您也可以使用這些執行個體或容器角色來僅取得另一個角色的憑證。若要這樣做，請使用 `credential_source` (而不是 `source_profile`) 以指定如何尋找憑證。`credential_source` 屬性支援下列數值：
+ `Environment` – 從環境變數擷取來源憑證。
+ `Ec2InstanceMetadata` – 使用連接到 Amazon EC2 執行個體描述檔的 IAM 角色。
+ `EcsContainer` – 使用連接到 Amazon ECS 容器的 IAM 角色。

以下範例顯示經由參考 Amazon EC2 執行個體描述檔而使用的相同 `marketingadminrole` 角色。

```
[profile marketingadmin]
role_arn = arn:aws:iam::123456789012:role/marketingadminrole
credential_source = Ec2InstanceMetadata
```

當您叫用角色時，您會有其他選項可以取得，例如使用多重要素驗證、外部 ID (由第三方公司用來存取其用戶端資源)。您也可以指定可在 AWS CloudTrail 日誌中更輕鬆地稽核的唯一角色工作階段名稱。

## 設定和使用角色
<a name="cli-role-prepare"></a>

當您使用指定 IAM 角色的設定檔執行命令時， AWS CLI 會使用來源設定檔的登入資料來呼叫 AWS Security Token Service (AWS STS) 並請求指定角色的臨時登入資料。來源描述檔中的使用者必須有許可針對指定的描述檔中的角色呼叫 `sts:assume-role`。此角色必須有信任關係而允許來源描述檔中的使用者使用該角色。針對角色來擷取並使用臨時憑證的過程就稱為*擔任角色*。

在 IAM 中建立一個角色，並使用您希望使用者按照《AWS Identity and Access Management 使用者指南》**中的[建立角色以將許可委派給 IAM 使用者](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-user.html)下的過程來承擔的許可。如果角色和來源描述檔的 使用者在同一帳戶中，則您可以在設定角色的信任關係時輸入自己的帳戶 ID。

建立角色後，修改信任關係以允許 使用者承擔此角色。

以下範例顯示可連接到角色的信任政策。此政策允許帳戶 (123456789012) 中的任何使用者擔任角色，但***前提是***該帳戶的管理員明確將 `sts:AssumeRole` 許可授予使用者。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::123456789012:root"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
```

------

信任政策不會實際授予許可。帳戶的管理員必須連結含有適當許可的政策，才能將擔任角色的許可委派給個別使用者。以下範例顯示可連接到使用者的政策，該政策只允許使用者擔任 `marketingadminrole` 角色。如需有關授予使用者擔任角色之權限的詳細資訊，請參閱《IAM 使用者指南》**中的[授予使用者許可以切換角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_permissions-to-switch.html)。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "sts:AssumeRole",
      "Resource": "arn:aws:iam::123456789012:role/marketingadminrole"
    }
  ]
}
```

------

使用者不需要額外的許可，即可使用角色描述檔執行 AWS CLI 命令。反之，執行命令所需的許可來自於連接到*角色*的許可。您可以將許可政策連接到角色，以指定可以對哪些 AWS 資源執行哪些動作。如需有關將許可附加至角色 (運作方式與使用者相同) 的詳細資訊，請參閱《IAM 使用者指南》**中的[變更 IAM 使用者的許可](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_change-permissions.html)。

既然您已正確設定角色描述檔、角色許可、角色信任關係和使用者許可，您可以在命令列叫用 `--profile` 選項來使用該角色。例如，以下使用本主題開頭範例所定義的 `marketingadmin` 角色所連接的許可，呼叫 Amazon S3 `ls` 命令。

```
$ aws s3 ls --profile marketingadmin
```

若要使用多個呼叫的角色，可以從命令列為目前工作階段設定 `AWS_PROFILE` 環境變數。因為已定義環境變數，您不需要在每個命令中指定 `--profile` 選項。

**Linux 或 macOS**

```
$ export AWS_PROFILE=marketingadmin
```

**Windows**

```
C:\> setx AWS_PROFILE marketingadmin
```

如需設定使用者與角色的詳細資訊，請參閱《IAM 使用者指南》**中的 [IAM 身分 (使用者、使用者群組和角色)](https://docs.aws.amazon.com/IAM/latest/UserGuide/id.html) 和 [IAM 角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id-roles.html)。

## 使用多重要素驗證
<a name="cli-configure-role-mfa"></a>

為了提高安全性，當使用者嘗試使用角色設定檔進行呼叫時，您可以要求使用者提供由多重要素驗證 (MFA) 裝置、U2F 裝置或行動應用程式產生的一次性金鑰。

首先，您可以選擇修改 IAM 角色的信任關係來要求 MFA。這可防止任何人在使用該角色之前未先使用 MFA 進行驗證。相關範例請參閱下列範例中的 `Condition` 行。此政策可讓名為 `anika` 的使用者擔任政策連接的角色，前提是使用者已使用 MFA 進行驗證。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": { "AWS": "arn:aws:iam::123456789012:user/anika" },
      "Action": "sts:AssumeRole",
      "Condition": { "Bool": { "aws:multifactorAuthPresent": true } }
    }
  ]
}
```

------

接著，在角色設定檔中新增一行，指定使用者的 MFA 裝置的 ARN。以下範例 `config` 檔案項目顯示兩個角色設定檔，都使用使用者 `anika` 的存取金鑰來請求角色 `cli-role` 的臨時登入資料。使用者 `anika` 具有擔任角色的許可，由角色的信任政策所授予。

```
[profile role-without-mfa]
region = us-west-2
role_arn= arn:aws:iam::128716708097:role/cli-role
source_profile=cli-user

[profile role-with-mfa]
region = us-west-2
role_arn= arn:aws:iam::128716708097:role/cli-role
source_profile = cli-user
mfa_serial = arn:aws:iam::128716708097:mfa/cli-user

[profile cli-user]
region = us-west-2
output = json
```

`mfa_serial` 設定可以採取 ARN，如圖所示，或硬體 MFA 字符的序號。

第一個設定檔 `role-without-mfa` 不需要 MFA。不過，由於連接到該角色的上述範例信任政策要求 MFA，因此使用此設定檔來執行命令的任何嘗試都會失敗。

```
$ aws iam list-users --profile role-without-mfa

An error occurred (AccessDenied) when calling the AssumeRole operation: Access denied
```

第二個設定檔項目 `role-with-mfa` 識別要使用的 MFA 裝置。當使用者嘗試使用此設定檔執行 AWS CLI 命令時， 會 AWS CLI 提示使用者輸入 MFA 裝置提供的一次性密碼 (OTP)。如果 MFA 驗證成功，命令會執行請求操作。OTP 不會顯示在螢幕上。

```
$ aws iam list-users --profile role-with-mfa
Enter MFA code for arn:aws:iam::123456789012:mfa/cli-user:
{
    "Users": [
        {
            ...
```

## 跨帳戶角色和外部 ID
<a name="cli-configure-role-xaccount"></a>

您可以藉由將角色設定為跨帳戶角色，使 使用者能夠使用屬於不同帳戶的角色。在角色建立期間，將角色類型設定為 **Another AWS account** (其他 AWS 帳戶)，如[建立角色以將許可委派給 IAM 使用者](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-user.html)所述。或者，您可以選取 **Require MFA (需要 MFA)**。如 [使用多重要素驗證](#cli-configure-role-mfa) 中所描述的，**Require MFA** (需要 MFA) 選項在信任關係中設定適當的條件。

如果您使用[外部 ID](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-user_externalid.html) 以進一步控制誰可使用跨帳戶的角色，則您還必須將 `external_id` 參數新增至角色描述檔。這通常是在另一個帳戶由公司或組織外的某人控制時才使用。

```
[profile crossaccountrole]
role_arn = arn:aws:iam::234567890123:role/SomeRole
source_profile = default
mfa_serial = arn:aws:iam::123456789012:mfa/saanvi
external_id = 123456
```

## 指定角色工作階段名稱以更輕鬆地稽核
<a name="cli-configure-role-session-name"></a>

當多人共用一個角色時，稽核就更具挑戰。您想要將呼叫的每個操作與呼叫該動作的個人建立關聯。不過，當該個人使用某個角色時，個人擔任的角色會是與呼叫操作不同的動作，因此您必須手動將兩者建立關聯。

您可以在使用者擔任角色時指定唯一的角色工作階段名稱，藉此簡化此操作。您可以將 `role_session_name` 參數新增至指定角色的 `config` 檔案中的每個具名描述檔。`role_session_name` 值會傳遞到 `AssumeRole` 操作，並成為角色工作階段的 ARN 的一部分。它也包含在所有記錄操作的 AWS CloudTrail 日誌中。

例如，您可以建立以角色為基礎的設定檔，如下所示。

```
[profile namedsessionrole]
role_arn = arn:aws:iam::234567890123:role/SomeRole
source_profile = default
role_session_name = Session_Maria_Garcia
```

這會導致角色工作階段中具有下列 ARN。

```
arn:aws:iam::234567890123:assumed-role/SomeRole/Session_Maria_Garcia
```

此外，所有 AWS CloudTrail 日誌都會在每個操作擷取的資訊中包含角色工作階段名稱。

## 擔任具有 Web 身分的角色
<a name="cli-configure-role-oidc"></a>

您可以設定設定檔，以指示 AWS CLI 應該使用 [Web 聯合身分和 Open ID Connect (OIDC)](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_oidc.html) 擔任角色。當您在設定檔中指定此項目時， AWS CLI 會自動為您進行對應的 AWS STS `AssumeRoleWithWebIdentity`呼叫。

**注意**  
當您指定使用 IAM 角色的設定檔時， AWS CLI 會進行適當的呼叫來擷取臨時登入資料。這些憑證會儲存在 `~/.aws/cli/cache` 中。指定相同設定檔的後續 AWS CLI 命令會使用快取的暫時登入資料，直到過期為止。此時， AWS CLI 會自動重新整理登入資料。

若要使用 Web 聯合身分擷取並使用臨時憑證，您可以在共用描述檔中指定以下組態值。

[role\$1arn](#cli-configure-role)  
指定要擔任的角色 ARN。

web\$1identity\$1token\$1file  
指定檔案的路徑，該檔案包含由身分提供者提供的 OAuth 2.0 存取字符或 OpenID Connect ID 字符。 AWS CLI 會載入此檔案，並傳遞其內容作為 `WebIdentityToken` 操作的 `AssumeRoleWithWebIdentity` 引數。

[role\$1session\$1name](#cli-configure-role-session-name)  
指定套用到此擔任角色工作階段的選用名稱。

下列是擔任具有 Web 身分角色描述檔所需的最少量組態的組態範例。

```
# In ~/.aws/config

[profile web-identity]
role_arn=arn:aws:iam:123456789012:role/RoleNameToAssume
web_identity_token_file=/path/to/a/token
```

您也可以使用 [環境變數](cli-configure-envvars.md) 提供此組態。

AWS\$1ROLE\$1ARN  
要擔任的角色 ARN。

AWS\$1WEB\$1IDENTITY\$1TOKEN\$1FILE  
Web 身分字符檔案的路徑。

AWS\$1ROLE\$1SESSION\$1NAME  
套用到此擔任角色工作階段的名稱。

**注意**  
這些環境變數目前只適用於擔任具有 Web 身分提供者的角色。它們不適用於一般擔任角色供應商組態。

## 清除快取的憑證
<a name="cli-configure-role-cache"></a>

當您使用角色時， 會在本機 AWS CLI 快取臨時登入資料，直到過期為止。下次您嘗試使用它們時， AWS CLI 會嘗試代表您續約它們。

如果角色的臨時憑證被[撤銷](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_revoke-sessions.html)，則不會自動續約，而嘗試使用它們會失敗。不過，您可以刪除快取，強制 AWS CLI 擷取新的登入資料。

**Linux 或 macOS**

```
$ rm -r ~/.aws/cli/cache
```

**Windows**

```
C:\> del /s /q %UserProfile%\.aws\cli\cache
```

# 使用 的 IAM 使用者登入資料進行驗證 AWS CLI
<a name="cli-authentication-user"></a>

**警告**  
為避免安全風險，在開發專用軟體或使用真實資料時，請勿使用 IAM 使用者進行身分驗證。相反地，搭配使用聯合功能和身分提供者，例如 [AWS IAM Identity Center](https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html)。

本節說明如何以 IAM 使用者進行基本設定。其中包含使用 `config` 和 `credentials` 檔案的安全憑證。要改為查看 AWS IAM Identity Center的配置說明，請參閱 [使用 設定 IAM Identity Center 身分驗證 AWS CLI](cli-configure-sso.md)。

**Contents**
+ [步驟 1：建立 IAM 使用者](#cli-authentication-user-create)
+ [步驟 2：取得您的存取金鑰](#cli-authentication-user-get)
+ [步驟 3：設定 AWS CLI](#cli-authentication-user-configure.title)
  + [使用 `aws configure`](#cli-authentication-user-configure-wizard)
  + [透過 .CSV 檔案匯入存取金鑰](#cli-authentication-user-configure-csv)
  + [直接編輯 `config` 和 `credentials` 檔案](#cli-authentication-user-configure-csv.titlecli-authentication-user-configure-file)
+ [（選用） 搭配 IAM 使用者憑證使用多重驗證](#cli-authentication-user-configure-csv.titlecli-authentication-user-mfa)

## 步驟 1：建立 IAM 使用者
<a name="cli-authentication-user-create"></a>

按照 *IAM 使用者指南*中的[建立 IAM 使用者 (主控台)](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html#id_users_create_console) 程序來建立 IAM 使用者。
+ 針對**權限選項**，請選擇**直接連接政策**做為您指派權限給此使用者的方式。
+ 多數「入門」SDK 教學都使用 Amazon S3 服務做為範例。若要讓應用程式能夠完整存取 Amazon S3，請選取要連接至此使用者的 `AmazonS3FullAccess` 政策。

## 步驟 2：取得您的存取金鑰
<a name="cli-authentication-user-get"></a>

1. 登入 AWS 管理主控台 並開啟位於 https：//[https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/) 的 IAM 主控台。

1. 在 IAM 主控台的導覽窗格中，選取**使用者**，然後選取您先前建立使用者的 **`User name`**。

1. 在使用者頁面上，選取**安全憑證**頁面。接著，在**存取金鑰**下，選取**建立存取金鑰**。

1.  針對**建立存取金鑰步驟 1**，請選擇**命令列界面 (CLI)**。

1. 在**建立存取金鑰步驟 2** 中，輸入選用標籤並選取**下一步**。

1. 在**建立存取金鑰步驟 3** 中，選取**下載 .csv 檔案**，以儲存包含 IAM 使用者存取金鑰和私密存取金鑰的 `.csv` 檔案。您之後將會用到此資訊。

1. 選取完成。

## 步驟 3：設定 AWS CLI
<a name="cli-authentication-user-configure.title"></a>

針對一般用途， AWS CLI 需要下列資訊：
+ 存取金鑰 ID
+ 私密存取金鑰
+ AWS 區域
+ 輸出格式

會將此資訊 AWS CLI 儲存在 `credentials` 檔案中名為 的*設定檔* （設定集合） `default`中。根據預設，當您執行未明確指定要使用之設定檔的 AWS CLI 命令時，會使用此設定檔中的資訊。如需有關 `credentials` 檔案的詳細資訊，請參閱[中的組態和登入資料檔案設定 AWS CLI](cli-configure-files.md)。

若要設定 AWS CLI，請使用下列其中一個程序：

**Topics**
+ [使用 `aws configure`](#cli-authentication-user-configure-wizard)
+ [透過 .CSV 檔案匯入存取金鑰](#cli-authentication-user-configure-csv)
+ [直接編輯 `config` 和 `credentials` 檔案](#cli-authentication-user-configure-csv.titlecli-authentication-user-configure-file)

### 使用 `aws configure`
<a name="cli-authentication-user-configure-wizard"></a>

對於一般用途， `aws configure`命令是設定 AWS CLI 安裝的最快方式。此設定精靈會針對您要使用的每項資訊給予提示。除非使用 `--profile`選項另有指定，否則 會將此資訊 AWS CLI 存放在 `default` 設定檔中。

下列範例使用範例值來設定 `default` 設定檔。將範本值取代為您自己的值，如下列章節所述。

```
$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: json
```

下列範例使用範例值來設定名為 `userprod` 的設定檔。將範本值取代為您自己的值，如下列章節所述。

```
$ aws configure --profile userprod
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: json
```

### 透過 .CSV 檔案匯入存取金鑰
<a name="cli-authentication-user-configure-csv"></a>

您可以匯入建立存取金鑰後下載的純文字 `.csv` 檔案，而非使用 `aws configure` 來輸入存取金鑰。

`.csv` 檔案必須包含以下標頭。
+ User Name (使用者名稱) - 此欄必須加入您的 `.csv`。這是用來在匯入時建立 `config` 與 `credentials` 檔案中使用的設定檔名稱。
+ 存取金鑰 ID
+ 私密存取金鑰

**注意**  
在初始存取金鑰建立過程中，一旦關閉**下載 .csv 檔案**對話方塊中，您將無法在關閉對話方塊之後存取私密存取金鑰。如果您需要 `.csv` 檔案，您必須自己建立一個包含所需標頭和您所儲存存取金鑰資訊的檔案。如果您無法存取您的存取金鑰資訊，則必須建立新的存取金鑰。

若要匯入 `.csv` 檔案，請使用 `aws configure import` 命令與 `--csv` 選項，如以下所示：

```
$ aws configure import --csv file://credentials.csv
```

如需詳細資訊，請參閱 `aws\$1configure\$1import`。

### 直接編輯 `config` 和 `credentials` 檔案
<a name="cli-authentication-user-configure-csv.titlecli-authentication-user-configure-file"></a>

若要直接編輯 `config` 和 `credentials` 檔案，請執行下列動作。

1. 建立或開啟共用的 AWS `credentials` 檔案。這個檔案是位於 Linux 和 macOS 系統上的 `~/.aws/credentials` 和 Windows 上的 `%USERPROFILE%\.aws\credentials`。如需詳細資訊，請參閱 [中的組態和登入資料檔案設定 AWS CLI](cli-configure-files.md)。

1. 將以下文字新增至共用的 `credentials` 檔案。取代您先前下載 `.csv` 檔案中的範例值，然後儲存檔案。

   ```
   [default] 
   aws_access_key_id = AKIAIOSFODNN7EXAMPLE 
   aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
   ```

## （選用） 搭配 IAM 使用者憑證使用多重驗證
<a name="cli-authentication-user-configure-csv.titlecli-authentication-user-mfa"></a>

為了提高安全性，您可以在嘗試撥打電話時，使用從多重驗證 (MFA) 裝置、U2F 裝置或行動應用程式產生的一次性金鑰。

使用已啟用 MFA 的 IAM 使用者，執行 [https://docs.aws.amazon.com/cli/latest/reference/configure/mfa-login.html](https://docs.aws.amazon.com/cli/latest/reference/configure/mfa-login.html)命令來設定新設定檔，以搭配指定設定檔的多重要素驗證 (MFA) 使用。如果未指定設定檔，則 MFA 是以`default`設定檔為基礎。如果未設定預設設定檔，`mfa-login`命令會在要求 MFA 資訊之前提示您輸入 AWS 登入資料。下列命令範例使用您的預設組態並建立 MFA 設定檔。

```
$ aws configure mfa-login
MFA serial number or ARN: arn:aws:iam::123456789012:mfa/MFADeviceName
MFA token code: 123456
Profile to update [session-MFADeviceName]:
Temporary credentials written to profile 'session-MFADeviceName'
Credentials will expire at 2023-05-19 18:06:10 UTC
To use these credentials, specify --profile session-MFADeviceName when running AWS CLI commands
```

若要更新現有的設定檔，請使用 `--update-profile` 參數。

```
$ aws configure mfa-login --profile myprofile --update-profile mfaprofile
MFA token code: 123456
Temporary credentials written to profile 'mfaprofile'
Credentials will expire at 2023-05-19 18:06:10 UTC
To use these credentials, specify --profile mfaprofile when running AWS CLI commands
```

此命令目前僅支援硬體或軟體型一次性密碼 (OTP) 驗證器。此命令目前不支援通行金鑰和 U2F 裝置。

若要使用您的 MFA 設定檔，請搭配 命令使用 `--profile`選項。

```
$ aws s3 ls --profile mfaprofile
```

如需搭配 IAM 使用 MFA 的詳細資訊，包括如何將 MFA 指派給 IAM 使用者，請參閱*AWS Identity and Access Management 《 使用者指南*》中的 [AWS IAM 中的多重要素驗證](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa.html)。

# 在 中使用 Amazon EC2 執行個體中繼資料做為登入資料 AWS CLI
<a name="cli-configure-metadata"></a>

當您 AWS CLI 從 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體內執行 時，您可以簡化將登入資料提供給命令的程序。每個 Amazon EC2 執行個體都包含中繼資料，可供 AWS CLI 直接查詢臨時憑證。當 IAM 角色連接至執行個體時， AWS CLI 會自動且安全地從執行個體中繼資料擷取登入資料。

若要停用此服務，請使用 [AWS\$1EC2\$1METADATA\$1DISABLED](cli-configure-envvars.md#envvars-list-AWS_EC2_METADATA_DISABLED) 環境變數。

**Topics**
+ [先決條件](#cli-configure-metadata-prereqs)
+ [設定用於 Amazon EC2 中繼資料的設定檔](#cli-configure-metadata-configure)

## 先決條件
<a name="cli-configure-metadata-prereqs"></a>

若要搭配 使用 Amazon EC2 登入資料 AWS CLI，您需要完成下列操作：
+ 安裝及設定 AWS CLI。如需詳細資訊，請參閱[安裝或更新至最新版本的 AWS CLI](getting-started-install.md)及[的身分驗證和存取憑證 AWS CLI](cli-chap-authentication.md)。
+ 您瞭解組態檔案和命名設定檔。如需詳細資訊，請參閱[中的組態和登入資料檔案設定 AWS CLI](cli-configure-files.md)。
+ 您已建立可存取所需資源的 AWS Identity and Access Management (IAM) 角色，並在啟動該角色時將該角色連接到 Amazon EC2 執行個體。如需詳細資訊，請參閱《[Amazon EC2 使用者指南》中的 Amazon EC2 的 IAM 政策](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-policies-for-amazon-ec2.html)，以及《IAM 使用者指南》中的[授予在 Amazon EC2 執行個體上執行的應用程式存取 AWS 資源](https://docs.aws.amazon.com/IAM/latest/UserGuide/role-usecase-ec2app.html)的權限。 *Amazon EC2 * **

## 設定用於 Amazon EC2 中繼資料的設定檔
<a name="cli-configure-metadata-configure"></a>

若要指定您想要使用託管 Amazon EC2 執行個體設定檔中可用的憑證，請在組態檔案中的具名設定檔使用下列語法。如需詳細說明，請參閱下列步驟。

```
[profile profilename]
role_arn = arn:aws:iam::123456789012:role/rolename
credential_source = Ec2InstanceMetadata
region = region
```

1. 在組態檔案中建立設定檔。

   ```
   [profile profilename]
   ```

1. 新增可存取所需資源的 IAM ARN 角色。

   ```
   role_arn = arn:aws:iam::123456789012:role/rolename
   ```

1. 指定 `Ec2InstanceMetadata` 作為憑證來源。

   ```
   credential_source = Ec2InstanceMetadata
   ```

1. 設定您的區域。

   ```
   region = region
   ```

**範例**

下列範例會假設 *`marketingadminrole`* 角色，並在名為 `marketingadmin` 的 Amazon EC2 執行個體描述檔中使用 `us-west-2` 區域。

```
[profile marketingadmin]
role_arn = arn:aws:iam::123456789012:role/marketingadminrole
credential_source = Ec2InstanceMetadata
region = us-west-2
```

# 在 中使用外部程序來採購登入資料 AWS CLI
<a name="cli-configure-sourcing-external"></a>

**警告**  
本主題討論從外部程序取得憑證。如果用於產生憑證的命令可由未經核准的程序或使用者所存取，這可能會有安全風險。我們建議您使用 AWS CLI 和 提供的支援、安全替代方案 AWS ，以降低入侵憑證的風險。務必保護 `config` 檔案及任何支援檔案和工具，以防止洩露。  
請確定您的自訂登入資料工具不會將任何秘密資訊寫入 ，`StdErr`因為 SDKs和 AWS CLI 可以擷取並記錄這類資訊，並可能將其公開給未經授權的使用者。

如果您有方法可以產生或查詢 未直接支援的登入資料 AWS CLI，您可以透過在 `config` 檔案中設定 `credential_process` 設定來設定 AWS CLI 來使用登入資料。

例如，您在 `config` 檔案中可能包含類似以下的項目。

```
[profile developer]
credential_process = /opt/bin/awscreds-custom --username helen
```

**語法**  
若要以與任何作業系統相容的方式建立此字串，請遵循這些規則：
+ 如果路徑或檔案名稱包含空格，完整的路徑和檔案名稱請以雙引號 (「」) 括住。路徑和檔案名稱只能包含字元：A-Z a-z 0-9 – \$1. 空格
+ 如果參數名稱或參數值包含空格，則該元素請以雙引號 (「」) 括住。僅括住名稱或值，而非對組。
+ 請勿在字串中包含任何環境變數。例如，您無法包含 `$HOME` 或 `%USERPROFILE%`。
+ 請勿將主資料夾指定為 `~`。您必須指定完整路徑。

**適用於 Windows 的範例**

```
credential_process = "C:\Path\To\credentials.cmd" parameterWithoutSpaces "parameter with spaces"
```

**適用於 Linux 或 macOS 的範例**

```
credential_process = "/Users/Dave/path/to/credentials.sh" parameterWithoutSpaces "parameter with spaces"
```

**登入資料程式的預期輸出**

會執行設定檔中指定的 AWS CLI 命令，然後從 讀取資料`STDOUT`。您指定的命令必須在 `STDOUT` 上產生符合以下語法的 JSON 輸出。

```
{
  "Version": 1,
  "AccessKeyId": "an AWS access key",
  "SecretAccessKey": "your AWS secret access key",
  "SessionToken": "the AWS session token for temporary credentials", 
  "Expiration": "ISO8601 timestamp when the credentials expire"
}
```

**注意**  
截至本文編寫時，`Version` 索引鍵必須設定為 `1`。這可能隨著結構演進而逐漸遞增。

`Expiration` 索引鍵是 [ISO8601](https://en.wikipedia.org/wiki/ISO_8601) 格式的時間戳記。如果 `Expiration` 索引鍵未出現在工具的輸出中，CLI 會假設憑證是不重新整理的長期憑證。否則，憑證就視為臨時憑證，在過期之前可透過重新執行 `credential_process` 命令來自動重新整理。

**注意**  
 AWS CLI ***不會***像擔任角色登入資料一樣快取外部程序登入資料。如果需要快取，您必須在外部程序中實作它。

外部程序可能傳回非零傳回碼，以表示擷取憑證時發生錯誤。