

• 2026 年 4 月 30 日之後將不再提供 AWS Systems Manager CloudWatch Dashboard。客戶可以繼續使用 Amazon CloudWatch 主控台來檢視、建立和管理其 Amazon CloudWatch 儀表板，就像現在一樣。如需詳細資訊，請參閱 [Amazon CloudWatch Dashboard 文件](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)。

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

# 演練：使用資源資料同步來彙總庫存資料
<a name="inventory-resource-data-sync"></a>

下列逐步解說說明如何使用 AWS Command Line Interface () 為 AWS Systems Manager 庫存建立資源資料同步組態AWS CLI。資源資料同步會自動將所有受管節點的庫存資料移至中央 Amazon Simple Storage Service (Amazon S3) 儲存貯體。每當搜索到新的庫存資料時，同步就會自動更新中央 Amazon S3 儲存貯體內的資料。

本演練也說明如何使用 Amazon Athena 和 Amazon Quick 來查詢和分析彙總資料。如需使用 中的 Systems Manager 建立資源資料同步的詳細資訊 AWS 管理主控台，請參閱 [演練：使用資源資料同步來彙總庫存資料](#inventory-resource-data-sync)。如需有關使用 中的 Systems Manager 從多個 AWS 區域 和 帳戶查詢清查的資訊 AWS 管理主控台，請參閱 [查詢來自多個區域和帳戶的清查資料](systems-manager-inventory-query.md)。

**注意**  
此逐步教學涵蓋的資訊能協助您使用 AWS Key Management Service (AWS KMS) 加密同步作業。庫存並不會收集任何使用者專用、私有或敏感資料，因此加密為選用功能。如需 的詳細資訊 AWS KMS，請參閱 [AWS Key Management Service 開發人員指南](https://docs.aws.amazon.com/kms/latest/developerguide/)。

**開始之前**  
在開始本節中的演練之前，請先檢閱或完成以下任務：
+ 收集受管節點的庫存資料。基於本演練中 Amazon Athena 和 Amazon Quick 章節的目的，我們建議您收集應用程式資料。如需如何收集庫存資料的詳細資訊，請參閱 [設定清查收集](inventory-collection.md) 或 [使用 AWS CLI 設定庫存資料收集](inventory-collection-cli.md)。
+ （選用） 如果庫存資料存放在使用 AWS Key Management Service () 加密的 Amazon Simple Storage Service (Amazon S3 AWS KMS) 儲存貯體中，您也必須設定 IAM 帳戶和服務`Amazon-GlueServiceRoleForSSM`角色以進行 AWS KMS 加密。如果您未設定 IAM 帳戶和此角色，Systems Manager 會在您選擇主控台上的 **Detailed View** (詳細檢視) 索引標籤時顯示 `Cannot load Glue tables`。如需詳細資訊，請參閱[（選用） 設定檢視 AWS KMS 加密資料的許可](systems-manager-inventory-query.md#systems-manager-inventory-query-kms)。
+ （選用） 如果您想要使用 加密資源資料同步 AWS KMS，則必須建立新的金鑰，其中包含下列政策，或者您必須更新現有的金鑰並將此政策新增至其中。

------
#### [ JSON ]

****  

  ```
  {
      "Version":"2012-10-17",		 	 	 
      "Id": "ssm-access-policy",
      "Statement": [
          {
              "Sid": "ssm-access-policy-statement",
              "Action": [
                  "kms:GenerateDataKey"
              ],
              "Effect": "Allow",
              "Principal": {
                  "Service": "ssm.amazonaws.com"
              },
              "Resource": "arn:aws:kms:us-east-1:123456789012:key/KMS_key_id",
              "Condition": {
                  "StringLike": {
                      "aws:SourceAccount": "123456789012"
                  },
                  "ArnLike": {
                      "aws:SourceArn": "arn:aws:ssm:*:123456789012:resource-data-sync/*"
                  }
              }
          }
      ]
  }
  ```

------

**建立庫存的資源資料同步**

1. 開啟位於 [https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/) 的 Amazon S3 主控台。

1. 建立儲存貯體以存放您彙整的清查資料。如需詳細資訊，請參閱 *Amazon Simple Storage Service User Guide* 中的 [Creating a bucket](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html)。請記下儲存貯體名稱和您建立儲存貯體 AWS 區域 所在的 。

1. 建立儲存貯體後，請選擇 **Permissions (許可)** 索引標籤，接著選擇 **Bucket Policy (儲存貯體政策)**。

1. 複製下列儲存貯體政策並貼至政策編輯器。將 amzn-s3-demo-bucket 和 *account-id* 取代為您建立的 Amazon S3 儲存貯體名稱和有效的 AWS 帳戶 ID。新增多個帳戶時，請為每個帳戶新增額外的條件字串和 ARN。新增帳戶時，請移除範例中額外的預留位置。或者，您也可以使用 Amazon S3 字首 (子目錄) 取代 *bucket-prefix*。如果您沒有建立字首，請將 *bucket-prefix/* 從政策中的 ARN 移除。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Sid": " SSMBucketDelivery",
               "Effect": "Allow",
               "Principal": {
                   "Service": "ssm.amazonaws.com"
               },
               "Action": "s3:PutObject",
               "Resource": [
                   "arn:aws:s3:::amzn-s3-demo-bucket/bucket-prefix/*/accountid=111122223333/*"
               ],
               "Condition": {
                   "StringEquals": {
                       "s3:x-amz-acl": "bucket-owner-full-control",
                       "aws:SourceAccount": [
                           "111122223333",
                           "444455556666",
                           "123456789012",
                           "777788889999"
                       ]
                   },
                   "ArnLike": {
                       "aws:SourceArn": [
                           "arn:aws:ssm:*:111122223333:resource-data-sync/*",
                           "arn:aws:ssm:*:444455556666:resource-data-sync/*",
                           "arn:aws:ssm:*:123456789012:resource-data-sync/*",
                           "arn:aws:ssm:*:777788889999:resource-data-sync/*"
                       ]
                   }
               }
           }
       ]
   }
   ```

------

1. (選用) 如果您想要加密同步作業，您必須將以下條件新增至前一步驟中所列的政策。將這些新增至 `StringEquals` 章節中。

   ```
   "s3:x-amz-server-side-encryption":"aws:kms",
   "s3:x-amz-server-side-encryption-aws-kms-key-id":"arn:aws:kms:region:account_ID:key/KMS_key_ID"
   ```

   請見此處範例：

   ```
   "StringEquals": {
             "s3:x-amz-acl": "bucket-owner-full-control",
             "aws:SourceAccount": "account-id",
             "s3:x-amz-server-side-encryption":"aws:kms",
             "s3:x-amz-server-side-encryption-aws-kms-key-id":"arn:aws:kms:region:account_ID:key/KMS_key_ID"
           }
   ```

1. 如果您尚未安裝並設定 AWS Command Line Interface (AWS CLI)，請安裝並設定 。

   如需相關資訊，請參閱[安裝或更新最新版本的 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)。

1. （選用） 如果您想要加密同步，請執行下列命令來驗證儲存貯體政策是否強制執行 AWS KMS 金鑰需求。將每個*範例資源預留位置*取代為您自己的資訊。

------
#### [ Linux & macOS ]

   ```
   aws s3 cp ./A_file_in_the_bucket s3://amzn-s3-demo-bucket/prefix/ \
   --sse aws:kms \
   --sse-kms-key-id "arn:aws:kms:region:account_ID:key/KMS_key_id" \
   --region region, for example, us-east-2
   ```

------
#### [ Windows ]

   ```
   aws s3 cp ./A_file_in_the_bucket s3://amzn-s3-demo-bucket/prefix/ ^ 
       --sse aws:kms ^
       --sse-kms-key-id "arn:aws:kms:region:account_ID:key/KMS_key_id" ^
       --region region, for example, us-east-2
   ```

------

1. 請執行下列命令，以您在此程序開頭建立的 Amazon S3 儲存貯體，建立資源資料同步組態。此命令會從 AWS 區域 您登入的 建立同步。
**注意**  
如果同步與目標 Amazon S3 儲存貯體位於不同區域，您可能需要支付資料傳輸費用。如需詳細資訊，請參閱 [Amazon S3 定價](https://aws.amazon.com/s3/pricing/)。

------
#### [ Linux & macOS ]

   ```
   aws ssm create-resource-data-sync \
   --sync-name a_name \
   --s3-destination "BucketName=amzn-s3-demo-bucket,Prefix=prefix_name, if_specified,SyncFormat=JsonSerDe,Region=bucket_region"
   ```

------
#### [ Windows ]

   ```
   aws ssm create-resource-data-sync ^
   --sync-name a_name ^
   --s3-destination "BucketName=amzn-s3-demo-bucket,Prefix=prefix_name, if_specified,SyncFormat=JsonSerDe,Region=bucket_region"
   ```

------

   您可以透過 `region` 參數，指定應建立同步組態的位置。在以下範例中，系統會將來自 us-west-1 區域的庫存資料同步至 us-west-2 區域內的 Amazon S3 儲存貯體。

------
#### [ Linux & macOS ]

   ```
   aws ssm create-resource-data-sync \
       --sync-name InventoryDataWest \
       --s3-destination "BucketName=amzn-s3-demo-bucket,Prefix=HybridEnv,SyncFormat=JsonSerDe,Region=us-west-2" 
       --region us-west-1
   ```

------
#### [ Windows ]

   ```
   aws ssm create-resource-data-sync ^ 
   --sync-name InventoryDataWest ^
   --s3-destination "BucketName=amzn-s3-demo-bucket,Prefix=HybridEnv,SyncFormat=JsonSerDe,Region=us-west-2" ^ --region us-west-1
   ```

------

   （選用） 如果您想要使用 加密同步 AWS KMS，請執行下列命令來建立同步。如果您加密同步，則 AWS KMS 金鑰和 Amazon S3 儲存貯體必須位於相同的區域。

------
#### [ Linux & macOS ]

   ```
   aws ssm create-resource-data-sync \
   --sync-name sync_name \
   --s3-destination "BucketName=amzn-s3-demo-bucket,Prefix=prefix_name, if_specified,SyncFormat=JsonSerDe,AWSKMSKeyARN=arn:aws:kms:region:account_ID:key/KMS_key_ID,Region=bucket_region" \
   --region region
   ```

------
#### [ Windows ]

   ```
   aws ssm create-resource-data-sync ^
   --sync-name sync_name ^
   --s3-destination "BucketName=amzn-s3-demo-bucket,Prefix=prefix_name, if_specified,SyncFormat=JsonSerDe,AWSKMSKeyARN=arn:aws:kms:region:account_ID:key/KMS_key_ID,Region=bucket_region" ^
   --region region
   ```

------

1. 執行下列命令，藉此檢視同步組態狀態。

   ```
   aws ssm list-resource-data-sync 
   ```

   若您在不同區域中建立同步組態，請務必指定 `region` 參數，如下方範例所示。

   ```
   aws ssm list-resource-data-sync --region us-west-1
   ```

1. 成功建立同步組態後，請檢查 Amazon S3 中的目標儲存貯體。庫存資料應該會在幾分鐘內顯示。

**使用 Amazon Athena 的資料**

下節會介紹在 Amazon Athena 中檢視及查詢資料的方法。開始操作前，建議您了解 Athena。如需詳細資訊，請參閱《*Amazon Athena 使用者指南*》中的[什麼是 Amazon Athena？](https://docs.aws.amazon.com/athena/latest/ug/what-is.html)和[使用資料](https://docs.aws.amazon.com/athena/latest/ug/work-with-data.html)。

**檢視和查詢 Amazon Athena 中的資料**

1. 前往 [https://console.aws.amazon.com/athena/](https://console.aws.amazon.com/athena/home) 開啟 Athena 主控台。

1. 複製下列陳述式並貼至查詢編輯器，然後選擇**執行查詢**。

   ```
   CREATE DATABASE ssminventory
   ```

   系統會建立名為 ssminventory 的資料庫。

1. 複製下列陳述式並貼至查詢編輯器，然後選擇**執行查詢**。使用 Amazon S3 目標的名稱和字首取代 amzn-s3-demo-bucket 和 *bucket\$1prefix*。

   ```
   CREATE EXTERNAL TABLE IF NOT EXISTS ssminventory.AWS_Application (
   Name string,
   ResourceId string,
   ApplicationType string,
   Publisher string,
   Version string,
   InstalledTime string,
   Architecture string,
   URL string,
   Summary string,
   PackageId string
   ) 
   PARTITIONED BY (AccountId string, Region string, ResourceType string)
   ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
   WITH SERDEPROPERTIES (
     'serialization.format' = '1'
   ) LOCATION 's3://amzn-s3-demo-bucket/bucket_prefix/AWS:Application/'
   ```

1. 複製下列陳述式並貼至查詢編輯器，然後選擇**執行查詢**。

   ```
   MSCK REPAIR TABLE ssminventory.AWS_Application
   ```

   系統將分割資料表。
**注意**  
如果您從其他 AWS 區域 或 建立資源資料同步 AWS 帳戶，則必須再次執行此命令來更新分割區。另外，您可能也需要更新 Amazon S3 儲存貯體政策。

1. 選擇 `AWS_Application` 資料表旁的檢視圖示，即可預覽資料。  
![\[Amazon Athena 的預覽資料圖示。\]](http://docs.aws.amazon.com/zh_tw/systems-manager/latest/userguide/images/sysman-inventory-resource-data-sync-walk.png)

1. 複製下列陳述式並貼至查詢編輯器，然後選擇**執行查詢**。

   ```
   SELECT a.name, a.version, count( a.version) frequency 
   from aws_application a where
   a.name = 'aws-cfn-bootstrap'
   group by a.name, a.version
   order  by frequency desc
   ```

   查詢會傳回不同版本的 計數`aws-cfn-bootstrap`，這是 LinuxmacOS、 和 的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體上存在 AWS 的應用程式Windows Server。

1. 分別複製下列陳述式並貼至查詢編輯器，使用 Amazon S3 的相關資訊取代 amzn-s3-demo-bucket 和 *bucket-prefix*，然後選擇**執行查詢**。這些陳述式能夠設定 Athena 中的其他庫存資料表。

   ```
   CREATE EXTERNAL TABLE IF NOT EXISTS ssminventory.AWS_AWSComponent (
    `ResourceId` string,
     `Name` string,
     `ApplicationType` string,
     `Publisher` string,
     `Version` string,
     `InstalledTime` string,
     `Architecture` string,
     `URL` string
   )
   PARTITIONED BY (AccountId string, Region string, ResourceType string)
   ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
   WITH SERDEPROPERTIES (
     'serialization.format' = '1'
   ) LOCATION 's3://amzn-s3-demo-bucket/bucket-prefix/AWS:AWSComponent/'
   ```

   ```
   MSCK REPAIR TABLE ssminventory.AWS_AWSComponent
   ```

   ```
   CREATE EXTERNAL TABLE IF NOT EXISTS ssminventory.AWS_WindowsUpdate (
     `ResourceId` string,
     `HotFixId` string,
     `Description` string,
     `InstalledTime` string,
     `InstalledBy` string
   )
   PARTITIONED BY (AccountId string, Region string, ResourceType string)
   ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
   WITH SERDEPROPERTIES (
     'serialization.format' = '1'
   ) LOCATION 's3://amzn-s3-demo-bucket/bucket-prefix/AWS:WindowsUpdate/'
   ```

   ```
   MSCK REPAIR TABLE ssminventory.AWS_WindowsUpdate
   ```

   ```
   CREATE EXTERNAL TABLE IF NOT EXISTS ssminventory.AWS_InstanceInformation (
     `AgentType` string,
     `AgentVersion` string,
     `ComputerName` string,
     `IamRole` string,
     `InstanceId` string,
     `IpAddress` string,
     `PlatformName` string,
     `PlatformType` string,
     `PlatformVersion` string
   )
   PARTITIONED BY (AccountId string, Region string, ResourceType string)
   ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
   WITH SERDEPROPERTIES (
     'serialization.format' = '1'
   ) LOCATION 's3://amzn-s3-demo-bucket/bucket-prefix/AWS:InstanceInformation/'
   ```

   ```
   MSCK REPAIR TABLE ssminventory.AWS_InstanceInformation
   ```

   ```
   CREATE EXTERNAL TABLE IF NOT EXISTS ssminventory.AWS_Network (
     `ResourceId` string,
     `Name` string,
     `SubnetMask` string,
     `Gateway` string,
     `DHCPServer` string,
     `DNSServer` string,
     `MacAddress` string,
     `IPV4` string,
     `IPV6` string
   )
   PARTITIONED BY (AccountId string, Region string, ResourceType string)
   ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
   WITH SERDEPROPERTIES (
     'serialization.format' = '1'
   ) LOCATION 's3://amzn-s3-demo-bucket/bucket-prefix/AWS:Network/'
   ```

   ```
   MSCK REPAIR TABLE ssminventory.AWS_Network
   ```

   ```
   CREATE EXTERNAL TABLE IF NOT EXISTS ssminventory.AWS_PatchSummary (
     `ResourceId` string,
     `PatchGroup` string,
     `BaselineId` string,
     `SnapshotId` string,
     `OwnerInformation` string,
     `InstalledCount` int,
     `InstalledOtherCount` int,
     `NotApplicableCount` int,
     `MissingCount` int,
     `FailedCount` int,
     `OperationType` string,
     `OperationStartTime` string,
     `OperationEndTime` string
   )
   PARTITIONED BY (AccountId string, Region string, ResourceType string)
   ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
   WITH SERDEPROPERTIES (
     'serialization.format' = '1'
   ) LOCATION 's3://amzn-s3-demo-bucket/bucket-prefix/AWS:PatchSummary/'
   ```

   ```
   MSCK REPAIR TABLE ssminventory.AWS_PatchSummary
   ```

**在 Amazon Quick 中使用資料**

下一節提供在 Amazon Quick 中建置視覺化的連結概觀。

**在 Amazon Quick 中建置視覺化**

1. 註冊 [Amazon Quick](https://quicksight.aws/)，然後登入 Quick 主控台。

1. 建立來自您所建立之 `AWS_Application` 資料表和任何其他資料表的資料集。如需詳細資訊，請參閱《[Amazon Quick 使用者指南》中的使用 Amazon Athena 資料建立資料集](https://docs.aws.amazon.com/quicksuite/latest/userguide/create-a-data-set-athena.html)。 **

1. 聯結資料表。舉例而言，您可以聯結 `AWS_InstanceInformation` 中的 `instanceid` 欄位，因為該欄位與其他庫存資料表中的 `resourceid` 欄位相符。如需聯結資料表的詳細資訊，請參閱《*Amazon Quick 使用者指南*》中的[聯結資料](https://docs.aws.amazon.com/quicksuite/latest/userguide/joining-data.html)。

1. 建置視覺效果。如需詳細資訊，請參閱《[Amazon Quick 使用者指南》中的分析和報告：在 Amazon Quick Sight 中視覺化資料](https://docs.aws.amazon.com/quicksuite/latest/userguide/working-with-visuals.html)。 **