AWS Systems Manager State Manager
Associations, Desired State, Targeting, Scheduling, Automation Runbooks, Configuration Drift
Tổng quan
State Manager là tool trong AWS Systems Manager giúp tự động duy trì trạng thái mong muốn (desired state) trên managed nodes và các AWS resources khác.
[!IMPORTANT] State Manager MIỄN PHÍ hoàn toàn. Không tính phí cho associations hay executions.
Khái niệm cốt lõi — Association
Association là đơn vị cấu hình chính của State Manager — nó định nghĩa:
| Thành phần | Vai trò | Ví dụ |
|---|---|---|
| SSM Document | Hành động cần thực hiện | AWS-RunShellScript, AWS-ConfigureAWSPackage |
| Parameters | Tham số cho document | Tên package, commands cần chạy |
| Targets | Nodes/resources áp dụng | Tag Environment=Production, tất cả nodes |
| Schedule | Khi nào chạy | rate(1 day), cron(0 2 ? * SUN *) |
| Rate Controls | Kiểm soát tốc độ deploy | Concurrency: 10, Error Threshold: 5% |
Cách State Manager hoạt động
Khi nào Association tự động chạy lại?
| Trigger | Mô tả |
|---|---|
| Tạo mới | Chạy ngay lập tức (mặc định) |
| Theo schedule | Theo cron/rate đã định |
| Edit association | Khi thay đổi document version, parameters, schedule, output location |
| Edit document | Khi document $DEFAULT/$LATEST version thay đổi |
| Node online lần đầu | Node mới match target → apply ngay |
| Node reconnect | Node offline rồi online lại (miss scheduled run) |
| Node trở lại sau 30+ ngày | Node stopped > 30 ngày rồi start lại |
| Manual | User chạy thủ công từ console/CLI |
[!NOTE] Có thể tắt auto-run khi target thay đổi bằng option Apply association only at the next specified Cron interval (
ApplyOnlyAtCronInterval=true). Hữu ích khi dùng Automation runbooks để tránh phát sinh chi phí khi launch nhiều instances.
SSM Documents dùng với State Manager
State Manager hỗ trợ 3 loại documents:
| Document Type | Mô tả | Ví dụ |
|---|---|---|
| Command | Chạy commands trên nodes | AWS-RunShellScript, AWS-RunPowerShellScript |
| Policy | Apply cấu hình liên tục | AWS-GatherSoftwareInventory |
| Automation | Chạy runbooks phức tạp | AWS-RestartEC2Instance, AWS-CreateSnapshot |
Documents phổ biến cho State Manager
| Document | Mục đích |
|---|---|
AWS-ConfigureAWSPackage | Cài/gỡ/update AWS packages (CloudWatch Agent, v.v.) |
AWS-RunShellScript | Chạy shell script trên Linux |
AWS-RunPowerShellScript | Chạy PowerShell trên Windows |
AWS-UpdateSSMAgent | Tự động update SSM Agent |
AWS-ApplyDSCMofs | Apply PowerShell DSC configurations |
AWS-JoinDirectoryServiceDomain | Join Windows vào Active Directory domain |
AWS-GatherSoftwareInventory | Thu thập inventory data |
AWS-ApplyAnsiblePlaybooks | Chạy Ansible playbooks |
AWS-ApplyChefRecipes | Chạy Chef recipes |
Targeting — Chọn nodes mục tiêu
[!TIP] Luôn ưu tiên dùng Tags để target, đặc biệt khi dùng Command/Policy documents và Auto Scaling Groups. Nodes mới launch với đúng tags sẽ tự động nhận association.
Scheduling — Lên lịch chạy
Cron và Rate Expressions
| Loại | Ví dụ | Ý nghĩa |
|---|---|---|
| Rate | rate(1 day) | Mỗi ngày 1 lần |
| Rate | rate(12 hours) | Mỗi 12 giờ |
| Cron | cron(0 2 ? * SUN *) | Chủ nhật lúc 2AM UTC |
| Cron | cron(30 23 ? * TUE#3 *) | Thứ 3 tuần thứ 3 mỗi tháng, 23:30 UTC |
| Cron | cron(0 0 ? * 3L *) | Thứ 3 cuối cùng mỗi tháng, 0:00 UTC |
Schedule Offset
Cho phép delay thêm N ngày sau ngày scheduled. Ví dụ:
[!NOTE] State Manager không hỗ trợ chỉ định months trong cron expressions cho associations.
Rate Controls — Kiểm soát tốc độ
State Manager + Automation Runbooks
State Manager không chỉ chạy commands trên nodes, mà còn schedule Automation runbooks để quản lý AWS resources rộng hơn:
| Hành động | Ví dụ Runbook |
|---|---|
| Attach IAM role cho EC2 | Custom runbook |
| Enforce Security Group rules | Custom runbook |
| Tạo/xóa DynamoDB backups | AWS-CreateDynamoDBBackup |
| Tạo/xóa EBS snapshots | AWS-CreateSnapshot |
| Tắt S3 public access | Custom runbook |
| Start/Stop EC2 theo giờ | AWS-StartEC2Instance, AWS-StopEC2Instance |
| Patch AMIs | AWS-UpdateLinuxAmi |
[!WARNING] Khi dùng Automation runbooks target "all instances" và launch nhiều instances thường xuyên, runbook sẽ chạy trên mỗi instance mới → có thể phát sinh chi phí Automation cao. Dùng
ApplyOnlyAtCronInterval=trueđể tránh.
Giới hạn: Tối đa 100 concurrent automations running trong 1 AWS account.
Compliance Reporting
State Manager tích hợp với SSM Compliance để báo cáo trạng thái tuân thủ:
| Status | Ý nghĩa |
|---|---|
| Compliant | Association chạy thành công, node ở desired state |
| Non-Compliant | Association fail hoặc node không ở desired state |
- Có thể gán Compliance Severity (Critical, High, Medium, Low, Unspecified)
- Kết hợp với Change Calendar để kiểm soát khi nào association được phép chạy
- Output có thể lưu vào S3 bucket để audit
Use Cases phổ biến
1. Bootstrap instances khi launch
2. Tự động update SSM Agent
3. Join Windows vào Active Directory
4. Enforce security configs
5. Stop/Start EC2 ngoài giờ (tiết kiệm chi phí)
So sánh State Manager vs các công cụ khác
State Manager vs Maintenance Windows
| Tiêu chí | State Manager | Maintenance Windows |
|---|---|---|
| Mục đích | Duy trì desired state liên tục | Chạy tasks trong khung giờ cho phép |
| Focus | Compliance & configuration | Time-sensitive operations |
| Scheduling | Cron/rate, chạy khi target thay đổi | Chỉ trong maintenance window |
| Auto-apply cho nodes mới | ✅ Có | ❌ Không |
| Use case | "EC2 phải luôn cài antivirus" | "Patch OS vào Chủ nhật 2AM" |
State Manager vs các công cụ ngoài AWS
| Tiêu chí | State Manager | Ansible | Puppet/Chef |
|---|---|---|---|
| Chi phí | FREE | Free (community) / Paid | Paid |
| Agent | SSM Agent (có sẵn) | Agentless (SSH) | Agent required |
| Setup | Minimal | Cần setup | Cần master server |
| AWS integration | Native | Qua modules | Qua modules |
| Scope | AWS resources | Any server | Any server |
| Learning curve | Thấp | Trung bình | Cao |
State Manager vs AWS Config
| Tiêu chí | State Manager | AWS Config |
|---|---|---|
| Vai trò | Enforce desired state | Detect configuration changes |
| Hành động | Apply configs, fix drift | Record & evaluate compliance |
| Remediation | Trực tiếp qua associations | Gián tiếp qua SSM Automation |
[!TIP] Kết hợp cả hai: AWS Config phát hiện drift → trigger SSM Automation → State Manager enforce lại desired state.
Pricing
| Component | Chi phí |
|---|---|
| State Manager | FREE |
| Associations | FREE |
| Association executions | FREE |
| SSM Agent | FREE |
[!NOTE] State Manager hoàn toàn miễn phí. Tuy nhiên, nếu dùng Automation runbooks qua State Manager, phí Automation vẫn áp dụng (free 100,000 steps/tháng, sau đó tính phí).
Best Practices
- Dùng Tags để target — Tự động apply cho nodes mới, dễ quản lý
- Set Rate Controls — Tránh impact toàn bộ fleet cùng lúc
- Dùng
ApplyOnlyAtCronIntervalcho Automation runbooks — Tránh chi phí bất ngờ - Kết hợp Change Calendar — Ngăn association chạy trong freeze periods
- Lưu output vào S3 — Giữ audit trail cho compliance
- Tách associations theo environment — Dev/Staging/Prod dùng tags khác nhau
- Monitor Compliance dashboard — Phát hiện non-compliant nodes sớm
- Custom IAM role cho AssociationDispatchAssumeRole — Kiểm soát permissions chặt chẽ