

# 使用 Amazon Systems Manager 安装 CloudWatch 代理
<a name="installing-cloudwatch-agent-ssm"></a>

 使用 Amazon Systems Manager 更轻松地在 Amazon EC2 实例集上安装 CloudWatch 代理。您可以将代理下载到一台服务器上，然后为实例集中的所有服务器创建您的 CloudWatch 代理配置文件。然后，您可以使用 Systems Manager，通过创建的配置文件将代理安装到其他服务器上。可以按照以下主题使用 Amazon Systems Manager 安装和运行 CloudWatch 代理。

**Topics**
+ [安装或更新 SSM 代理](#update-SSM-Agent-EC2instance-first)
+ [验证 Systems Manager 的先决条件](#install-CloudWatch-Agent-minimum-requirements-first)
+ [验证 Internet 访问权限](#install-CloudWatch-Agent-internet-access-first)
+ [将 CloudWatch 代理程序包下载到您的第一个实例](#install-CloudWatch-Agent-EC2-first)
+ [创建和修改代理配置文件](#CW-Agent-Instance-Create-Configuration-File-first)
+ [使用代理配置在其他 EC2 实例上安装和启动 CloudWatch 代理](#install-CloudWatch-Agent-on-EC2-Instance-fleet)
+ [使用代理配置在其他 EC2 实例上安装 CloudWatch 代理](#install-CloudWatch-Agent-on-EC2-Instance-fleet)
+ [（可选）修改 CloudWatch 代理的通用配置和命名配置文件](#CloudWatch-Agent-profile-instance-fleet)

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

在 Amazon EC2 实例上，CloudWatch 代理要求实例运行 SSM 代理 2.2.93.0 或更高版本。在安装 CloudWatch 代理之前，如果还没有在实例上更新或安装 SSM Agent，请执行该操作。

有关在运行 Linux 的实例上安装或更新 SSM Agent 的信息，请参阅 *Amazon Systems Manager 用户指南*中的[在 Linux 实例上安装和配置 SSM Agent](https://docs.amazonaws.cn/systems-manager/latest/userguide/manually-install-ssm-agent-linux.html)。

有关安装或更新 SSM Agent 的更多信息，请参阅 *Amazon Systems Manager 用户指南*中的[使用 SSM Agent](https://docs.amazonaws.cn/systems-manager/latest/userguide/ssm-agent.html)。

## 验证 Systems Manager 的先决条件
<a name="install-CloudWatch-Agent-minimum-requirements-first"></a>

在使用 Systems Manager Run Command 安装和配置 CloudWatch 代理之前，请确保您的实例满足 Systems Manager 的最低要求。有关更多信息，请参阅 *Amazon Systems Manager 用户指南*中的 [Systems Manager 的先决条件](https://docs.amazonaws.cn/systems-manager/latest/userguide/systems-manager-setting-up.html#systems-manager-prereqs)。

## 验证 Internet 访问权限
<a name="install-CloudWatch-Agent-internet-access-first"></a>

您的 Amazon EC2 实例必须能够连接到 CloudWatch 端点。可以通过互联网网关、NAT 网关或 CloudWatch 接口 VPC 终端节点实现此连接。有关如何配置互联网访问权限的更多信息，请参阅 *Amazon VPC 用户指南*中的[互联网网关](https://docs.amazonaws.cn/vpc/latest/userguide/VPC_Internet_Gateway.html)。

要在您的代理上配置的终端节点和端口如下所示：
+ 如果要使用代理收集指标，则必须允许列出相应区域的 CloudWatch 端点。这些端点在 *Amazon Web Services 一般参考* 的 [Amazon CloudWatch](https://docs.amazonaws.cn/general/latest/gr/rande.html#cw_region) 中列出。
+ 如果要使用代理收集日志，则必须允许列出相应区域的 CloudWatch Logs 端点。这些端点在 *Amazon Web Services 一般参考* 的 [Amazon CloudWatch Logs](https://docs.amazonaws.cn/general/latest/gr/rande.html#cwl_region) 中列出。
+ 如果使用 Systems Manager 安装代理或使用 Parameter Store 存储配置文件，您必须允许列出相应区域的 Systems Manager 端点。这些端点在 *Amazon Web Services 一般参考* 的 [Amazon Systems Manager](https://docs.amazonaws.cn/general/latest/gr/rande.html#ssm_region) 中列出。

## 将 CloudWatch 代理程序包下载到您的第一个实例
<a name="install-CloudWatch-Agent-EC2-first"></a>

使用以下步骤来使用 Systems Manager 下载 CloudWatch 代理软件包。

**使用 Systems Manager 下载 CloudWatch 代理**

1. 通过 [https://console.aws.amazon.com/systems-manager/](https://console.amazonaws.cn/systems-manager/) 打开 Systems Manager 控制台。

1. 在导航窗格中，选择 **Run Command**。

   –或者–

   如果打开了 Amazon Systems Manager 主页，请向下滚动并选择 **Explore Run Command（浏览 Run Command）**。

1. 选择 **Run command（运行命令）**。

1. 在 **Command document（命令文档）**列表中，选择 **AWS-ConfigureAWSPackage**。

1. 在 **Targets（目标）**区域中，选择要在其上安装 CloudWatch 代理的实例。如果您没有看到特定实例，则可能未将其配置为可使用 Systems Manager 的托管式实例。有关更多信息，请参阅 *Amazon Systems Manager 用户指南*中的[为混合环境设置 Amazon Systems Manager](https://docs.amazonaws.cn/systems-manager/latest/userguide/systems-manager-managedinstances.html)。

1. 在**操作**列表中，选择**安装**。

1. 在 **Name (名称)** 字段中，输入 *AmazonCloudWatchAgent*。

1. 将 **Version (版本)** 设置为 **latest (最新)** 以安装最新版本的代理。

1. 选择**运行**。

1. （可选）在 **Targets and outputs（目标和输出）**区域中，选择实例名称旁边的按钮，然后选择 **View output（查看输出）**。Systems Manager 将显示已成功安装该代理。

   

## 创建和修改代理配置文件
<a name="CW-Agent-Instance-Create-Configuration-File-first"></a>

在下载 CloudWatch 代理后，您必须先创建配置文件，然后再在任何服务器上启动该代理。

如果您打算在 Systems Manager Parameter Store 中保存代理配置文件，则必须使用 EC2 实例以保存到 Parameter Store。此外，您必须首先使用 `CloudWatchAgentAdminRole` IAM 角色附加到该实例。有关附加角色的更多信息，请参阅《Amazon EC2 用户指南》**中的[将 IAM 角色附加到实例](https://docs.amazonaws.cn/AWSEC2/latest/WindowsGuide/iam-roles-for-amazon-ec2.html#attach-iam-role)。

有关创建 CloudWatch 代理配置文件的更多信息，请参阅 [创建 CloudWatch 代理配置文件](create-cloudwatch-agent-configuration-file.md)。

## 使用代理配置在其他 EC2 实例上安装和启动 CloudWatch 代理
<a name="install-CloudWatch-Agent-on-EC2-Instance-fleet"></a>

在 Parameter Store 中保存 CloudWatch 代理配置后，您可以在其他服务器上安装该代理时使用该配置。

对于每台服务器，请按照本节前面列出的步骤验证 Systems Manager 的先决条件、SSM 代理的版本和互联网访问权限。然后，使用您创建的 CloudWatch 代理配置文件，按照以下说明在其他实例上安装 CloudWatch 代理。

**步骤 1：下载和安装 CloudWatch 代理**

要能够将 CloudWatch 数据发送到其他区域，请确保您附加到此实例的 IAM 角色有权在该区域中写入 CloudWatch 数据。

下面是使用 `aws configure` 命令为 CloudWatch 代理创建命名配置文件的示例。该示例假设您使用默认配置文件名称 `AmazonCloudWatchAgent`。

**为 CloudWatch 代理创建 AmazonCloudWatchAgent 配置文件**
+ 在 Linux 服务器上，键入以下命令并按照提示进行操作：

  ```
  sudo aws configure --profile AmazonCloudWatchAgent
  ```

  在 Windows Server 上，以管理员身份打开 PowerShell，键入以下命令并按照提示进行操作。

  ```
  aws configure --profile AmazonCloudWatchAgent
  ```

## 使用代理配置在其他 EC2 实例上安装 CloudWatch 代理
<a name="install-CloudWatch-Agent-on-EC2-Instance-fleet"></a>

在 Parameter Store 中保存 CloudWatch 代理配置后，您可以在其他服务器上安装该代理时使用该配置。

对于每台服务器，请按照本节前面列出的步骤验证 Systems Manager 的先决条件、SSM 代理的版本和互联网访问权限。然后，使用您创建的 CloudWatch 代理配置文件，按照以下说明在其他实例上安装 CloudWatch 代理。

**步骤 1：下载和安装 CloudWatch 代理**

**注意**  
当您安装或更新 CloudWatch 代理时，仅支持 **Uninstall and reinstall**（卸载并重新安装）选项。您无法使用 **In-place update**（就地更新）选项。

您可以使用 Systems Manager Run Command 或 Amazon S3 下载链接下载 CloudWatch 代理程序包。

Systems Manager Run Command 使您能够管理实例配置。您可以指定一个 Systems Manager 文档，指定一些参数，然后在一个或多个实例上执行命令。实例上的 SSM Agent 负责处理命令并按指定方式配置实例。

**使用 Run Command 下载 CloudWatch 代理**

1. 通过 [https://console.aws.amazon.com/systems-manager/](https://console.amazonaws.cn/systems-manager/) 打开 Systems Manager 控制台。

1. 在导航窗格中，选择 **Run Command**。

   –或者–

   如果打开了 Amazon Systems Manager 主页，请向下滚动并选择 **Explore Run Command（浏览 Run Command）**。

1. 选择 **Run command（运行命令）**。

1. 在 **Command document（命令文档）**列表中，选择 **AWS-ConfigureAWSPackage**。

1. 在 **Targets（目标）**区域中，选择要在其上安装 CloudWatch 代理的实例。如果未看到特定的实例，则可能没有为 Run Command 配置该实例。有关更多信息，请参阅 *Amazon Systems Manager 用户指南*中的[为混合环境设置 Amazon Systems Manager](https://docs.amazonaws.cn/systems-manager/latest/userguide/systems-manager-managedinstances.html)。

1. 在**操作**列表中，选择**安装**。

1. 在 **Name (名称)** 框中，输入 *AmazonCloudWatchAgent*。

1. 将 **Version (版本)** 设置为 **latest (最新)** 以安装最新版本的代理。

1. 选择**运行**。

1. （可选）在 **Targets and outputs（目标和输出）**区域中，选择实例名称旁边的按钮，然后选择 **View output（查看输出）**。Systems Manager 将显示已成功安装该代理。

**步骤 2：使用您的代理配置文件启动 CloudWatch 代理**

使用 Systems Manager Run Command 执行以下步骤来启动代理。

有关如何在启用了安全增强型 Linux (SELinux) 的系统上设置代理的信息，请参阅[通过安全增强型 Linux (SELinux) 设置 CloudWatch 代理](CloudWatch-Agent-SELinux.md)。

**使用 Run Command 启动 CloudWatch 代理**

1. 通过 [https://console.aws.amazon.com/systems-manager/](https://console.amazonaws.cn/systems-manager/) 打开 Systems Manager 控制台。

1. 在导航窗格中，选择 **Run Command**。

   –或者–

   如果打开了 Amazon Systems Manager 主页，请向下滚动并选择 **Explore Run Command（浏览 Run Command）**。

1. 选择 **Run command（运行命令）**。

1. 在**命令文档**列表中，选择 **AmazonCloudWatch-ManageAgent**。

1. 在 **Targets（目标）**区域中，选择安装了 CloudWatch 代理的实例。

1. 在**操作**列表中，选择**配置**。

1. 在**可选的配置源**列表中，选择 **ssm**。

1. 在**可选的配置位置**框中，输入您创建并保存到 Systems Manager Parameter Store 的代理配置文件的 Systems Manager 参数名称，如 [创建 CloudWatch 代理配置文件](create-cloudwatch-agent-configuration-file.md) 中所述。

1. 在完成这些步骤后，在**可选的重新启动**列表中选择**是**以启动该代理。

1. 选择**运行**。

1. （可选）在 **Targets and outputs（目标和输出）**区域中，选择实例名称旁边的按钮，然后选择 **View output（查看输出）**。Systems Manager 将显示已成功启动该代理。

## （可选）修改 CloudWatch 代理的通用配置和命名配置文件
<a name="CloudWatch-Agent-profile-instance-fleet"></a>

CloudWatch 代理包含一个名为 `common-config.toml` 的配置文件。您可以使用该文件指定代理和区域信息（可选）。

在运行 Linux 的服务器上，该文件位于 `/opt/aws/amazon-cloudwatch-agent/etc` 目录中。在运行 Windows Server 的服务器上，该文件位于 `C:\ProgramData\Amazon\AmazonCloudWatchAgent` 目录中。

默认 `common-config.toml` 如下所示：

```
# This common-config is used to configure items used for both ssm and cloudwatch access
 
 
## Configuration for shared credential.
## Default credential strategy will be used if it is absent here:
##            Instance role is used for EC2 case by default.
##            AmazonCloudWatchAgent profile is used for onPremise case by default.
# [credentials]
#    shared_credential_profile = "{profile_name}"
#    shared_credential_file= "{file_name}"
 
## Configuration for proxy.
## System-wide environment-variable will be read if it is absent here.
## i.e. HTTP_PROXY/http_proxy; HTTPS_PROXY/https_proxy; NO_PROXY/no_proxy
## Note: system-wide environment-variable is not accessible when using ssm run-command.
## Absent in both here and environment-variable means no proxy will be used.
# [proxy]
#    http_proxy = "{http_url}"
#    https_proxy = "{https_url}"
#    no_proxy = "{domain}"
```

最初将注释所有行。要设置凭证配置文件或代理设置，请从该行中删除 `#` 并指定一个值。您可以手动编辑该文件，或者使用 Systems Manager 中的 `RunShellScript` Run Command 执行该操作：
+ `shared_credential_profile` – 对于本地部署服务器，此行指定 IAM 用户凭证配置文件，以用于将数据发送到 CloudWatch。如果您将此行注释掉，则会使用 `AmazonCloudWatchAgent`。

  在 EC2 实例上，您可以使用此行让 CloudWatch 代理将数据从该实例发送到不同 Amazon 区域中的 CloudWatch。要执行此操作，请指定一个包含 `region` 字段的命名配置文件，该字段指定要发送到的区域的名称。

  如果指定 `shared_credential_profile`，您还必须从 `[credentials]` 行开头删除 `#`。
+ `shared_credential_file` – 要让代理在位于默认路径以外的路径中的文件中查找凭证，请在此处指定完整的路径和文件名。在 Linux 上，默认路径为 `/root/.aws`；在 Windows Server 上，默认路径为 `C:\\Users\\Administrator\\.aws`。

  下面的第一个示例显示对 Linux 服务器有效的 `shared_credential_file` 行的语法，第二个示例对 Windows Server 有效。在 Windows Server 上，您必须转义 \$1 字符。

  ```
  shared_credential_file= "/usr/username/credentials"
  ```

  ```
  shared_credential_file= "C:\\Documents and Settings\\username\\.aws\\credentials"
  ```

  如果指定 `shared_credential_file`，您还必须从 `[credentials]` 行开头删除 `#`。
+ 代理设置 – 如果您的服务器使用 HTTP 或 HTTPS 代理联系 Amazon 服务，请在 `http_proxy` 和 `https_proxy` 字段中指定这些代理。如果应从代理中排除某些 URL，请在 `no_proxy` 字段中指定这些 URL 并以逗号分隔。