

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

# 在 中建立和使用別名 AWS CLI
<a name="cli-usage-alias"></a>

別名是您可以在 AWS Command Line Interface (AWS CLI) 中建立的捷徑，以縮短您經常使用的命令或指令碼。您可以在位於組態資料夾的 `alias` 檔案中建立別名。

**Topics**
+ [先決條件](#cli-usage-alias-prepreqs)
+ [步驟 1：建立別名檔案](#cli-usage-alias-create-file)
+ [步驟 2：建立別名](#cli-usage-alias-create-alias)
+ [步驟 3：呼叫別名](#cli-usage-alias-call-alias)
+ [別名儲存庫範例](#cli-usage-alias-examples)
+ [Resources](#cli-usage-alias-references)

## 先決條件
<a name="cli-usage-alias-prepreqs"></a>

若要使用別名命令，您需要完成以下事項：
+ 安裝及設定 AWS CLI。如需詳細資訊，請參閱[安裝或更新至最新版本的 AWS CLI](getting-started-install.md)及[的身分驗證和存取憑證 AWS CLI](cli-chap-authentication.md)。
+ 使用最低 AWS CLI 版本 1.11.24 或 2.0.0。
+ （選用） 若要使用別名 bash AWS CLI 指令碼，您必須使用與 bash 相容的終端機。

## 步驟 1：建立別名檔案
<a name="cli-usage-alias-create-file"></a>

若要建立 `alias` 檔案，您可以使用檔案導覽和文字編輯器，或使用您偏好的終端機並按照逐步程序來建立。若要快速建立別名檔案，請使用以下命令區塊。

------
#### [ Linux and macOS ]

```
$ mkdir -p ~/.aws/cli
$ echo '[toplevel]' > ~/.aws/cli/alias
```

------
#### [ Windows ]

```
C:\> md %USERPROFILE%\.aws\cli
C:\> echo [toplevel] > %USERPROFILE%/.aws/cli/alias
```

------

**建立別名檔案**

1. 在 AWS CLI 組態資料夾中建立名為 `cli`的資料夾。預設情況下，組態資料夾是 `~/.aws/` (Linux 或 macOS) 或 `%USERPROFILE%\.aws\` (Windows)。您可以透過檔案導覽或使用以下命令來建立此資料夾。

------
#### [ Linux and macOS ]

   ```
   $ mkdir -p ~/.aws/cli
   ```

------
#### [ Windows ]

   ```
   C:\> md %USERPROFILE%\.aws\cli
   ```

------

   產生的 `cli` 資料夾預設路徑為 `~/.aws/cli/` (Linux 或 macOS) 或 `%USERPROFILE%\.aws\cli` (Windows)。

1. 在 `cli` 資料夾中，建立沒有副檔名且名為 `alias` 的文字檔案，並將 `[toplevel]` 新增至第一行。您可以透過偏好的文字編輯器或使用下列命令來建立此檔案。

------
#### [ Linux and macOS ]

   ```
   $ echo '[toplevel]' > ~/.aws/cli/alias
   ```

------
#### [ Windows ]

   ```
   C:\> echo [toplevel] > %USERPROFILE%/.aws/cli/alias
   ```

------

## 步驟 2：建立別名
<a name="cli-usage-alias-create-alias"></a>

您可以使用基本命令或 Bash 指令碼來建立別名。

### 建立基本命令別名
<a name="cli-usage-alias-create-alias-basic"></a>

您可以在上一步建立的 `alias` 檔案中，使用下列語法新增命令以建立別名。

**語法**

```
aliasname = command [--options]
```

*aliasname* 是您所命名的別名。*command* 為您要呼叫的命令，此命令可包含其他別名。您可以在別名中加入選項或參數，或在呼叫別名時新增選項或參數。

以下範例使用 [https://docs.aws.amazon.com/cli/latest/reference/sts/get-caller-identity.html](https://docs.aws.amazon.com/cli/latest/reference/sts/get-caller-identity.html) 命令來建立名為 `aws whoami` 的別名。由於此別名會呼叫現有的 AWS CLI 命令，因此您編寫的命令無需添加 `aws` 前綴。

```
whoami = sts get-caller-identity
```

下列範例採用之前的 `whoami` 範例，並新增 `Account` 篩選條件和文字 `output` 選項。

```
whoami2 = sts get-caller-identity --query Account --output text
```

### 建立子命令別名
<a name="cli-usage-alias-create-alias-sub-command"></a>

**注意**  
子命令別名功能需要 1.11.24 或 2.0.0 的最低 AWS CLI 版本

您可以在上一步建立的 `alias` 檔案中，使用下列語法新增命令以建立子命令的別名。

**語法**

```
[command commandGroup]
aliasname = command [--options]
```

*commandGroup* 是命令名稱空間，例如，命令 `aws ec2 describe-regions` 在 `ec2` 命令群組下。*aliasname* 是您所命名的別名。*command* 為您要呼叫的命令，此命令可包含其他別名。您可以在別名中加入選項或參數，或在呼叫別名時新增選項或參數。

以下範例使用 [https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-regions.html](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-regions.html) 命令來建立名為 `aws ec2 regions` 的別名。由於此別名會呼叫 `ec2` 命令命名空間下現有的 AWS CLI 命令，因此您編寫的命令無需添加 `aws ec2` 字首。

```
[command ec2]
regions = describe-regions --query Regions[].RegionName
```

若要從命令名稱空間之外的命令建立別名，請在完整命令前加上驚嘆號。以下範例使用 [https://docs.aws.amazon.com/cli/latest/reference/iam/list-instance-profiles.html](https://docs.aws.amazon.com/cli/latest/reference/iam/list-instance-profiles.html) 命令來建立名為 `aws ec2 instance-profiles` 的別名。

```
[command ec2]
instance-profiles = !aws iam list-instance-profiles
```

**注意**  
別名只使用現有的命令命名空間，您不能建立新的命令空間。例如，由於 `johnsmith` 命令名空間尚未存在，因此您無法使用 `[command johnsmith]` 區段建立別名。

### 建立 Bash 指令碼別名
<a name="cli-usage-alias-create-alias-scripting"></a>

**警告**  
若要使用別名 bash AWS CLI 指令碼，您必須使用與 bash 相容的終端機

您可以透過以下語法，在更進階的流程中使用 Bash 指令碼建立別名。

**語法**

```
aliasname = 
    !f() {
        script content
}; f
```

*aliasname* 是您所命名的別名，*script content* 則是您在呼叫別名時要執行的指令碼。

以下範例使用 `opendns` 來輸出您目前的 IP 地址。由於您可以在其他別名中使用別名，因此以下 `myip` 別名，可用於從其他別名中允許或撤銷 IP 地址的存取權限。

```
myip =
  !f() {
    dig +short myip.opendns.com @resolver1.opendns.com
  }; f
```

下列指令碼範例會呼叫上一個 `aws myip` 別名，以授予您的 IP 地址進入 Amazon EC2 安全群組的權限。

```
authorize-my-ip =
  !f() {
    ip=$(aws myip)
    aws ec2 authorize-security-group-ingress --group-id ${1} --cidr $ip/32 --protocol tcp --port 22
  }; f
```

在您呼叫使用 Bash 指令碼的別名時，變數一律會按照您輸入的順序傳遞。在 Bash 指令碼中，變數名稱不是考慮因素，僅會考量其顯示順序。在下列 `textalert` 別名範例中，`--message` 選項的變數為第一個，而 `--phone-number` 選項為第二個。

```
textalert =
  !f() {
    aws sns publish --message "${1}" --phone-number ${2}
  }; f
```

## 步驟 3：呼叫別名
<a name="cli-usage-alias-call-alias"></a>

若要執行您在 `alias` 檔案中建立的別名，請使用以下語法。您可以在呼叫別名時新增其他選項。

**語法**

```
$ aws aliasname
```

下列範例使用 `aws whoami` 命令別名。

```
$ aws whoami
{
    "UserId": "A12BCD34E5FGHI6JKLM",
    "Account": "1234567890987",
    "Arn": "arn:aws:iam::1234567890987:user/userName"
}
```

以下範例使用 `aws whoami` 別名以及其他選項，以便只將 `Account` 號碼回傳到 `text` 輸出中。

```
$ aws whoami --query Account --output text
1234567890987
```

下列範例使用 `aws ec2 regions`[ 子命令別名](#cli-usage-alias-create-alias-sub-command)。

```
$ aws ec2 regions
[
    "ap-south-1",
    "eu-north-1",
    "eu-west-3",
    "eu-west-2",
...
```

### 使用 Bash 指令碼變數呼叫別名
<a name="cli-usage-alias-call-alias-variables"></a>

在您呼叫使用 Bash 指令碼的別名時，變數會按照您輸入的順序傳遞。在 Bash 指令碼中，變數名稱不是考慮因素，僅會考量其顯示順序。例如，在下列 `textalert` 別名中，`--message` 選項的變數為第一個，而 `--phone-number` 為第二個。

```
textalert =
  !f() {
    aws sns publish --message "${1}" --phone-number ${2}
  }; f
```

在呼叫 `textalert` 別名時，您必須按照變數在別名中的執行順序來傳遞變數。在以下範例中，我們使用變數 `$message` 和 `$phone`。`$message` 變數會作為 `--message` 選項的 `${1}` 傳遞，`$phone` 變數則作為 `--phone-number` 選項的 `${2}` 傳遞。如此會成功呼叫 `textalert` 別名來傳送訊息。

```
$ aws textalert $message $phone
{
    "MessageId": "1ab2cd3e4-fg56-7h89-i01j-2klmn34567"
}
```

在下列範例中，呼叫 `$phone` 和 `$message` 別名時會對調順序。`$message` 變數會作為 `--message` 選項的 `${1}` 傳遞，`$phone` 變數則作為 `--phone-number` 選項的 `${2}` 傳遞。由於變數順序不正確，別名會錯誤傳遞變數。因為 `$message` 的內容與 `--phone-number` 選項的電話號碼格式需求不相符，所以會導致錯誤發生。

```
$ aws textalert $phone $message
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help

Unknown options: text
```

## 別名儲存庫範例
<a name="cli-usage-alias-examples"></a>

*GitHub* 上的[AWS CLI 別名儲存庫](https://github.com/awslabs/awscli-aliases)包含 AWS CLI 開發人員團隊和社群建立的 AWS CLI 別名範例。您可以使用整個 `alias` 檔案範例，或者自行採用個別別名。

**警告**  
執行本節中的命令會刪除您現有的 `alias` 檔案。為了避免覆寫現有別名檔案，請變更下載位置。

**使用儲存庫中的別名**

1. 安裝 Git。如需安裝說明，請參閱 *Git 文件*中的[新手入門 – 安裝 Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)。

1. 安裝 `jp` 命令。`jp` 命令是用於 `tostring` 別名。如需安裝說明，請參閱 *GitHub* 上的 [JMESPath (jp) README.md](https://github.com/jmespath/jp)。

1. 安裝 `jq` 命令。`jq` 命令是用於 `tostring-with-jq` 別名。如需安裝說明，請參閱 *GitHub* 上的 [JSON processor (jq)](https://stedolan.github.io/jq/download/)。

1. 執行下列其中一項操作來下載 `alias` 檔案：
   + 執行以下命令，即可從儲存庫下載 `alias` 檔案並將它複製到組態資料夾。

------
#### [ Linux and macOS ]

     ```
     $ git clone https://github.com/awslabs/awscli-aliases.git
     $ mkdir -p ~/.aws/cli
     $ cp awscli-aliases/alias ~/.aws/cli/alias
     ```

------
#### [ Windows ]

     ```
     C:\> git clone https://github.com/awslabs/awscli-aliases.git
     C:\> md %USERPROFILE%\.aws\cli
     C:\> copy awscli-aliases\alias %USERPROFILE%\.aws\cli
     ```

------
   + 直接從儲存庫下載，並儲存至 AWS CLI 組態`cli`資料夾中的 資料夾。預設情況下，組態資料夾是 `~/.aws/` (Linux 或 macOS) 或 `%USERPROFILE%\.aws\` (Windows)。

1. 若要驗證別名是否正常運作，請執行以下別名。

   ```
   $ aws whoami
   ```

   如此會顯示與 `aws sts get-caller-identity` 命令相同的回應：

   ```
   {
       "Account": "012345678901",
       "UserId": "AIUAINBADX2VEG2TC6HD6",
       "Arn": "arn:aws:iam::012345678901:user/myuser"
   }
   ```

## Resources
<a name="cli-usage-alias-references"></a>
+ *GitHub* 上的[AWS CLI 別名儲存庫](https://github.com/awslabs/awscli-aliases)包含 AWS CLI 開發人員團隊建立的 AWS CLI 別名範例，以及社群的 AWS CLI 貢獻。
+ 來自 [AWS re：Invent 2016 的別名功能公告：YouTube 上的有效 AWS CLI 使用者](https://www.youtube.com/watch?t=1590&v=Xc1dHtWa9-Q)。 *YouTube* 
+ [https://docs.aws.amazon.com/cli/latest/reference/sts/get-caller-identity.html](https://docs.aws.amazon.com/cli/latest/reference/sts/get-caller-identity.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/sns/publish.html](https://docs.aws.amazon.com/cli/latest/reference/sns/publish.html)