

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

# 在內部部署伺服器上安裝 CloudWatch 代理程式
<a name="install-CloudWatch-Agent-on-premise"></a>

 若您已在一台電腦上下載 CloudWatch 代理程式，並建立代理程式設定檔，可以使用該設定檔來在其他內部部署伺服器上安裝代理程式。

## 在內部部署伺服器下載 CloudWatch 代理程式
<a name="download-CloudWatch-Agent-onprem"></a>

您可以使用 Systems Manager 執行命令或 Simple Storage Service (Amazon S3) 下載連結來下載 CloudWatch 代理程式套件。

### 使用 Systems Manager 下載
<a name="download-CloudWatch-Agent-onprem-fleet-sys"></a>

若要使用 Systems Manager 執行命令，您必須使用 Amazon EC2 Systems Manager 註冊您的內部部署伺服器。如需詳細資訊，請參閱*《AWS Systems Manager 使用者指南》*中的[在混合環境中設定 Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-managedinstances.html)。

如果您已經註冊伺服器，請將 SSM Agent 更新為最新版本。

如需在執行 Linux 的伺服器上更新 SSM Agent 的相關資訊，請參閱*《AWS Systems Manager 使用者指南》*中的[在混合環境 (Linux) 中安裝 SSM Agent](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-managedinstances.html#sysman-install-managed-linux)。

如需在執行 Windows Server 的伺服器上更新 SSM Agent 的相關資訊，請參閱*《AWS Systems Manager 使用者指南》*中的[在混合環境 (Windows) 中安裝 SSM Agent](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-managedinstances.html#sysman-install-managed-win)。

**若要在現場部署伺服器上使用 SSM Agent 下載 CloudWatch 代理程式套件**

1. 在 https：//[https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/) 開啟 Systems Manager 主控台。

1. 在導覽窗格中，選擇 **執行命令**。

   -或-

   如果 AWS Systems Manager 首頁開啟，請向下捲動並選擇**探索執行命令**。

1. 選擇**執行命令**。

1. 在 **Command document (命令文件)** 清單中，選取 **AWS-ConfigureAWSPackage** 旁的按鈕。

1. 在 **Targets** (目標) 區域中，選取要在其上安裝 CloudWatch 代理程式的伺服器。若您沒有看到特定伺服器，可能是因為它尚未設定用於執行命令。如需詳細資訊，請參閱*AWS Systems Manager 《 使用者指南*》中的[AWS Systems Manager 設定混合環境](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-managedinstances.html)。

1. 在 **Action (動作)** 清單中，選擇 **Install (安裝)**。

1. 在 **Name (名稱)** 方塊中，輸入 {{AmazonCloudWatchAgent}}。

1. 將 **Version (版本)** 維持空白，以安裝代理程式的最新版本。

1. 選擇**執行**。

   下載代理程式套件之後，下一個步驟是設定然後啟動。

## （在內部部署伺服器上安裝） 指定 IAM 登入資料和 AWS 區域
<a name="install-CloudWatch-Agent-iam_user-SSM-onprem"></a>

若要啟用 CloudWatch 代理程式來從內部部署伺服器傳送資料，您必須指定您先前建立的 IAM 使用者存取金鑰及秘密金鑰。

您還必須使用 `region` 欄位指定 AWS 要傳送指標的區域。

以下是此檔案的範例。

```
[AmazonCloudWatchAgent]
aws_access_key_id={{my_access_key}}
aws_secret_access_key={{my_secret_key}}
region = {{us-west-1}}
```

針對 {{my\_access\_key}} 和 {{my\_secret\_key}}，使用沒有寫入 Systems Manager 參數存放區許可的 IAM 使用者金鑰。

若您將此描述檔命名為 `AmazonCloudWatchAgent`，您便不需要執行更多作業。您也可以選擇給予它一個不同的名稱，並將名稱指定為 ` common-config.toml` 檔案中 `shared_credential_profile` 的值，如下一節所述。

以下是使用 **aws configure** 命令建立 CloudWatch 代理程式具名描述檔的範例。此範例假設您使用 `AmazonCloudWatchAgent` 的預設描述檔名稱。

**若要建立 CloudWatch 代理程式的 AmazonCloudWatchAgent 描述檔**

1. 如果您尚未這麼做， AWS Command Line Interface 請在伺服器上安裝 。如需詳細資訊，請參閱[安裝 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html)。

1. 在 Linux 伺服器上，輸入以下命令並依提示操作：

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

   在 Windows Server 上，以系統管理員身分開啟 PowerShell，輸入以下命令並依照提示操作。

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

## (選用) 修改 CloudWatch 代理程式的常見組態及具名描述檔
<a name="CloudWatch-Agent-profile-onprem"></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` 執行命令：
+ `shared_credential_profile` – 針對現場部署伺服器，這一行會指定要用來將資料傳送給 CloudWatch 的 IAM 使用者憑證描述檔。若您將此行標示為註解，則會使用 `AmazonCloudWatchAgent`。如需建立此描述檔的詳細資訊，請參閱[（在內部部署伺服器上安裝） 指定 IAM 登入資料和 AWS 區域](#install-CloudWatch-Agent-iam_user-SSM-onprem)。

  在 EC2 執行個體上，您可以使用此行讓 CloudWatch 代理程式從此執行個體將資料傳送至不同區域中的 CloudWatch AWS 。若要執行此作業，請指定一個具名描述檔，其中包含指定要傳送對象區域名稱的 `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 上，您必須跳脫 \\ 字元。

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

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

  如果您指定 `shared_credential_file`，即必須也要移除 `[credentials]` 行開頭中的 `#`。
+ 代理設定 – 若您的伺服器使用 HTTP 或 HTTPS 代理來和 AWS 服務聯絡，請在 `http_proxy` 和 `https_proxy` 欄位中指定那些代理。如有必須排除在代理之外的 URL，請在 `no_proxy` 欄位中指定並以逗號分隔。