逐步解說:使用 CLI - AWS Systems Manager

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

逐步解說:使用 CLI

以下程序會逐步引導您將 AWS Systems Manager 庫存設定為從受管節點中收集中繼資料的程序。設定庫存集合時,首先要建立 Systems Manager State Manager 關聯。Systems Manager 會在執行關聯時收集庫存資料。如果沒有先建立關聯,則當您試圖使用 Systems Manager Run Command 等呼叫 aws:softwareInventory 外掛程式時,系統即會傳回以下錯誤:

The aws:softwareInventory plugin can only be invoked via ssm-associate.

注意

一個節點一次只能設定一個庫存關聯。若您為節點設定兩個以上的庫存關聯,關聯便不會執行,並且也不會收集任何庫存資料。

快速設定所有受管節點以進行詳細目錄 (CLI)

您可以快速設定您 AWS 帳戶 和目前區域中的所有受管節點,以收集庫存資料。這是過程稱為全域庫存關聯的建立。若要透過 AWS CLI建立全域庫存關聯,請針對 instanceIds 值使用萬用字元選項,如下方範例所示:

設定您 AWS 帳戶 和目前區域中所有受管節點的清查 (CLI)
  1. 安裝和配置 AWS Command Line Interface (AWS CLI),如果你還沒有。

    如需相關資訊,請參閱安裝或更新最新版本的 AWS CLI

  2. 執行下列命令。

    Linux & macOS
    aws ssm create-association \ --name AWS-GatherSoftwareInventory \ --targets Key=InstanceIds,Values=* \ --schedule-expression "rate(1 day)" \ --parameters applications=Enabled,awsComponents=Enabled,customInventory=Enabled,instanceDetailedInformation=Enabled,networkConfig=Enabled,services=Enabled,windowsRoles=Enabled,windowsUpdates=Enabled
    Windows
    aws ssm create-association ^ --name AWS-GatherSoftwareInventory ^ --targets Key=InstanceIds,Values=* ^ --schedule-expression "rate(1 day)" ^ --parameters applications=Enabled,awsComponents=Enabled,customInventory=Enabled,instanceDetailedInformation=Enabled,networkConfig=Enabled,services=Enabled,windowsRoles=Enabled,windowsUpdates=Enabled
注意

此命令不允許庫存收集 Windows 登錄檔或檔案的中繼資料。若要庫存這些資料類型,請使用下一個程序。

在受管節點上手動設定庫存 (CLI)

使用下列程序,透過使用節點IDs或標籤手動設定受管理節點上的 AWS Systems Manager 詳細目錄。

手動設定受管節點以進行詳細目錄 (CLI)
  1. 安裝和配置 AWS Command Line Interface (AWS CLI),如果你還沒有。

    如需相關資訊,請參閱安裝或更新最新版本的 AWS CLI

  2. 執行下列命令,以建立能在節點上執行 Systems Manager 庫存的 State Manager 關聯。替換每個 example resource placeholder 使用您自己的信息。此命令會將該服務設定為每六小時執行一次,並收集來自節點的網路組態、Windows 更新與應用程式中繼資料。

    Linux & macOS
    aws ssm create-association \ --name "AWS-GatherSoftwareInventory" \ --targets "Key=instanceids,Values=an_instance_ID" \ --schedule-expression "rate(240 minutes)" \ --output-location "{ \"S3Location\": { \"OutputS3Region\": \"region_ID, for example us-east-2\", \"OutputS3BucketName\": \"amzn-s3-demo-bucket\", \"OutputS3KeyPrefix\": \"Test\" } }" \ --parameters "networkConfig=Enabled,windowsUpdates=Enabled,applications=Enabled"
    Windows
    aws ssm create-association ^ --name "AWS-GatherSoftwareInventory" ^ --targets "Key=instanceids,Values=an_instance_ID" ^ --schedule-expression "rate(240 minutes)" ^ --output-location "{ \"S3Location\": { \"OutputS3Region\": \"region_ID, for example us-east-2\", \"OutputS3BucketName\": \"amzn-s3-demo-bucket\", \"OutputS3KeyPrefix\": \"Test\" } }" ^ --parameters "networkConfig=Enabled,windowsUpdates=Enabled,applications=Enabled"

    系統會回應相關資訊,如下所示。

    {
        "AssociationDescription": {
            "ScheduleExpression": "rate(240 minutes)",
            "OutputLocation": {
                "S3Location": {
                    "OutputS3KeyPrefix": "Test",
                    "OutputS3BucketName": "Test bucket",
                    "OutputS3Region": "us-east-2"
                }
            },
            "Name": "The name you specified",
            "Parameters": {
                "applications": [
                    "Enabled"
                ],
                "networkConfig": [
                    "Enabled"
                ],
                "windowsUpdates": [
                    "Enabled"
                ]
            },
            "Overview": {
                "Status": "Pending",
                "DetailedStatus": "Creating"
            },
            "AssociationId": "1a2b3c4d5e6f7g-1a2b3c-1a2b3c-1a2b3c-1a2b3c4d5e6f7g",
            "DocumentVersion": "$DEFAULT",
            "LastUpdateAssociationDate": 1480544990.06,
            "Date": 1480544990.06,
            "Targets": [
                {
                    "Values": [
                       "i-02573cafcfEXAMPLE"
                    ],
                    "Key": "InstanceIds"
                }
            ]
        }
    }

    您可以使用帶有標EC2籤的Targets參數來定位大型節點群組。請參閱以下範例。

    Linux & macOS
    aws ssm create-association \ --name "AWS-GatherSoftwareInventory" \ --targets "Key=tag:Environment,Values=Production" \ --schedule-expression "rate(240 minutes)" \ --output-location "{ \"S3Location\": { \"OutputS3Region\": \"us-east-2\", \"OutputS3BucketName\": \"amzn-s3-demo-bucket\", \"OutputS3KeyPrefix\": \"Test\" } }" \ --parameters "networkConfig=Enabled,windowsUpdates=Enabled,applications=Enabled"
    Windows
    aws ssm create-association ^ --name "AWS-GatherSoftwareInventory" ^ --targets "Key=tag:Environment,Values=Production" ^ --schedule-expression "rate(240 minutes)" ^ --output-location "{ \"S3Location\": { \"OutputS3Region\": \"us-east-2\", \"OutputS3BucketName\": \"amzn-s3-demo-bucket\", \"OutputS3KeyPrefix\": \"Test\" } }" ^ --parameters "networkConfig=Enabled,windowsUpdates=Enabled,applications=Enabled"

    您也可以在運算式中使用 fileswindowsRegistry 庫存類型,以庫存 Windows Server 節點上的檔案和 Windows 登錄機碼。如需這些庫存類型的詳細資訊,請參閱使用檔案與 Windows 登錄檔清查

    Linux & macOS
    aws ssm create-association \ --name "AWS-GatherSoftwareInventory" \ --targets "Key=instanceids,Values=i-0704358e3a3da9eb1" \ --schedule-expression "rate(240 minutes)" \ --parameters '{"files":["[{\"Path\": \"C:\\Program Files\", \"Pattern\": [\"*.exe\"], \"Recursive\": true}]"], "windowsRegistry": ["[{\"Path\":\"HKEY_LOCAL_MACHINE\\Software\\Amazon\", \"Recursive\":true}]"]}' \ --profile dev-pdx
    Windows
    aws ssm create-association ^ --name "AWS-GatherSoftwareInventory" ^ --targets "Key=instanceids,Values=i-0704358e3a3da9eb1" ^ --schedule-expression "rate(240 minutes)" ^ --parameters '{"files":["[{\"Path\": \"C:\\Program Files\", \"Pattern\": [\"*.exe\"], \"Recursive\": true}]"], "windowsRegistry": ["[{\"Path\":\"HKEY_LOCAL_MACHINE\\Software\\Amazon\", \"Recursive\":true}]"]}' ^ --profile dev-pdx
  3. 執行下列命令,以檢視關聯狀態。

    aws ssm describe-instance-associations-status --instance-id an_instance_ID

    系統會回應相關資訊,如下所示。

    {
    "InstanceAssociationStatusInfos": [
             {
                "Status": "Pending",
                "DetailedStatus": "Associated",
                "Name": "reInvent2016PolicyDocumentTest",
                "InstanceId": "i-1a2b3c4d5e6f7g",
                "AssociationId": "1a2b3c4d5e6f7g-1a2b3c-1a2b3c-1a2b3c-1a2b3c4d5e6f7g",
                "DocumentVersion": "1"
            }
    ]
    }