建立 SSM 文件內容 - AWS Systems Manager

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

建立 SSM 文件內容

如果 AWS Systems Manager 公用文件未執行您要對 AWS 資源執行的所有動作,您可以建立自己的 SSM 文件。您也可以使用主控台複製 SSM 文件。複製文件會將現有文件的內容複製到您可以修改的新文件中。建立或複製文件時,文件的內容不得超過 64 KB。此配額也包括在執行階段為輸入參數指定的內容。當您建立新的 CommandPolicy 文件時,我們建議您使用結構描述 2.2 或更新版本,以便您可以利用最新的功能,例如文件編輯、自動版本控制、排序等。

撰寫 SSM 文件內容

若要建立您自己的 SSM 文件內容,請務必瞭解 SSM 文件提供的不同結構描述、功能、外掛程式和語法。我們建議您熟悉下列資源。

AWS 預先定義的 SSM 文件可能會執行您需要的某些動作。您可以根據文件類型,在自訂 SSM 文件中使用 aws:runDocumentaws:runCommandaws:executeAutomation 外掛程式來呼叫這些文件。您也可以將這些文件的部分複製到自訂 SSM 文件中,然後編輯內容以符合您的需求。

提示

建立 SSM 文件內容時,您可能會在測試時數次變更內容並更新 SSM 文件。下列命令會以您的最新內容更新 SSM 文件,並將文件的預設版本更新為文件的最新版本。

注意

Linux 和 Windows 命令使用 jq 命令列工具來篩選 JSON 回應資料。

Linux & macOS
latestDocVersion=$(aws ssm update-document \ --content file://path/to/file/documentContent.json \ --name "ExampleDocument" \ --document-format JSON \ --document-version '$LATEST' \ | jq -r '.DocumentDescription.LatestVersion') aws ssm update-document-default-version \ --name "ExampleDocument" \ --document-version $latestDocVersion
Windows
latestDocVersion=$(aws ssm update-document ^ --content file://C:\path\to\file\documentContent.json ^ --name "ExampleDocument" ^ --document-format JSON ^ --document-version "$LATEST" ^ | jq -r '.DocumentDescription.LatestVersion') aws ssm update-document-default-version ^ --name "ExampleDocument" ^ --document-version $latestDocVersion
PowerShell
$content = Get-Content -Path "C:\path\to\file\documentContent.json" | Out-String $latestDocVersion = Update-SSMDocument ` -Content $content ` -Name "ExampleDocument" ` -DocumentFormat "JSON" ` -DocumentVersion '$LATEST' ` | Select-Object -ExpandProperty LatestVersion Update-SSMDocumentDefaultVersion ` -Name "ExampleDocument" ` -DocumentVersion $latestDocVersion

複製 SSM 文件

您可以使用 Systems Manager AWS Systems Manager 文件主控台複製文件,以建立 SSM 文件。複製 SSM 文件會將現有文件的內容複製到您可以修改的新文件中。您無法複製大於 64 KB 的文件。

若要複製 SSM 文件
  1. 開啟主 AWS Systems Manager 控台,網址為 https://console.aws.amazon.com/systems-manager/

  2. 在導覽窗格中,選擇 Documents (文件)

    -或-

    如果 AWS Systems Manager 首頁首頁開啟,請選擇功能表圖示 ( 
    The menu icon
  ) 以開啟導覽窗格,然後在導覽窗格中選擇 [文件]。

  3. 在搜尋方塊中,輸入您要複製的文件的名稱。

  4. 選擇您要翻製的文件名稱,然後選擇 Actions (動作) 下拉式選單中的 Clone document (複製文件)。

  5. 視需要修改文件,然後選擇 Create document (建立文件) 以儲存文件。

撰寫 SSM 文件內容之後,您可以使用下列其中一種方法來建立 SSM 文件。

建立 SSM 文件

建立複合文件

合 AWS Systems Manager (SSM) 文件是透過執行一或多個次要 SSM 文件來執行一系列動作的自訂文件。複合文件提升了 infrastructure as code,讓您能夠為常見任務建立一組標準的 SSM 文件,例如自舉軟體或網域加入執行個體。然後,您可以共用這些文件, AWS 區域 以減少 SSM 文件維護並確保一致性。 AWS 帳戶

例如,您可以建立複合的文件來執行下列動作:

  1. 安裝允許清單中的所有修補程式。

  2. 安裝防毒軟體

  3. 從下載腳本GitHub並運行它們。

在這個範例中,自訂 SSM 文件中包含下列外掛程式以執行下列動作:

  1. 用於執行 AWS-RunPatchBaseline 文件的 aws:runDocument 外掛程式,可安裝所有允許列出的修補程式。

  2. 用於執行 AWS-InstallApplication 文件的 aws:runDocument 外掛程式,可安裝防毒軟體。

  3. 從中下載腳本GitHub並運行它們的aws:downloadContent插件。

複合文件和次要文件可以存放在 Systems Manager GitHub (公有和私有儲存庫) 或 Amazon S3 中。您可以用 JSON 或 YAML 格式建立複合文件和次要文件。

注意

複合文件的執行深度最多只能為三個文件。這表示複合文件可以呼叫一個子文件,以及該子文件可以再呼叫一個文件。

若要建立複合文件,需要在自訂 SSM 文件中新增 aws:runDocument 外掛程式,並指定所需的輸入。下列的範例是個建立複合的文件來執行下列動作:

  1. 執行aws:downloadContent外掛程式,將 SSM 文件從GitHub公用存放庫下載到稱為啟動程序的本機目錄。SSM 文件稱為 StateManagerBootstrap .yml (YAML 文件)。

  2. 執行aws:runDocument外掛程式以執行 StateManagerBootstrap .yml 文件。無需指定參數。

  3. 執行 aws:runDocument 外掛程式,以執行 AWS-ConfigureDocker pre-defined SSM 文件。指定的參數在執行個體上安裝 Docker。

{ "schemaVersion": "2.2", "description": "My composite document for bootstrapping software and installing Docker.", "parameters": { }, "mainSteps": [ { "action": "aws:downloadContent", "name": "downloadContent", "inputs": { "sourceType": "GitHub", "sourceInfo": "{\"owner\":\"TestUser1\",\"repository\":\"TestPublic\", \"path\":\"documents/bootstrap/StateManagerBootstrap.yml\"}", "destinationPath": "bootstrap" } }, { "action": "aws:runDocument", "name": "runDocument", "inputs": { "documentType": "LocalPath", "documentPath": "bootstrap", "documentParameters": "{}" } }, { "action": "aws:runDocument", "name": "configureDocker", "inputs": { "documentType": "SSMDocument", "documentPath": "AWS-ConfigureDocker", "documentParameters": "{\"action\":\"Install\"}" } } ] }
詳細資訊