

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

# 安裝 AWS Systems Manager 代理程式
<a name="install-systems-manager-agent"></a>

Agent AWS Systems Manager (Systems Manager Agent) 是您安裝的 Amazon 軟體，可讓 Systems Manager 更新、管理和設定 Greengrass 核心裝置、Amazon EC2 執行個體和其他資源。代理程式會在 中處理並執行 Systems Manager 服務的請求 AWS 雲端。然後，代理程式會將狀態和執行時間資訊傳回 Systems Manager 服務。如需詳細資訊，請參閱*AWS Systems Manager 《 使用者指南*》中的[關於 Systems Manager 代理](https://docs.aws.amazon.com/systems-manager/latest/userguide/prereqs-ssm-agent.html)程式。

AWS 提供 Systems Manager Agent 做為 Greengrass 元件，您可以將其部署到您的 Greengrass 核心裝置，以使用 Systems Manager 管理它們。[Systems Manager Agent 元件](systems-manager-agent-component.md)會安裝 Systems Manager Agent 軟體，並將核心裝置註冊為 Systems Manager 中的受管節點。遵循此頁面上的步驟來完成先決條件，並將 Systems Manager Agent 元件部署至核心裝置或核心裝置群組。

**Topics**
+ [步驟 1：完成一般 Systems Manager 設定步驟](#setup-systems-manager)
+ [步驟 2：建立 Systems Manager 的 IAM 服務角色](#create-ssm-service-role)
+ [步驟 3：將許可新增至權杖交換角色](#update-token-exchange-role-ssm-agent)
+ [步驟 4：部署 Systems Manager Agent 元件](#deploy-ssm-agent-component)
+ [步驟 5：使用 Systems Manager 驗證核心裝置註冊](#verify-ssm-registration)

## 步驟 1：完成一般 Systems Manager 設定步驟
<a name="setup-systems-manager"></a>

如果您尚未這麼做，請完成 的一般設定步驟 AWS Systems Manager。如需詳細資訊，請參閱*AWS Systems Manager 《 使用者指南*》中的[完成一般 Systems Manager 設定步驟](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-edge-devices-setup-general.html)。

## 步驟 2：建立 Systems Manager 的 IAM 服務角色
<a name="create-ssm-service-role"></a>

Systems Manager Agent 使用 AWS Identity and Access Management (IAM) 服務角色與 通訊 AWS Systems Manager。Systems Manager 會擔任此角色，在每個核心裝置上啟用 Systems Manager 功能。當您部署元件時，Systems Manager Agent 元件也會使用此角色將核心裝置註冊為 Systems Manager 受管節點。如果您尚未這麼做，請為 Systems Manager Agent 元件建立要使用的 Systems Manager 服務角色。如需詳細資訊，請參閱[《 使用者指南》中的為邊緣裝置建立 IAM 服務角色](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-setting-up-edge-devices.html)。 *AWS Systems Manager *

## 步驟 3：將許可新增至權杖交換角色
<a name="update-token-exchange-role-ssm-agent"></a>

Greengrass 核心裝置使用稱為字符交換角色的 IAM 服務角色來與 AWS 服務互動。每個核心裝置都有您在[安裝 AWS IoT Greengrass 核心軟體](install-greengrass-core-v2.md)時建立的字符交換角色。許多 Greengrass 元件，例如 Systems Manager Agent，需要此角色的額外許可。Systems Manager 代理程式元件需要下列許可，其中包括使用您在 中建立之角色的許可[步驟 2：建立 Systems Manager 的 IAM 服務角色](#create-ssm-service-role)。

```
{
  "Version": "2012-10-17",		 	 	 
  "Statement": [
    {
      "Action": [
        "iam:PassRole"
      ],
      "Effect": "Allow",
      "Resource": [
        "arn:aws:iam::account-id:role/SSMServiceRole"
      ]
    },
    {
      "Action": [
        "ssm:AddTagsToResource",
        "ssm:RegisterManagedInstance"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}
```

如果您尚未這麼做，請將這些許可新增至核心裝置的字符交換角色，以允許 Systems Manager Agent 操作。您可以將新政策新增至權杖交換角色，以授予此許可。

### 將許可新增至權杖交換角色 （主控台）
<a name="update-token-exchange-role-ssm-agent-console"></a>

1. 在 [IAM 主控台](https://console.aws.amazon.com/iam)導覽功能表中，選擇**角色**。

1. 當您安裝 AWS IoT Greengrass Core 軟體時，請選擇您設定為字符交換角色的 IAM 角色。如果您在安裝 AWS IoT Greengrass Core 軟體時未指定權杖交換角色的名稱，則會建立名為 的角色`GreengrassV2TokenExchangeRole`。

1. 在**許可**下，選擇**新增許可**，然後選擇**連接政策**。

1. 選擇**建立政策**。**建立政策**頁面會在新的瀏覽器索引標籤中開啟。

1. 在 **Create policy (建立政策)** 頁面上，執行下列動作：

   1. 選擇 **JSON** 以開啟 JSON 編輯器。

   1. 將以下 政策貼到 JSON 編輯器。以您在 中建立的服務角色名稱取代 *SSMServiceRole*[步驟 2：建立 Systems Manager 的 IAM 服務角色](#create-ssm-service-role)。

      ```
      {
        "Version": "2012-10-17",		 	 	 
        "Statement": [
          {
            "Action": [
              "iam:PassRole"
            ],
            "Effect": "Allow",
            "Resource": [
              "arn:aws:iam::account-id:role/SSMServiceRole"
            ]
          },
          {
            "Action": [
              "ssm:AddTagsToResource",
              "ssm:RegisterManagedInstance"
            ],
            "Effect": "Allow",
            "Resource": "*"
          }
        ]
      }
      ```

   1. 選擇下**一步：標籤**。

   1. 選擇下**一步：檢閱**。

   1. 為政策輸入 **Name (名稱)**，例如 **GreengrassSSMAgentComponentPolicy**。

   1. 選擇**建立政策**。

   1. 切換到您開啟權杖交換角色的上一個瀏覽器索引標籤。

1. 在**新增許可**頁面上，選擇重新整理按鈕，然後選取您在上一個步驟中建立的 Greengrass Systems Manager 代理程式政策。

1. 選擇**連接政策**。

   使用此字符交換角色的核心裝置現在具有與 Systems Manager 服務互動的許可。

### 將許可新增至權杖交換角色 (AWS CLI)
<a name="update-token-exchange-role-ssm-agent-cli"></a>

**新增授予 Systems Manager 使用許可的政策**

1. 建立名為 的檔案`ssm-agent-component-policy.json`，並將下列 JSON 複製到 檔案。以您在 中建立的服務角色名稱取代 *SSMServiceRole*[步驟 2：建立 Systems Manager 的 IAM 服務角色](#create-ssm-service-role)。

   ```
   {
     "Version": "2012-10-17",		 	 	 
     "Statement": [
       {
         "Action": [
           "iam:PassRole"
         ],
         "Effect": "Allow",
         "Resource": [
           "arn:aws:iam::account-id:role/SSMServiceRole"
         ]
       },
       {
         "Action": [
           "ssm:AddTagsToResource",
           "ssm:RegisterManagedInstance"
         ],
         "Effect": "Allow",
         "Resource": "*"
       }
     ]
   }
   ```

1. 執行下列命令，從 中的政策文件建立政策`ssm-agent-component-policy.json`。

------
#### [ Linux or Unix ]

   ```
   aws iam create-policy \
     --policy-name GreengrassSSMAgentComponentPolicy \
     --policy-document file://ssm-agent-component-policy.json
   ```

------
#### [ Windows Command Prompt (CMD) ]

   ```
   aws iam create-policy ^
     --policy-name GreengrassSSMAgentComponentPolicy ^
     --policy-document file://ssm-agent-component-policy.json
   ```

------
#### [ PowerShell ]

   ```
   aws iam create-policy `
     --policy-name GreengrassSSMAgentComponentPolicy `
     --policy-document file://ssm-agent-component-policy.json
   ```

------

   從輸出中的政策中繼資料複製政策 Amazon Resource Name (ARN)。您可以在下一個步驟中使用此 ARN 將此政策連接至核心裝置角色。

1. 執行下列命令，將政策連接至字符交換角色。
   + 以您在安裝 AWS IoT Greengrass Core 軟體時指定的字符交換角色名稱取代 *GreengrassV2TokenExchangeRole*。如果您在安裝 AWS IoT Greengrass Core 軟體時未指定權杖交換角色的名稱，則會建立名為 的角色`GreengrassV2TokenExchangeRole`。
   + 將政策 ARN 取代為上一個步驟的 ARN。

------
#### [ Linux or Unix ]

   ```
   aws iam attach-role-policy \
     --role-name GreengrassV2TokenExchangeRole \
     --policy-arn arn:aws:iam::123456789012:policy/GreengrassSSMAgentComponentPolicy
   ```

------
#### [ Windows Command Prompt (CMD) ]

   ```
   aws iam attach-role-policy ^
     --role-name GreengrassV2TokenExchangeRole ^
     --policy-arn arn:aws:iam::123456789012:policy/GreengrassSSMAgentComponentPolicy
   ```

------
#### [ PowerShell ]

   ```
   aws iam attach-role-policy `
     --role-name GreengrassV2TokenExchangeRole `
     --policy-arn arn:aws:iam::123456789012:policy/GreengrassSSMAgentComponentPolicy
   ```

------

   如果命令沒有輸出，表示成功。使用此字符交換角色的核心裝置現在具有與 Systems Manager 服務互動的許可。

## 步驟 4：部署 Systems Manager Agent 元件
<a name="deploy-ssm-agent-component"></a>

完成下列步驟以部署和設定 Systems Manager Agent 元件。您可以將元件部署至單一核心裝置或一組核心裝置。

### 部署 Systems Manager Agent 元件 （主控台）
<a name="deploy-ssm-agent-component-console"></a>

1. 在[AWS IoT Greengrass 主控台](https://console.aws.amazon.com/greengrass)導覽功能表中，選擇**元件**。

1. 在**元件**頁面上，選擇**公有元件**索引標籤，然後選擇 **aws.greengrass.SystemsManagerAgent**。

1. 在 **aws.greengrass.SystemsManagerAgent** 頁面中，選擇**部署**。

1. <a name="deploy-component-choose-deployment-step"></a>從**新增至部署**中，選擇要修改的現有部署，或選擇建立新的部署，然後選擇**下一步**。

1. <a name="deploy-component-choose-target-step"></a>如果您選擇建立新的部署，請選擇部署的目標核心裝置或物件群組。在**指定目標**頁面的**部署目標**下，選擇核心裝置或物件群組，然後選擇**下一步**。

1. 在**選取元件**頁面上，確認已選取**aws.greengrass.SystemsManagerAgent**元件，選擇**下一步**。

1. 在**設定元件**頁面上，選取 **aws.greengrass.SystemsManagerAgent**，然後執行下列動作：

   1. 選擇**設定元件**。

   1. 在**設定aws.greengrass.SystemsManagerAgent**模態、**在組態更新**下，在**要合併的組態**中，輸入下列組態更新。以您在 中建立的服務角色名稱取代 *SSMServiceRole*[步驟 2：建立 Systems Manager 的 IAM 服務角色](#create-ssm-service-role)。

      ```
      {
        "SSMRegistrationRole": "SSMServiceRole",
        "SSMOverrideExistingRegistration": false
      }
      ```
**注意**  <a name="deploy-ssm-agent-component-configuration-options"></a>
如果核心裝置已執行以混合啟用註冊的 Systems Manager Agent，請將 `SSMOverrideExistingRegistration`變更為 `true`。此參數指定 Systems Manager Agent 已在具有混合啟用的裝置上執行時，Systems Manager Agent 元件是否註冊核心裝置。  
您也可以指定要新增至 Systems Manager Agent 元件為核心裝置建立之 Systems Manager 受管節點的標籤 (`SSMResourceTags`)。如需詳細資訊，請參閱 [Systems Manager Agent 元件組態](systems-manager-agent-component.md#systems-manager-agent-component-configuration)。

   1. 選擇**確認**以關閉模態，然後選擇**下一步**。

1. <a name="deploy-component-configure-advanced-settings-step"></a>在**設定進階設定**頁面上，保留預設組態設定，然後選擇 **下一步**。

1. <a name="deploy-component-review-and-deploy-step"></a>在 **Review (檢閱)** 頁面，選擇 **Deploy (部署)**。

   部署可能需要一分鐘的時間才能完成。

### 部署 Systems Manager Agent 元件 (AWS CLI)
<a name="deploy-ssm-agent-component-cli"></a>

若要部署 Systems Manager Agent 元件，請建立包含在 `components` 物件`aws.greengrass.SystemsManagerAgent`中的部署文件，並指定元件的組態更新。遵循 中的指示[建立部署](create-deployments.md)建立新的部署或修改現有的部署。

下列範例部分部署文件指定 使用名為 的服務角色`SSMServiceRole`。以您在 中建立的服務角色名稱取代 *SSMServiceRole*[步驟 2：建立 Systems Manager 的 IAM 服務角色](#create-ssm-service-role)。

```
{
  ...,
  "components": {
    ...,
    "aws.greengrass.SystemsManagerAgent": {
      "componentVersion": "1.0.0",
      "configurationUpdate": {
        "merge": "{\"SSMRegistrationRole\":\"SSMServiceRole\",\"SSMOverrideExistingRegistration\":false}"
      }
    }
  }
}
```

**注意**  <a name="deploy-ssm-agent-component-configuration-options"></a>
如果核心裝置已執行以混合啟用註冊的 Systems Manager Agent，請將 `SSMOverrideExistingRegistration`變更為 `true`。此參數指定 Systems Manager Agent 已在具有混合啟用的裝置上執行時，Systems Manager Agent 元件是否註冊核心裝置。  
您也可以指定要新增至 Systems Manager Agent 元件為核心裝置建立之 Systems Manager 受管節點的標籤 (`SSMResourceTags`)。如需詳細資訊，請參閱 [Systems Manager Agent 元件組態](systems-manager-agent-component.md#systems-manager-agent-component-configuration)。

可能需要幾分鐘才能完成部署。您可以使用 AWS IoT Greengrass 服務來檢查部署的狀態，也可以檢查 AWS IoT Greengrass Core 軟體日誌和 Systems Manager Agent 元件日誌，以確認 Systems Manager Agent 是否成功執行。如需詳細資訊，請參閱下列內容：
+ [檢查部署狀態](check-deployment-status.md)
+ [監控 AWS IoT Greengrass 日誌](monitor-logs.md)
+ *AWS Systems Manager 《 使用者指南*》中的[檢視 Systems Manager Agent 日誌](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-agent-logs.html) 

如果部署失敗或 Systems Manager Agent 未執行，您可以對每個核心裝置上的部署進行疑難排解。如需詳細資訊，請參閱下列內容：
+ [故障診斷 AWS IoT Greengrass V2](troubleshooting.md)
+ *AWS Systems Manager 《 使用者指南*》中的 [Systems Manager Agent 故障診斷](https://docs.aws.amazon.com/systems-manager/latest/userguide/troubleshooting-ssm-agent.html) 

## 步驟 5：使用 Systems Manager 驗證核心裝置註冊
<a name="verify-ssm-registration"></a>

Systems Manager Agent 元件執行時，會將核心裝置註冊為 Systems Manager 中的受管節點。您可以使用 AWS IoT Greengrass 主控台、Systems Manager 主控台和 Systems Manager API 來驗證核心裝置是否已註冊為受管節點。受管節點在主控台和 API AWS 的部分中也稱為執行個體。

### 驗證核心裝置註冊AWS IoT Greengrass （主控台）
<a name="verify-ssm-registration-greengrass-console"></a>

1. 在[AWS IoT Greengrass 主控台](https://console.aws.amazon.com/greengrass)導覽功能表中，選擇**核心裝置**。

1. 選擇要驗證的核心裝置。

1. 在核心裝置的詳細資訊頁面上，尋找**AWS Systems Manager 執行個體**屬性。如果此屬性存在並顯示 Systems Manager 主控台的連結，則核心裝置會註冊為受管節點。

   您也可以找到 **AWS Systems Manager ping 狀態**屬性，以檢查核心裝置上的 Systems Manager 代理程式狀態。當狀態為**線上**時，您可以使用 Systems Manager 管理核心裝置。

### 驗證核心裝置註冊 (Systems Manager 主控台）
<a name="verify-ssm-registration-ssm-console"></a>

1. 在 [Systems Manager 主控台](https://console.aws.amazon.com/systems-manager)導覽功能表中，選擇**機群管理員**。

1. 在**受管節點**下，執行下列動作：

   1. 新增**來源類型**為 的篩選條件**AWS::IoT::Thing**。

   1. 新增篩選條件，其中**來源 ID** 是要驗證的核心裝置名稱。

1. 在**受管節點**資料表中尋找核心裝置。如果核心裝置位於 資料表中，則會將其註冊為受管節點。

   您也可以找到 **Systems Manager 代理程式 ping 狀態**屬性，以檢查核心裝置上的 Systems Manager 代理程式狀態。當狀態為**線上**時，您可以使用 Systems Manager 管理核心裝置。

### 驗證核心裝置註冊 (AWS CLI)
<a name="verify-ssm-registration-cli"></a>
+ 使用 [DescribeInstanceInformation](https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_DescribeInstanceInformation.html) 操作，取得符合您指定篩選條件的受管節點清單。執行下列命令來驗證核心裝置是否已註冊為受管節點。以要驗證的核心裝置名稱取代 *MyGreengrassCore*。

  ```
  aws ssm describe-instance-information --filter Key=SourceIds,Values=MyGreengrassCore Key=SourceTypes,Values=AWS::IoT::Thing
  ```

  回應包含符合篩選條件的受管節點清單。如果清單包含受管節點，則核心裝置會註冊為受管節點。您也可以在回應中找到有關核心裝置受管節點的其他資訊。如果 `PingStatus` 屬性是 `Online`，您可以使用 Systems Manager 管理核心裝置。

在 Systems Manager 中確認核心裝置已註冊為受管節點後，您可以使用 Systems Manager 主控台和 API 來管理該核心裝置。如需可用來管理 Greengrass 核心裝置的 Systems Manager 功能的詳細資訊，請參閱*AWS Systems Manager 《 使用者指南*》中的 [Systems Manager 功能](https://docs.aws.amazon.com/systems-manager/latest/userguide/features.html)。