🐥

AWS SSM Document 個人メモ

2024/12/31に公開

マネージドノード

SSMの機能を利用可能にしたEC2

SSM Agent がインストールされていること (Amazon Linuxはデフォルトでインストール済み)
SSMサービスエンドポイント への通信が可能なこと (Internet GatewayまたはVPC Endpoint)
IAMロール (AmazonSSMManagedInstanceCore) が付与されていること

Run Command

マネージドノードのEC2に対して、Document (処理)を実行可能
AWS-RunShellScript を使用すれば、複数のEC2(Linux)に対して、複数のコマンドを実行可能

Document名 説明
AWS-RunShellScript ShellScriptを実行 (Linux)
AWS-RunPowerShellScript PowerShellScriptを実行 (Windows)
AWS-RunPatchBaseline パッチ適用
AWS-InstallWindowsUpdates (特定のKBの) Windows Updateを適用
AWS-UpdateSSMAgent SSM Agentを更新
AWS-ConfigureAWSPackage CloudWatch Agent等のインストール/更新
AWS-GatherSoftwareInventory SSM Inventory用にOS情報を取得

Maintenance Windows と State Manager Association (関連付け) の違い

どちらも Document を定期的に実行可能にする機能
EC2のOSやAgentを常に最新のバージョンに保ちたい場合は、State Manager Association の方がよい

項目 Maintenance Windows State Manager Association
実行タイミング ・スケジュール(Cron/Rate) ・スケジュール(Cron/Rate)
・EC2作成時
・30日以上停止していたEC2が起動した場合
実行対象 ・Document
・Automation
・Lambda
・Step Functions
・Document

カスタムのDocumentを作成する例

複数のDocumentを実行したり、ShellScriptを実行したりする場合は、カスタムのDocumentを作成するとよい
AWS-UpdateSSMAgent は実行できないため注意

https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/documents-command-ssm-plugin-reference.html#aws-rundocument

以下を実行するDocumentを作成する例

・SSM Agentの最新バージョンをインストール
・CloudWatch Agentの最新バージョンをインストール
・ユーザーを作成

{
  "schemaVersion": "2.2",
  "description": "Update SSM Agent and CloudWatch Agent, Create Linux User",
  "parameters": {
    "version": {
      "default": "",
      "description": "(Optional) A specific version of the Amazon SSM Agent to install. If not specified, the agent will be updated to the latest version.",
      "type": "String"
    },
    "allowDowngrade": {
      "default": "false",
      "description": "(Optional) Allow the Amazon SSM Agent service to be downgraded to an earlier version. If set to false, the service can be upgraded to newer versions only (default). If set to true, specify the earlier version.",
      "type": "String",
      "allowedValues": [
        "true",
        "false"
      ]
    },
    "username": {
      "type": "String",
      "description": "(Required) The name of the user to create."
    },
    "password": {
      "type": "String",
      "description": "(Required) The password for the user."
    }
  },
  "mainSteps": [
    {
      "action": "aws:updateSsmAgent",
      "name": "awsupdateSsmAgent",
      "inputs": {
        "agentName": "amazon-ssm-agent",
        "source": "https://s3.{Region}.amazonaws.com/amazon-ssm-{Region}/ssm-agent-manifest.json",
        "allowDowngrade": "{{ allowDowngrade }}",
        "targetVersion": "{{ version }}"
      }
    },
    {
      "action": "aws:runDocument",
      "name": "UpdateCloudWatchAgent",
      "inputs": {
        "documentType": "SSMDocument",
        "documentPath": "AWS-ConfigureAWSPackage",
        "documentParameters": {
          "action": "Install",
          "installationType": "Uninstall and reinstall",
          "name": "AmazonCloudWatchAgent",
          "version": "latest"
        }
      }
    },
    {
      "action": "aws:runShellScript",
      "name": "runShellScript",
      "precondition": {
        "StringEquals": [
          "platformType",
          "Linux"
        ]
      },
      "inputs": {
        "timeoutSeconds": "60",
        "runCommand": [
          "#!/bin/bash",
          "USERNAME={{ username }}",
          "PASSWORD={{ password }}",
          "if ! id \"$USERNAME\" &>/dev/null; then",
          "  sudo useradd $USERNAME",
          "  echo \"$USERNAME:$PASSWORD\" | sudo chpasswd",
          "  echo \"User $USERNAME has been created\"",
          "else",
          "  echo \"User $USERNAME already exists\"",
          "fi"
        ]
      }
    }
  ]
}

Inventory から、パッチ適用やOSユーザを確認する例


GitHubで編集を提案

Discussion