概要

マネージドノード(EC2インスタンス、オンプレミスサーバー)を定義した状態に自動的に維持する構成管理サービス

出典: [AWS Systems Manager State Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems- manager-state.html)

State Manager, a tool in AWS Systems Manager, is a secure and scalable configuration management service that automates the process of keeping your managed nodes and other AWS resources in a state that you define.

具体的ユースケース

ノード管理

  • 起動時に特定のソフトウェアをインストール
  • Linux/macOS/Windowsでのスクリプト実行
  • SSM Agentや他のAWSパッケージの定期更新
  • Microsoft Active Directoryドメインへの参加

AWSリソース管理(Automationと組み合わせ)

  • EC2インスタンスへのSystems Managerロールのアタッチ
  • セキュリティグループのインバウンド/アウトバウンドルールの強制
  • DynamoDBバックアップの作成/削除
  • EBSスナップショットの作成/削除
  • S3バケットの読み書き権限の制御
  • EC2/RDSインスタンスの起動/再起動/停止
  • AMIへのパッチ適用

関連用語

Association(アソシエーション)

AWSリソースに割り当てる構成のこと。

維持したい状態、ターゲット、スケジュールを定義する

出典: Understanding how State Manager works

A State Manager association is a configuration that you assign to your AWS resources. The configuration defines the state that you want to maintain on your resources.

動作の定義方法

CFn上での指定方法を例に見ていく

何をするか: SSM ドキュメントで指定

Name: AWS-RunShellScript  # AWS提供のシェルスクリプト実行ドキュメント
Parameters:
  commands:
    - dnf install -y httpd
    - systemctl start httpd

よく使う AWS 提供ドキュメント:

ドキュメント名用途
AWS-RunShellScriptLinux でシェルスクリプト実行
AWS-RunPowerShellScriptWindows で PowerShell 実行
AWS-UpdateSSMAgentSSM Agent の更新
AWS-ApplyPatchBaselineパッチ適用
AWS-ConfigureAWSPackageDistributor パッケージのインストール

どこに適用するか: Targets で指定

# タグで指定
Targets:
  - Key: tag:Environment
    Values:
      - production
      - staging

# インスタンスIDで指定
Targets:
  - Key: InstanceIds
    Values:
      - i-1234567890abcdef0

# Resource Group で指定
Targets:
  - Key: resource-groups:Name
    Values:
      - my-resource-group

いつ実行するか: ScheduleExpression で指定

# rate式(最小30分)
ScheduleExpression: rate(30 minutes)
ScheduleExpression: rate(1 hour)
ScheduleExpression: rate(1 day)
 
# cron式
ScheduleExpression: cron(0 2 ? * SUN *)   # 毎週日曜 2:00 UTC
ScheduleExpression: cron(0 0/4 * * ? *)   # 4時間ごと

Configuration Drift(構成ドリフト)

ノードの実際の状態が、定義された望ましい状態から逸脱すること

State Managerはこれを検出し、自動修復する

使用するSSMコンポーネント

┌─────────────────────────────────────────────────────────────┐
│                    State Manager                             │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│  ┌──────────────┐    ┌──────────────┐    ┌──────────────┐  │
│  │ Association  │───▶│ SSM Document │───▶│   Targets    │  │
│  │  (構成定義)   │    │  (実行内容)   │    │ (適用対象)   │  │
│  └──────────────┘    └──────────────┘    └──────────────┘  │
│         │                   │                    │          │
│         ▼                   ▼                    ▼          │
│  ┌──────────────┐    ┌──────────────┐    ┌──────────────┐  │
│  │  Schedule    │    │  Run Command │    │  SSM Agent   │  │
│  │ (実行間隔)   │    │  Automation  │    │ (ノード上)   │  │
│  └──────────────┘    └──────────────┘    └──────────────┘  │
│                                                              │
└─────────────────────────────────────────────────────────────┘

料金

State Managerは追加料金なしで利用可能

出典: AWS Systems Manager State Manager

State Manager is available at no additional charge.

よく使われるSSMドキュメント

ドキュメント名用途
AWS-UpdateSSMAgentSSM Agentの更新
AWS-ConfigureAWSPackageAWSパッケージのインストール/更新
AWS-RunShellScriptLinuxでシェルスクリプト実行
AWS-RunPowerShellScriptWindowsでPowerShell実行
AWS-ApplyAnsiblePlaybooksAnsible Playbookの実行
AWS-ApplyChefRecipesChef Recipeの実行
AWS-JoinDirectoryServiceDomainADドメインへの参加

試してみる