

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

# 管理 CodeDeploy 代理程式操作
<a name="codedeploy-agent-operations"></a>

本節中的指示說明如何安裝、解除安裝、重新安裝或更新 CodeDeploy 代理程式，以及如何驗證 CodeDeploy 代理程式是否正在執行。

**Topics**
+ [驗證 CodeDeploy 代理程式是否正在執行](codedeploy-agent-operations-verify.md)
+ [判斷 CodeDeploy 代理程式的版本](codedeploy-agent-operations-version.md)
+ [安裝 CodeDeploy 代理程式](codedeploy-agent-operations-install.md)
+ [更新 CodeDeploy 代理程式](codedeploy-agent-operations-update.md)
+ [解除安裝 CodeDeploy 代理程式](codedeploy-agent-operations-uninstall.md)
+ [將 CodeDeploy 代理程式日誌傳送至 CloudWatch](codedeploy-agent-operations-cloudwatch-agent.md)

# 驗證 CodeDeploy 代理程式是否正在執行
<a name="codedeploy-agent-operations-verify"></a>

本節說明如果您懷疑 CodeDeploy 代理程式已停止在執行個體上執行，要執行的命令。

**Topics**
+ [驗證 Amazon Linux 或 RHEL 的 CodeDeploy 代理程式是否正在執行](#codedeploy-agent-operations-verify-linux)
+ [驗證 Ubuntu Server 的 CodeDeploy 代理程式是否正在執行](#codedeploy-agent-operations-verify-ubuntu)
+ [驗證適用於 Windows Server 的 CodeDeploy 代理程式是否正在執行](#codedeploy-agent-operations-verify-windows)

## 驗證 Amazon Linux 或 RHEL 的 CodeDeploy 代理程式是否正在執行
<a name="codedeploy-agent-operations-verify-linux"></a>

若要查看 CodeDeploy 代理程式是否已安裝並執行，請登入執行個體，然後執行下列命令：

```
systemctl status codedeploy-agent
```

如果命令傳回錯誤，則不會安裝 CodeDeploy 代理程式。按照 [安裝適用於 Amazon Linux 或 RHEL 的 CodeDeploy 代理程式](codedeploy-agent-operations-install-linux.md) 中的說明加以安裝。

如果已安裝並執行 CodeDeploy 代理程式，您應該會看到類似 的訊息`The AWS CodeDeploy agent is running`。

如果您看到類似 `error: No AWS CodeDeploy agent running` 的訊息，請啟動服務並執行以下兩個命令，一次一個：

```
systemctl start codedeploy-agent
```

```
systemctl status codedeploy-agent
```

## 驗證 Ubuntu Server 的 CodeDeploy 代理程式是否正在執行
<a name="codedeploy-agent-operations-verify-ubuntu"></a>

若要查看 CodeDeploy 代理程式是否已安裝並執行，請登入執行個體，然後執行下列命令：

```
systemctl status codedeploy-agent
```

如果命令傳回錯誤，則不會安裝 CodeDeploy 代理程式。按照 [安裝適用於 Ubuntu Server 的 CodeDeploy 代理程式](codedeploy-agent-operations-install-ubuntu.md) 中的說明加以安裝。

如果已安裝並執行 CodeDeploy 代理程式，您應該會看到類似 的訊息`The AWS CodeDeploy agent is running`。

如果您看到類似 `error: No AWS CodeDeploy agent running` 的訊息，請啟動服務並執行以下兩個命令，一次一個：

```
systemctl start codedeploy-agent
```

```
systemctl status codedeploy-agent
```

## 驗證適用於 Windows Server 的 CodeDeploy 代理程式是否正在執行
<a name="codedeploy-agent-operations-verify-windows"></a>

若要查看 CodeDeploy 代理程式是否已安裝並執行，請登入執行個體，然後執行下列命令：

```
powershell.exe -Command Get-Service -Name codedeployagent
```

您應該會看到類似下列的輸出：

```
Status   Name               DisplayName
------   ----               -----------
Running codedeployagent    CodeDeploy Host Agent Service
```

如果命令傳回錯誤，則不會安裝 CodeDeploy 代理程式。按照 [安裝適用於 Windows Server 的 CodeDeploy 代理程式](codedeploy-agent-operations-install-windows.md) 中的說明加以安裝。

如果 `Status` 顯示 `Running` 以外的服務，請使用下列命令啟動服務：

```
powershell.exe -Command Start-Service -Name codedeployagent
```

您可以按照下列命令重新啟動服務：

```
powershell.exe -Command Restart-Service -Name codedeployagent
```

您可以按照下列命令停止服務：

```
powershell.exe -Command Stop-Service -Name codedeployagent
```

# 判斷 CodeDeploy 代理程式的版本
<a name="codedeploy-agent-operations-version"></a>

您可以透過兩種方式來判斷執行個體上執行的 CodeDeploy 代理程式版本。

首先，從 CodeDeploy 代理程式的 1.0.1.854 版開始，您可以在執行個體的 `.version` 檔案中檢視版本編號。下表顯示每個支援的作業系統的位置和範例版本字串。


| 作業系統 | 檔案位置 | 範例 agent\$1version 字串 | 
| --- | --- | --- | 
| Amazon Linux 和 Red Hat Enterprise Linux (RHEL) | /opt/codedeploy-agent/.version | OFFICIAL\$11.0.1.854\$1rpm | 
| Ubuntu Server | /opt/codedeploy-agent/.version | OFFICIAL\$11.0.1.854\$1deb | 
| Windows Server | C:\$1ProgramData\$1Amazon\$1CodeDeploy\$1.version | OFFICIAL\$11.0.1.854\$1msi | 

其次，您可以在執行個體上執行命令，以判斷 CodeDeploy 代理程式的版本。

**Topics**
+ [判斷 Amazon Linux 或 RHEL 上的版本](#codedeploy-agent-operations-version-linux)
+ [判斷 Ubuntu Server 上的版本](#codedeploy-agent-operations-version-ubuntu)
+ [判斷 Windows Server 上的版本](#codedeploy-agent-operations-version-windows)

## 判斷 Amazon Linux 或 RHEL 上的版本
<a name="codedeploy-agent-operations-version-linux"></a>

登入執行個體並執行下列命令：

```
sudo yum info codedeploy-agent
```

## 判斷 Ubuntu Server 上的版本
<a name="codedeploy-agent-operations-version-ubuntu"></a>

登入執行個體並執行下列命令：

```
sudo dpkg -s codedeploy-agent
```

## 判斷 Windows Server 上的版本
<a name="codedeploy-agent-operations-version-windows"></a>

登入執行個體並執行下列命令：

```
sc qdescription codedeployagent
```

# 安裝 CodeDeploy 代理程式
<a name="codedeploy-agent-operations-install"></a>

若要在 EC2 執行個體或內部部署伺服器上使用 CodeDeploy，必須先安裝 CodeDeploy 代理程式。建議您使用 安裝和更新 CodeDeploy 代理程式 AWS Systems Manager。如需 Systems Manager 的詳細資訊，請參閱[什麼是 AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html) 。您可以在建立部署群組時，使用 主控台中的 Systems Manager 設定 CodeDeploy 代理程式的安裝和排程更新。

您也可以使用命令列直接從 S3 儲存貯體安裝 CodeDeploy 代理程式。

如需安裝的建議版本，請參閱 [CodeDeploy 代理程式的版本歷史記錄](codedeploy-agent.md#codedeploy-agent-version-history)。

**Topics**
+ [使用 安裝 CodeDeploy 代理程式 AWS Systems Manager](codedeploy-agent-operations-install-ssm.md)
+ [使用命令列安裝 CodeDeploy 代理程式](codedeploy-agent-operations-install-cli.md)

# 使用 安裝 CodeDeploy 代理程式 AWS Systems Manager
<a name="codedeploy-agent-operations-install-ssm"></a>

您可以使用 AWS 管理主控台 或 AWS CLI ，使用 將 CodeDeploy 代理程式安裝到您的 Amazon EC2 或內部部署執行個體 AWS Systems Manager。您可以選擇安裝特定版本，或選擇永遠安裝最新版本的代理程式。如需 的詳細資訊 AWS Systems Manager，請參閱[什麼是 AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html) 。

 使用 AWS Systems Manager 是安裝和更新 CodeDeploy 代理程式的建議方法。您也可以從 Amazon S3 儲存貯體安裝 CodeDeploy 代理程式。如需有關使用 Simple Storage Service (Amazon S3) 下載連結的資訊，請參閱 [使用命令列安裝 CodeDeploy 代理程式](codedeploy-agent-operations-install-cli.md)。

**Topics**
+ [先決條件](#install-codedeploy-agent-prereqs)
+ [安裝 CodeDeploy 代理程式](#download-codedeploy-agent-on-EC2-Instance)

## 先決條件
<a name="install-codedeploy-agent-prereqs"></a>

依照中的步驟[CodeDeploy 入門](getting-started-codedeploy.md)設定 IAM 許可和 AWS CLI。

如果使用 Systems Manager 在內部部署伺服器上安裝 CodeDeploy 代理程式，您必須向 Amazon EC2 Systems Manager 註冊您的內部部署伺服器。如需詳細資訊，請參閱*AWS Systems Manager 《 使用者指南*》中的[在混合環境中設定 Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-managedinstances.html)。

## 安裝 CodeDeploy 代理程式
<a name="download-codedeploy-agent-on-EC2-Instance"></a>

在您可以使用 Systems Manager 安裝 CodeDeploy 代理程式之前，您必須確定執行個體已針對 Systems Manager 正確設定。

### 安裝或更新 SSM 代理程式
<a name="update-SSM-Agent-EC2instance"></a>

在 Amazon EC2 執行個體上，CodeDeploy 代理程式要求執行個體執行 2.3.274.0 版或更新版本。在安裝 CodeDeploy 代理程式之前，如果您尚未更新，請在執行個體上安裝 SSM 代理程式。

SSM 代理程式預先安裝在 提供的一些 Amazon EC2 AMIs上 AWS。如需詳細資訊，請參閱[預先安裝 SSM 代理AMIs)](https://docs.aws.amazon.com/systems-manager/latest/userguide/ami-preinstalled-agent.html)。

**注意**  
請確定 CodeDeploy 代理程式也支援執行個體的作業系統。如需詳細資訊，請參閱[CodeDeploy 代理程式支援的作業系統](codedeploy-agent.md#codedeploy-agent-supported-operating-systems)。

如需有關在執行 Linux 的執行個體上安裝或更新 SSM 代理程式的資訊，請參閱*AWS Systems Manager 《 使用者指南*》中的[在 Linux 執行個體上安裝和設定 SSM 代理程式](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-install-ssm-agent.html)。

如需有關在執行 Windows Server 的執行個體上安裝或更新 SSM 代理程式的資訊，請參閱*AWS Systems Manager 《 使用者指南*》中的[在 Windows 執行個體上安裝和設定 SSM 代理程式](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-install-ssm-win.html)。

### (選用) 驗證 Systems Manager 先決條件
<a name="install-codedeploy-agent-minimum-requirements"></a>

在您使用 Systems Manager Run Command 安裝 CodeDeploy 代理程式之前，請確認您的執行個體符合 Systems Manager 的最低需求。如需詳細資訊，請參閱《AWS Systems Manager 使用者指南》**中的[設定 AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-setting-up.html)。

### 安裝 CodeDeploy 代理程式
<a name="install-codedeploy-agent-EC2"></a>

使用 SSM，您可以安裝 CodeDeploy 一次，或設定排程來安裝新版本。

 若要安裝 CodeDeploy 代理程式，請在遵循使用 經銷商安裝或更新`AWSCodeDeployAgent`套件中的步驟時選擇套件。 [AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/distributor-working-with-packages-deploy.html)

# 使用命令列安裝 CodeDeploy 代理程式
<a name="codedeploy-agent-operations-install-cli"></a>

**注意**  
建議您使用 安裝 CodeDeploy 代理程式，以便 AWS Systems Manager 能夠設定代理程式的排程更新。如需詳細資訊，請參閱[使用 安裝 CodeDeploy 代理程式 AWS Systems Manager](codedeploy-agent-operations-install-ssm.md)。

使用以下主題，使用命令列安裝和執行 CodeDeploy 代理程式。

**Topics**
+ [安裝適用於 Amazon Linux 或 RHEL 的 CodeDeploy 代理程式](codedeploy-agent-operations-install-linux.md)
+ [安裝適用於 Ubuntu Server 的 CodeDeploy 代理程式](codedeploy-agent-operations-install-ubuntu.md)
+ [安裝適用於 Windows Server 的 CodeDeploy 代理程式](codedeploy-agent-operations-install-windows.md)

# 安裝適用於 Amazon Linux 或 RHEL 的 CodeDeploy 代理程式
<a name="codedeploy-agent-operations-install-linux"></a>

登入執行個體，並執行下列命令，一次一個：使用 `yum`來安裝套件時，`sudo yum update`首先執行命令會被視為最佳實務，但如果您不想更新所有套件，則可以略過該命令。

```
sudo yum update
```

```
sudo yum install ruby
```

```
sudo yum install wget
```

（選用） 若要清除任何先前代理程式快取資訊的 AMI，請執行下列指令碼：

```
#!/bin/bash
CODEDEPLOY_BIN="/opt/codedeploy-agent/bin/codedeploy-agent"
$CODEDEPLOY_BIN stop
yum erase codedeploy-agent -y
```

變更為您的主目錄：

```
cd /home/ec2-user
```

**注意**  
在先前的命令中， `/home/ec2-user`代表 Amazon Linux 或 RHEL Amazon EC2 執行個體的預設使用者名稱。如果您的執行個體是使用自訂的 AMI 建立的，AMI 擁有者可能已指定不同的預設使用者名稱。

下載 CodeDeploy 代理程式安裝程式：

```
wget https://bucket-name.s3.region-identifier.amazonaws.com/latest/install
```

*bucket-name* 是 Amazon S3 儲存貯體的名稱，其中包含您區域的 CodeDeploy 資源套件檔案，而 *region-identifier* 是您區域的識別符。

例如：

`https://aws-codedeploy-us-east-2.s3.us-east-2.amazonaws.com/latest/install`

如需儲存貯體名稱和區域識別符的清單，請參閱 [依區域顯示的資源套件儲存貯體名稱](resource-kit.md#resource-kit-bucket-names)。

設定 `install` 檔案的執行許可：

```
chmod +x ./install
```

若要安裝最新版本的 CodeDeploy 代理程式：
+ 

  ```
  sudo ./install auto
  ```

若要安裝 CodeDeploy 代理程式的特定版本：
+ 列出您區域中可用的版本：

  ```
  aws s3 ls s3://aws-codedeploy-region-identifier/releases/ --region region-identifier | grep '\.rpm$'
  ```
+ 安裝其中一個版本：

  ```
  sudo ./install auto -v releases/codedeploy-agent-version.noarch.rpm
  ```
**注意**  
AWS 支援 CodeDeploy 代理程式的最新次要版本。目前最新的次要版本為 1.7.x。

若要確認服務是否正在執行，請執行下列命令：

```
systemctl status codedeploy-agent
```

如果已安裝並執行 CodeDeploy 代理程式，您應該會看到類似 的訊息`The AWS CodeDeploy agent is running`。

如果您看到類似 `error: No AWS CodeDeploy agent running` 的訊息，請啟動服務並執行以下兩個命令，一次一個：

```
systemctl start codedeploy-agent
```

```
systemctl status codedeploy-agent
```

# 安裝適用於 Ubuntu Server 的 CodeDeploy 代理程式
<a name="codedeploy-agent-operations-install-ubuntu"></a>

**注意**  
建議您使用 安裝 CodeDeploy 代理程式，以便 AWS Systems Manager 能夠設定代理程式的排程更新。如需詳細資訊，請參閱[使用 安裝 CodeDeploy 代理程式 AWS Systems Manager](codedeploy-agent-operations-install-ssm.md)。

**在 Ubuntu Server 上安裝 CodeDeploy 代理程式**

1. 登入執行個體。

1. 依序輸入下列命令：

   ```
   sudo apt update
   ```

   ```
   sudo apt install ruby-full
   ```

   ```
   sudo apt install wget
   ```

1. 輸入以下命令：

   ```
   cd /home/ubuntu
   ```

   */home/ubuntu* 代表 Ubuntu Server 執行個體的預設使用者名稱。如果您的執行個體是使用自訂的 AMI 建立的，AMI 擁有者可能已指定不同的預設使用者名稱。

1. 輸入以下命令：

   ```
   wget https://bucket-name.s3.region-identifier.amazonaws.com/latest/install
   ```

   *bucket-name* 是 Amazon S3 儲存貯體的名稱，其中包含您區域的 CodeDeploy 資源套件檔案，而 *region-identifier* 是您區域的識別符。

   例如：

   `https://aws-codedeploy-us-east-2.s3.us-east-2.amazonaws.com/latest/install`

   如需儲存貯體名稱和區域識別符的清單，請參閱 [依區域顯示的資源套件儲存貯體名稱](resource-kit.md#resource-kit-bucket-names)。

1. 輸入以下命令：

   ```
   chmod +x ./install
   ```

1. 執行以下任意一項：
   + 若要在任何支援的 Ubuntu Server 版本上安裝最新版本的 CodeDeploy 代理程式，*但* 20.04 除外：

     ```
     sudo ./install auto
     ```
   + 若要在 Ubuntu Server 20.04 上安裝最新版本的 CodeDeploy 代理程式：
**注意**  
將輸出寫入臨時日誌檔案是解決 Ubuntu Server 20.04 上`install`指令碼的已知錯誤時應該使用的解決方法。

     ```
     sudo ./install auto > /tmp/logfile
     ```
   + 若要在任何支援的 Ubuntu Server 版本上安裝 CodeDeploy 代理程式的特定版本，*但* 20.04 除外：
     + 列出您區域中可用的版本：

       ```
       aws s3 ls s3://aws-codedeploy-region-identifier/releases/ --region region-identifier | grep '\.deb$'
       ```
     + 安裝其中一個版本：

       ```
       sudo ./install auto -v releases/codedeploy-agent-###.deb
       ```
**注意**  
AWS 支援 CodeDeploy 代理程式的最新次要版本。目前最新的次要版本為 1.7.x。
   + 若要在 Ubuntu Server 20.04 上安裝特定版本的 CodeDeploy 代理程式：
     + 列出您區域中可用的版本：

       ```
       aws s3 ls s3://aws-codedeploy-region-identifier/releases/ --region region-identifier | grep '\.deb$'
       ```
     + 安裝其中一個版本：

       ```
       sudo ./install auto -v releases/codedeploy-agent-###.deb > /tmp/logfile
       ```
**注意**  
將輸出寫入臨時日誌檔案是解決 Ubuntu Server 20.04 上`install`指令碼的已知錯誤時應該使用的解決方法。
**注意**  
AWS 支援 CodeDeploy 代理程式的最新次要版本。目前最新的次要版本為 1.7.x。

**檢查服務是否正在執行**

1. 輸入以下命令：

   ```
   systemctl status codedeploy-agent
   ```

   如果已安裝並執行 CodeDeploy 代理程式，您應該會看到類似 的訊息`The AWS CodeDeploy agent is running`。

1. 如果您看到類似 `error: No AWS CodeDeploy agent running` 的訊息，請啟動服務並執行以下兩個命令，一次一個：

   ```
   systemctl start codedeploy-agent
   ```

   ```
   systemctl status codedeploy-agent
   ```

# 安裝適用於 Windows Server 的 CodeDeploy 代理程式
<a name="codedeploy-agent-operations-install-windows"></a>

在 Windows Server 執行個體上，您可以使用下列其中一種方法來下載和安裝 CodeDeploy 代理程式：
+ Use AWS Systems Manager （建議）
+ 執行一系列的 Windows PowerShell 命令。
+ 選擇直接下載連結。
+ 執行 Amazon S3 複製命令。

**注意**  
CodeDeploy 代理程式安裝到的資料夾是 `C:\Program Data\Amazon\CodeDeploy`。請確定此路徑上沒有目錄連接或符號連結。

**Topics**
+ [使用 Systems Manager](#codedeploy-agent-operations-install-system-manager)
+ [使用 Windows PowerShell](#codedeploy-agent-operations-install-windows-powershell)
+ [使用直接連結](#codedeploy-agent-operations-install-windows-direct-link)
+ [使用 Amazon S3 複製命令](#codedeploy-agent-operations-install-windows-s3-copy)

## 使用 Systems Manager
<a name="codedeploy-agent-operations-install-system-manager"></a>

依照 中的指示[使用 安裝 CodeDeploy 代理程式 AWS Systems Manager](codedeploy-agent-operations-install-ssm.md)安裝 CodeDeploy 代理程式。

## 使用 Windows PowerShell
<a name="codedeploy-agent-operations-install-windows-powershell"></a>

登入執行個體，並且在 Windows PowerShell 執行以下命令：

1.  要求所有從網際網路下載的指令碼和組態檔案由信任的發行者簽署。如果您被提示更改執行政策，請輸入「**Y**」。

   ```
    Set-ExecutionPolicy RemoteSigned
   ```

1.  載入 AWS Tools for Windows PowerShell。

   ```
   Import-Module AWSPowerShell
   ```

1.  建立下載 CodeDeploy 代理程式安裝檔案的目錄。

   ```
   New-Item -Path "c:\temp" -ItemType "directory" -Force
   ```

1. 使用 `Set-AWSCredential`和 `Initialize-AWSDefaultConfiguration`命令設定 AWS 登入資料。如需詳細資訊，請參閱《PowerShell 使用者指南》的工具中的[使用 AWS 登入](https://docs.aws.amazon.com/powershell/latest/userguide/specifying-your-aws-credentials.html)資料。 *AWS PowerShell *

1.  下載 CodeDeploy 代理程式安裝檔案。
**注意**  
AWS 支援 CodeDeploy 代理程式的最新次要版本。目前最新的次要版本為 1.7.x。

   若要安裝最新版本的 CodeDeploy 代理程式：
   + 

     ```
     powershell.exe -Command Read-S3Object -BucketName bucket-name -Key latest/codedeploy-agent.msi -File c:\temp\codedeploy-agent.msi
     ```

   若要安裝 CodeDeploy 代理程式的特定版本：
   + 

     ```
     powershell.exe -Command Read-S3Object -BucketName bucket-name -Key releases/codedeploy-agent-###.msi -File c:\temp\codedeploy-agent.msi
     ```

   *bucket-name* 是 Amazon S3 儲存貯體的名稱，其中包含您所在區域的 CodeDeploy 資源套件檔案。例如，對於美國東部 （俄亥俄） 區域，將 *bucket-name* 取代為 `aws-codedeploy-us-east-2`。如需儲存貯體名稱的清單，請參閱 [依區域顯示的資源套件儲存貯體名稱](resource-kit.md#resource-kit-bucket-names)。

1.  執行 CodeDeploy 代理程式安裝檔案。

   ```
   c:\temp\codedeploy-agent.msi /quiet /l c:\temp\host-agent-install-log.txt
   ```

若要確認服務是否正在執行，請執行下列命令：

```
powershell.exe -Command Get-Service -Name codedeployagent
```

 如果 CodeDeploy 代理程式剛安裝且尚未啟動，則在執行**Get-Service**命令之後，您應該會在**狀態**下看到 **Start...**：

```
Status     Name                DisplayName
------     ----                -----------
Start...   codedeployagent    CodeDeploy Host Agent Service
```

如果 CodeDeploy 代理程式已在執行中，則在執行**Get-Service**命令之後，在**狀態**下，您應該會看到 **Running**：

```
Status     Name                DisplayName
------     ----                -----------
Running    codedeployagent    CodeDeploy Host Agent Service
```

## 使用直接連結
<a name="codedeploy-agent-operations-install-windows-direct-link"></a>

如果 Windows Server 執行個體上的瀏覽器安全設定提供許可 （例如 到 `https://s3.*.amazonaws.com`)，您可以使用 區域的直接連結下載 CodeDeploy 代理程式，然後手動執行安裝程式。

連結為：

```
https://s3.region.amazonaws.com/aws-codedeploy-region/latest/codedeploy-agent.msi
```

...其中 *region* 是您 AWS 部署應用程式的 區域。

例如：

```
https://s3.af-south-1.amazonaws.com/aws-codedeploy-af-south-1/latest/codedeploy-agent.msi
```

**重要**  
從與 CodeDeploy 應用程式相同的區域取得`.msi`檔案。當您執行 `codedeploy-agent-log` 檔案時，選擇不同的區域可能會導致 `.msi` 檔案`inconsistent region`失敗。

## 使用 Amazon S3 複製命令
<a name="codedeploy-agent-operations-install-windows-s3-copy"></a>

如果執行個體上安裝 AWS CLI ，您可以使用 Amazon S3 [cp](https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html) 命令下載 CodeDeploy 代理程式，然後手動執行安裝程式。如需詳細資訊，請參閱[在 Microsoft Windows AWS Command Line Interface 上安裝](https://docs.aws.amazon.com/cli/latest/userguide/awscli-install-windows.html) 。

Amazon S3 命令為：

```
aws s3 cp s3://aws-codedeploy-region/latest/codedeploy-agent.msi codedeploy-agent.msi --region region
```

...其中 *region* 是您 AWS 部署應用程式的 區域。

例如：

```
aws s3 cp s3://aws-codedeploy-af-south-1/latest/codedeploy-agent.msi codedeploy-agent.msi --region af-south-1
```

# 更新 CodeDeploy 代理程式
<a name="codedeploy-agent-operations-update"></a>

您可以使用 在所有支援的作業系統上設定 CodeDeploy 代理程式的自動排程更新 AWS Systems Manager。您也可以藉由在執行個體上執行命令，強制在所有支援的作業系統進行更新。

**Topics**
+ [在 Amazon Linux 或 RHEL 上更新 CodeDeploy 代理程式](codedeploy-agent-operations-update-linux.md)
+ [更新 Ubuntu Server 上的 CodeDeploy 代理程式](codedeploy-agent-operations-update-ubuntu.md)
+ [在 Windows Server 上更新 CodeDeploy 代理程式](codedeploy-agent-operations-update-windows.md)

# 在 Amazon Linux 或 RHEL 上更新 CodeDeploy 代理程式
<a name="codedeploy-agent-operations-update-linux"></a>

若要使用 設定 CodeDeploy 代理程式的自動排程更新 AWS Systems Manager，請遵循[使用 安裝 CodeDeploy 代理程式 AWS Systems Manager](https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ssm.html)中的步驟。

如果您想要強制更新 CodeDeploy 代理程式，請登入執行個體，然後執行下列命令：

```
sudo /opt/codedeploy-agent/bin/install auto
```

# 更新 Ubuntu Server 上的 CodeDeploy 代理程式
<a name="codedeploy-agent-operations-update-ubuntu"></a>

若要使用 設定 CodeDeploy 代理程式的自動排程更新 AWS Systems Manager，請遵循[使用 安裝 CodeDeploy 代理程式 AWS Systems Manager](https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ssm.html)中的步驟。

如果您想要強制更新 CodeDeploy 代理程式，請登入執行個體，然後執行下列命令：

```
sudo /opt/codedeploy-agent/bin/install auto
```

# 在 Windows Server 上更新 CodeDeploy 代理程式
<a name="codedeploy-agent-operations-update-windows"></a>

您可以使用 啟用 CodeDeploy 代理程式的自動更新 AWS Systems Manager。使用 Systems Manager，您可以透過建立與 Systems Manager State Manager 的關聯來設定 Amazon EC2 或內部部署執行個體的更新排程。您也可以解除安裝目前版本並安裝較新的版本，以手動更新 CodeDeploy 代理程式。

**Topics**
+ [使用 設定自動 CodeDeploy 代理程式更新 AWS Systems Manager](#codedeploy-agent-operations-update-windows-ssm)
+ [手動更新 CodeDeploy 代理程式](#codedeploy-agent-operations-update-windows-manual)
+ [（已棄用） 使用 Windows Server Updater 更新 CodeDeploy 代理程式](#codedeploy-agent-operations-update-windows-updater)

## 使用 設定自動 CodeDeploy 代理程式更新 AWS Systems Manager
<a name="codedeploy-agent-operations-update-windows-ssm"></a>

若要設定 Systems Manager 並啟用 CodeDeploy 代理程式的自動更新，請遵循[使用 安裝 CodeDeploy 代理程式 AWS Systems Manager](https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ssm.html)中的指示。

## 手動更新 CodeDeploy 代理程式
<a name="codedeploy-agent-operations-update-windows-manual"></a>

若要手動更新 CodeDeploy 代理程式，您可以從 CLI 或使用 Systems Manager 安裝最新版本。遵循[安裝 CodeDeploy 代理程式中的指示。](https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install.html)建議您遵循解除安裝 CodeDeploy 代理程式中[的指示來解除安裝舊版 CodeDeploy 代理程式](https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-uninstall.html)。

## （已棄用） 使用 Windows Server Updater 更新 CodeDeploy 代理程式
<a name="codedeploy-agent-operations-update-windows-updater"></a>

**注意**  
適用於 Windows Server 的 CodeDeploy 代理程式更新程式已棄用，且不會更新至 1.0.1.1597 之後的任何版本。

若要啟用 CodeDeploy 代理程式的自動更新，請在新的或現有的執行個體上安裝適用於 Windows Server 的 CodeDeploy 代理程式更新程式。定期檢查的更新程式的新版本。當偵測到新版本時，更新程式會移除目前的代理程式版本 (若有安裝的話)，然後再安裝最新版本。

更新程式偵測到新版本時，如果部署已在進行，部署會繼續完成。如果部署嘗試在更新程序期間啟動，部署會失敗。

如果您想要強制更新 CodeDeploy 代理程式，請遵循 中的指示[安裝適用於 Windows Server 的 CodeDeploy 代理程式](codedeploy-agent-operations-install-windows.md)。

在 Windows Server 執行個體上，您可以透過執行 Windows PowerShell 命令、使用直接下載連結或執行 Amazon S3 複製命令來下載和安裝 CodeDeploy 代理程式更新程式。

**Topics**
+ [使用 Windows PowerShell](#codedeploy-agent-operations-update-windows-powershell)
+ [使用直接連結](#codedeploy-agent-operations-update-windows-direct-link)
+ [使用 Amazon S3 複製命令](#codedeploy-agent-operations-update-windows-s3-copy)

### 使用 Windows PowerShell
<a name="codedeploy-agent-operations-update-windows-powershell"></a>

登入執行個體，並且在 Windows PowerShell 執行以下命令，一次一個：

```
Set-ExecutionPolicy RemoteSigned
```

 如果您被提示變更執行政策，請選擇 **Y** 讓 Windows PowerShell 要求所有從網際網路下載的指令碼和組態檔，均須由信任的發佈者簽署。

```
Import-Module AWSPowerShell
```

```
New-Item -Path "c:\temp" -ItemType "directory" -Force
```

```
powershell.exe -Command Read-S3Object -BucketName bucket-name -Key latest/codedeploy-agent-updater.msi -File c:\temp\codedeploy-agent-updater.msi
```

```
c:\temp\codedeploy-agent-updater.msi /quiet /l c:\temp\host-agent-updater-log.txt
```

```
powershell.exe -Command Get-Service -Name codedeployagent
```

*bucket-name* 是 Amazon S3 儲存貯體的名稱，其中包含您所在區域的 CodeDeploy 資源套件檔案。例如，對於美國東部 （俄亥俄） 區域，將 *bucket-name* 取代為 `aws-codedeploy-us-east-2`。如需儲存貯體名稱的清單，請參閱 [依區域顯示的資源套件儲存貯體名稱](resource-kit.md#resource-kit-bucket-names)。

如果您需要對更新程序錯誤進行疑難排解，請輸入下列命令來開啟 CodeDeploy 代理程式更新程式日誌檔案：

```
notepad C:\ProgramData\Amazon\CodeDeployUpdater\log\codedeploy-agent.updater.log
```

### 使用直接連結
<a name="codedeploy-agent-operations-update-windows-direct-link"></a>

如果 Windows Server 執行個體上的瀏覽器安全設定提供必要的許可 ` http://s3.*.amazonaws.com ` （例如 )，您可以使用直接連結下載 CodeDeploy 代理程式更新程式。

連結為：

```
https://s3.region.amazonaws.com/aws-codedeploy-region/latest/codedeploy-agent-updater.msi
```

...其中 *region* 是您 AWS 更新應用程式的 區域。

例如：

```
https://s3.af-south-1.amazonaws.com/aws-codedeploy-af-south-1/latest/codedeploy-agent-updater.msi
```

### 使用 Amazon S3 複製命令
<a name="codedeploy-agent-operations-update-windows-s3-copy"></a>

如果執行個體上安裝 AWS CLI ，您可以使用 Amazon S3 [cp](https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html) 命令下載 CodeDeploy 代理程式更新程式，然後手動執行安裝程式。如需詳細資訊，請參閱[在 Microsoft Windows AWS Command Line Interface 上安裝](https://docs.aws.amazon.com/cli/latest/userguide/awscli-install-windows.html) 。

Amazon S3 命令為：

```
aws s3 cp s3://aws-codedeploy-region/latest/codedeploy-agent-updater.msi codedeploy-agent-updater.msi --region region
```

...其中 *region* 是您 AWS 更新應用程式的 區域。

例如：

```
aws s3 cp s3://aws-codedeploy-af-south-1/latest/codedeploy-agent-updater.msi codedeploy-agent-updater.msi --region af-south-1
```

# 解除安裝 CodeDeploy 代理程式
<a name="codedeploy-agent-operations-uninstall"></a>

您可以在不再需要或想要執行全新安裝時，從執行個體中移除 CodeDeploy 代理程式。

## 從 Amazon Linux 或 RHEL 解除安裝 CodeDeploy 代理程式
<a name="codedeploy-agent-operations-uninstall-linux"></a>

若要解除安裝 CodeDeploy 代理程式，請登入執行個體並執行下列命令：

```
sudo yum erase codedeploy-agent
```

## 從 Ubuntu Server 解除安裝 CodeDeploy 代理程式
<a name="codedeploy-agent-operations-uninstall-ubuntu"></a>

若要解除安裝 CodeDeploy 代理程式，請登入執行個體並執行下列命令：

```
sudo dpkg --purge codedeploy-agent
```

## 從 Windows Server 解除安裝 CodeDeploy 代理程式
<a name="codedeploy-agent-operations-uninstall-windows"></a>

若要解除安裝 CodeDeploy 代理程式，請登入執行個體並執行下列三個命令，一次一個：

```
wmic
```

```
product where name="CodeDeploy Host Agent" call uninstall /nointeractive
```

```
exit
```

您也可以登入執行個體，並在**控制台**開啟**程式和功能**、選擇 **CodeDeploy 主機代理程式**，然後選擇**解除安裝**。

# 將 CodeDeploy 代理程式日誌傳送至 CloudWatch
<a name="codedeploy-agent-operations-cloudwatch-agent"></a>

您可以使用統一的 CloudWatch 代理程式將 CodeDeploy 代理程式指標和日誌資料傳送至 CloudWatch，或更簡單地傳送 CloudWatch 代理程式。 [ CloudWatch ](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/UseCloudWatchUnifiedAgent.html)

使用以下指示安裝 CloudWatch 代理程式，並將其設定為與 CodeDeploy 代理程式搭配使用。

## 先決條件
<a name="codedeploy-agent-operations-cloudwatch-prerequisites"></a>

開始之前，請先完成以下任務：
+ 安裝 CodeDeploy 代理程式並確認其正在執行。如需詳細資訊，請參閱[安裝 CodeDeploy 代理程式](codedeploy-agent-operations-install.md)及[驗證 CodeDeploy 代理程式是否正在執行](codedeploy-agent-operations-verify.md)。
+ 安裝 CloudWatch 代理程式。如需詳細資訊，請參閱[安裝 CloudWatch 代理程式](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-on-EC2-Instance.html)。
+ 將下列許可新增至 CodeDeploy IAM 執行個體描述檔：
  + CloudWatchLogsFullAccess
  + CloudWatchAgentServerPolicy

  如需 CodeDeploy 執行個體描述檔的詳細資訊，請參閱 [步驟 4：為您的 Amazon EC2 執行個體建立 IAM 執行個體描述檔](getting-started-create-iam-instance-profile.md)的 [CodeDeploy 入門](getting-started-codedeploy.md)。

## 設定 CloudWatch 代理程式以收集 CodeDeploy 日誌
<a name="codedeploy-agent-operations-cloudwatch-configure"></a>

您可以透過逐步執行精靈或手動建立或編輯組態檔案來設定 CloudWatch 代理程式。

**使用精靈設定 CloudWatch 代理程式 (Linux)**

1. 執行精靈，如[執行 CloudWatch 代理程式組態精靈](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/create-cloudwatch-agent-configuration-file-wizard.html#cloudwatch-agent-running-wizard)中所述。

1. 在精靈中，當系統要求時`Do you want to monitor any log files?`，輸入 **1**。

1. 指定 CodeDeploy 代理程式日誌檔案，如下所示：

   1. 對於`Log file path`輸入 CodeDeploy 日誌檔案的路徑，例如：**/var/log/aws/codedeploy-agent/codedeploy-agent.log**。

   1. 對於`Log group name`輸入日誌群組名稱，例如：**codedeploy-agent-log**。

   1. 對於`Log stream name`輸入日誌串流名稱，例如：**\$1instance\$1id\$1-codedeploy-agent-log**。

1. 當系統詢問 時`Do you want to specify any additional log files?`，輸入 **1**。

1. 指定 CodeDeploy 代理程式部署日誌，如下所示：

   1. 針對 CodeDeploy 部署日誌檔案`Log file path`輸入路徑，例如：**/opt/codedeploy-agent/deployment-root/deployment-logs/codedeploy-agent-deployments.log**。

   1. 對於`Log group name`輸入日誌群組名稱，例如：**codedeploy-agent-deployment-log**。

   1. 對於`Log stream name`輸入日誌串流名稱，例如：**\$1instance\$1id\$1-codedeploy-agent-deployment-log**。

1. 當系統詢問 時`Do you want to specify any additional log files?`，輸入 **1**。

1. 指定 CodeDeploy 代理程式更新程式日誌，如下所示：

   1. 對於 CodeDeploy `Log file path` 更新程式日誌檔的路徑，例如：**/tmp/codedeploy-agent.update.log**。

   1. 對於`Log group name`輸入日誌群組名稱，例如：**codedeploy-agent-updater-log**。

   1. 對於`Log stream name`輸入日誌串流名稱，例如：**\$1instance\$1id\$1-codedeploy-agent-updater-log**。

**使用精靈設定 CloudWatch 代理程式 (Windows)**

1. 執行精靈，如[執行 CloudWatch 代理程式組態精靈](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/create-cloudwatch-agent-configuration-file-wizard.html#cloudwatch-agent-running-wizard)中所述。

1. 在精靈中，當系統要求時`Do you want to monitor any customized log files?`，輸入 **1**。

1. 指定 CodeDeploy 日誌檔案，如下所示：

   1. 對於`Log file path`輸入路徑 r CodeDeploy 代理程式日誌檔案，例如：**C:\$1ProgramData\$1Amazon\$1CodeDeploy\$1log\$1codedeploy-agent-log.txt**。

   1. 對於`Log group name`輸入日誌群組名稱，例如：**codedeploy-agent-log**。

   1. 對於`Log stream name`輸入日誌串流名稱，例如：**\$1instance\$1id\$1-codedeploy-agent-log**。

1. 當系統詢問 時`Do you want to specify any additional log files?`，輸入 **1**。

1. 指定 CodeDeploy 代理程式部署日誌，如下所示：

   1. 對於`Log file path`輸入 CodeDeploy 部署日誌檔案的路徑，例如：**C:\$1ProgramData\$1Amazon\$1CodeDeploy\$1deployment-logs\$1codedeploy-agent-deployments.log**。

   1. 對於`Log group name`輸入日誌群組名稱，例如：**codedeploy-agent-deployment-log**。

   1. 對於`Log stream name`輸入日誌串流名稱，例如：**\$1instance\$1id\$1-codedeploy-agent-deployment-log**。

**透過手動建立或編輯組態檔案 (Linux) 來設定 CloudWatch 代理程式**

1. 建立或編輯 CloudWatch 代理程式組態檔案，如[手動建立或編輯 CloudWatch 代理程式組態檔案](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html)所述。

1. 請確定檔案已呼叫`/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json`且包含下列程式碼：

   ```
   ...
   "logs": {
       "logs_collected": {
           "files": {
               "collect_list": [
                   {
                       "file_path": "/var/log/aws/codedeploy-agent/codedeploy-agent.log",
                       "log_group_name": "codedeploy-agent-log",
                       "log_stream_name": "{instance_id}-agent-log"
                   },
                   {
                       "file_path": "/opt/codedeploy-agent/deployment-root/deployment-logs/codedeploy-agent-deployments.log",
                       "log_group_name": "codedeploy-agent-deployment-log",
                       "log_stream_name": "{instance_id}-codedeploy-agent-deployment-log"
                   },
                   {
                       "file_path": "/tmp/codedeploy-agent.update.log",
                       "log_group_name": "codedeploy-agent-updater-log",
                       "log_stream_name": "{instance_id}-codedeploy-agent-updater-log"
                   }
               ]
           }
       }
   }
   ...
   ```

**手動建立或編輯組態檔案來設定 CloudWatch 代理程式 (Windows)**

1. 建立或編輯 CloudWatch 代理程式組態檔案，如[手動建立或編輯 CloudWatch 代理程式組態檔案](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html)所述。

1. 請確定檔案已呼叫`C:\ProgramData\Amazon\AmazonCloudWatchAgent\amazon-cloudwatch-agent.json`且包含下列程式碼：

   ```
   ...
   "logs": {
           "logs_collected": {
               "files": {
                   "collect_list": [
                       {
                           "file_path": "C:\\ProgramData\\Amazon\\CodeDeploy\\log\\codedeploy-agent-log.txt",
                           "log_group_name": "codedeploy-agent-log",
                           "log_stream_name": "{instance_id}-codedeploy-agent-log"
                       },
                       {
                           "file_path": "C:\\ProgramData\\Amazon\\CodeDeploy\\deployment-logs\\codedeploy-agent-deployments.log",
                           "log_group_name": "codedeploy-agent-deployment-log",
                           "log_stream_name": "{instance_id}-codedeploy-agent-deployment-log"
                       }
                   ]
               },
               ...
           }
       },
   ...
   ```

## 重新啟動 CloudWatch 代理程式
<a name="codedeploy-agent-operations-cloudwatch-restart"></a>

進行變更後，請重新啟動 CloudWatch 代理程式，如[啟動 CloudWatch 代理程式](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-on-EC2-Instance-fleet.html#start-CloudWatch-Agent-EC2-fleet)中所述。