

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 管理 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 服务器的 CodeDeploy 代理是否正在运行](#codedeploy-agent-operations-verify-ubuntu)
+ [验证 Windows 服务器的 CodeDeploy 代理是否正在运行](#codedeploy-agent-operations-verify-windows)

## 验证 Amazon Linux 或 RHEL 的 CodeDeploy 代理是否正在运行
<a name="codedeploy-agent-operations-verify-linux"></a>

要查看 CodeDeploy 代理是否已安装并正在运行，请登录实例，然后运行以下命令：

```
systemctl status codedeploy-agent
```

如果命令返回错误，则表示未安装 CodeDeploy 代理。请按照[安装适用于亚马逊 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 服务器的 CodeDeploy 代理是否正在运行
<a name="codedeploy-agent-operations-verify-ubuntu"></a>

要查看 CodeDeploy 代理是否已安装并正在运行，请登录实例，然后运行以下命令：

```
systemctl status codedeploy-agent
```

如果命令返回错误，则表示未安装 CodeDeploy 代理。请按照[为 Ubuntu 服务器安装 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 服务器的 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 服务器的 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>

要 CodeDeploy 在 EC2 实例或本地服务器上使用，必须先安装 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 管理控制台 或将 CodeDeploy 代理安装 AWS CLI 到您的 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)。

 安装和更新 CodeDeploy 代理的推荐方法 AWS Systems Manager 是使用。您也可以从 Amazon S3 存储桶安装 CodeDeploy 代理。有关使用 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。

如果使用 System CodeDeploy s Manager 在本地服务器上安装代理，则必须向 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 代理之前，必须确保已为系统管理器正确配置实例。

### 安装或更新 SSM Agent
<a name="update-SSM-Agent-EC2instance"></a>

在 Amazon EC2 实例上， CodeDeploy 代理要求该实例运行版本 2.3.274.0 或更高版本。在安装 CodeDeploy 代理之前，请先在实例上更新或安装 SSM 代理（如果您尚未这样做）。

SSM 代理已预安装在由 AMIs 提供的某些 Amazon EC2 上。 AWS有关更多信息，请参阅[预装了 SSM 代理的 Amazon 系统映像 (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 服务器的实例上安装或更新 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 运行命令安装 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 代理，请在按照 dist [AWS Systems Manager ributor 安装或更新软件包中的步骤选择软件包](https://docs.aws.amazon.com/systems-manager/latest/userguide/distributor-working-with-packages-deploy.html)。`AWSCodeDeployAgent`

# 使用命令行安装 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**
+ [安装适用于亚马逊 Linux 或 RHEL 的 CodeDeploy 代理](codedeploy-agent-operations-install-linux.md)
+ [为 Ubuntu 服务器安装 CodeDeploy 代理](codedeploy-agent-operations-install-ubuntu.md)
+ [安装适用于 Windows 服务器的 CodeDeploy 代理](codedeploy-agent-operations-install-windows.md)

# 安装适用于亚马逊 Linux 或 RHEL 的 CodeDeploy 代理
<a name="codedeploy-agent-operations-install-linux"></a>

登录到实例，并依次运行以下命令。首先运行命令 `sudo yum update` 被认为是使用 `yum` 安装软件包的最佳做法，但如果您不想更新所有软件包，也可以跳过它。

```
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*是包含您所在地区的 CodeDeploy 资源包文件的 Amazon S3 存储桶的名称，*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 服务器安装 CodeDeploy 代理
<a name="codedeploy-agent-operations-install-ubuntu"></a>

**注意**  
我们建议使用安装 CodeDeploy 代理 AWS Systems Manager ，以便能够配置代理的预设更新。有关更多信息，请参阅 [使用安装 CodeDeploy 代理 AWS Systems Manager](codedeploy-agent-operations-install-ssm.md)。

**在 Ubuntu CodeDeploy 服务器上安装代理**

1. 登录到实例。

1. 依次输入以下命令：

   ```
   sudo apt update
   ```

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

   ```
   sudo apt install wget
   ```

1. 输入以下命令：

   ```
   cd /home/ubuntu
   ```

   */home/ubuntu*表示 Ubuntu 服务器实例的默认用户名。如果您的实例是使用某个自定义 AMI 创建的，该 AMI 所有者可能已指定不同的默认用户名。

1. 输入以下命令：

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

   *bucket-name*是包含您所在地区的 CodeDeploy 资源包文件的 Amazon S3 存储桶的名称，*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 服务器支持的任何版本（20.04 *除外*）上安装最新版本的 CodeDeploy 代理，请执行以下操作：

     ```
     sudo ./install auto
     ```
   + 要在 Ubuntu Server 20.04 上安装最新版本的 CodeDeploy 代理，请执行以下操作：
**注意**  
将输出写入临时日志文件是一种变通方法，当我们在 Ubuntu Server 20.04 上解决 `install` 脚本的一个已知错误时，应该使用这种方法。

     ```
     sudo ./install auto > /tmp/logfile
     ```
   + 要在任何支持的 Ubuntu 服务器版本（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
       ```
**注意**  
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 服务器的 CodeDeploy 代理
<a name="codedeploy-agent-operations-install-windows"></a>

在 Windows 服务器实例上，您可以使用以下方法之一来下载和安装 CodeDeploy 代理：
+ 使用 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.  要求从 Internet 下载的所有脚本和配置文件由可信发布者签名。如果系统提示您更改执行策略，请键入“**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 工具*中的[使用 AWS 凭证](https://docs.aws.amazon.com/powershell/latest/userguide/specifying-your-aws-credentials.html)。

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*是包含您所在地区的 CodeDeploy 资源包文件的 Amazon S3 存储桶的名称。例如，对于美国东部（俄亥俄州）区域，请*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
```

... 您要部署应用程序的 AWS 区域在哪里。*region*

例如：

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

**重要**  
从与您的 CodeDeploy应用程序相同的地区获取`.msi`文件。选择不同的区域可能会导致运行 `.msi` 文件时 `codedeploy-agent-log` 文件中出现 `inconsistent region` 错误。

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

如果实例上安装了，则可以使用 Amazon S3 [cp](https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html) 命令下载 CodeDeploy 代理，然后手动运行安装程序。 AWS CLI 有关信息，请参阅[AWS Command Line Interface 在微软 Windows 上安装](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
```

... 您要部署应用程序的 AWS 区域在哪里。*region*

例如：

```
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 CodeDeploy 服务器上的代理](codedeploy-agent-operations-update-ubuntu.md)
+ [在 Windows 服务器上更新 CodeDeploy 代理](codedeploy-agent-operations-update-windows.md)

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

要使用配置 CodeDeploy 代理的自动预设更新 AWS Systems Manager，请按照[安装 CodeDeploy 代理中的步骤进行](https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ssm.html)操作 AWS Systems Manager。

如果要强制更新 CodeDeploy 代理，请登录实例，然后运行以下命令：

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

# 更新 Ubuntu CodeDeploy 服务器上的代理
<a name="codedeploy-agent-operations-update-ubuntu"></a>

要使用配置 CodeDeploy 代理的自动预设更新 AWS Systems Manager，请按照[安装 CodeDeploy 代理中的步骤进行](https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ssm.html)操作 AWS Systems Manager。

如果要强制更新 CodeDeploy 代理，请登录实例，然后运行以下命令：

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

# 在 Windows 服务器上更新 CodeDeploy 代理
<a name="codedeploy-agent-operations-update-windows"></a>

您可以使用启用 CodeDeploy 代理的自动更新 AWS Systems Manager。通过使用 Systems Manager，您可以通过创建与 Systems Manager 状态管理器的关联来为您的 Amazon EC2 或本地实例配置更新计划。您也可以通过卸载当前版本并安装较新的版本来手动更新 CodeDeploy 代理。

**Topics**
+ [使用设置自动 CodeDeploy 代理更新 AWS Systems Manager](#codedeploy-agent-operations-update-windows-ssm)
+ [手动更新 CodeDeploy 代理](#codedeploy-agent-operations-update-windows-manual)
+ [（已弃用）使用 Windows 服务器更新程序更新 CodeDeploy 代理](#codedeploy-agent-operations-update-windows-updater)

## 使用设置自动 CodeDeploy 代理更新 AWS Systems Manager
<a name="codedeploy-agent-operations-update-windows-ssm"></a>

要配置 Systems Manager 并启用 CodeDeploy 代理的自动更新，请按照[使用安装 CodeDeploy 代理中的](https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ssm.html)说明进行操作 AWS Systems Manager。

## 手动更新 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 服务器更新程序更新 CodeDeploy 代理
<a name="codedeploy-agent-operations-update-windows-updater"></a>

**注意**  
Windows Server 的 CodeDeploy 代理更新程序已被弃用，并且不会更新到 1.0.1.1597 之后的任何版本。

要启用 CodeDeploy 代理的自动更新，请在新的或现有实例上安装适用于 Windows Server 的 CodeDeploy 代理更新程序。更新程序定期检查新版本。当检测到新版本时，更新程序将在安装最新版本之前，卸载当前版本的代理（如果已安装）。

如果在更新程序检测到新版本时部署操作已在进行中，则部署操作将会继续完成。如果尝试在更新过程中启动部署操作，则部署操作将失败。

如果要强制更新 CodeDeploy 代理，请按照中的说明进行操作[安装适用于 Windows 服务器的 CodeDeploy 代理](codedeploy-agent-operations-install-windows.md)。

在 Windows 服务器实例上，您可以通过运行 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 要求所有从 Internet 下载的脚本和配置文件都由受信任的发布者签名。

```
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*是包含您所在地区的 CodeDeploy 资源包文件的 Amazon S3 存储桶的名称。例如，对于美国东部（俄亥俄州）区域，请*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
```

... 您要更新应用程序的 AWS 区域在哪里。*region*

例如：

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

如果实例上安装了，则可以使用 Amazon S3 [cp](https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html) 命令下载 CodeDeploy 代理更新程序，然后手动运行安装程序。 AWS CLI 有关信息，请参阅[AWS Command Line Interface 在微软 Windows 上安装](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
```

... 您要更新应用程序的 AWS 区域在哪里。*region*

例如：

```
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 服务器上卸载 CodeDeploy 代理
<a name="codedeploy-agent-operations-uninstall-ubuntu"></a>

要卸载 CodeDeploy 代理，请登录实例并运行以下命令：

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

## 从 Windows 服务器上卸载 CodeDeploy 代理
<a name="codedeploy-agent-operations-uninstall-windows"></a>

要卸载 CodeDeploy 代理，请登录实例并运行以下三个命令，一次运行一个：

```
wmic
```

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

```
exit
```

您也可以登录实例，然后在 “**控制面板” 中打开 “**程序和功能**”**，选择 **CodeDeploy Host Agent**，然后选择 “**卸载**”。

# 将 CodeDeploy 代理日志发送到 CloudWatch
<a name="codedeploy-agent-operations-cloudwatch-agent"></a>

您可以使用[统一 CodeDeploy 代理，或者更简单地说， CloudWatch 使用 CloudWatch 代理，向代理](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/UseCloudWatchUnifiedAgent.html)发送 CloudWatch 代理指标和日志数据。

按照以下说明安装 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. 要`Log file path`输入 CodeDeploy 部署日志文件的路径，例如：**/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. `Log file path`输入 CodeDeploy 更新程序日志文件的路径，例如：。**/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`输入 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**。

**通过手动创建或编辑配置文件来配置 CloudWatch 代理 (Linux)**

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)。