AWS Learning
Monitoring & Management

AWS Systems Manager (SSM)

SSM Agent, Session Manager, Run Command, Patch Manager chi tiết

AWS Systems Manager là bộ công cụ quản lý infrastructure, giúp quản lý EC2 instances, on-premises servers, và các AWS resources một cách tập trung.

Tổng Quan

┌─────────────────────────────────────────────────────────────────────┐
│                    AWS Systems Manager                              │
│                                                                     │
│  ┌─────────────────────────────────────────────────────────────────┐│
│  │                    Operations Management                        ││
│  │  Explorer │ OpsCenter │ CloudWatch Dashboard │ PHD              ││
│  └─────────────────────────────────────────────────────────────────┘│
│                                                                     │
│  ┌─────────────────────────────────────────────────────────────────┐│
│  │                    Application Management                       ││
│  │  Parameter Store │ AppConfig                                    ││
│  └─────────────────────────────────────────────────────────────────┘│
│                                                                     │
│  ┌─────────────────────────────────────────────────────────────────┐│
│  │                    Change Management                            ││
│  │  Change Manager │ Automation │ Change Calendar │ Maintenance    ││
│  └─────────────────────────────────────────────────────────────────┘│
│                                                                     │
│  ┌─────────────────────────────────────────────────────────────────┐│
│  │                    Node Management                              ││
│  │  Fleet Manager │ Session Manager │ Run Command │ Patch Manager  ││
│  │  State Manager │ Inventory                                      ││
│  └─────────────────────────────────────────────────────────────────┘│
└─────────────────────────────────────────────────────────────────────┘

Các Tính Năng Chính

FeatureMô tảUse Case
Session ManagerSSH/RDP không cần mở portSecure access to instances
Run CommandChạy commands trên nhiều instancesBatch operations
Patch ManagerQuản lý OS patchesSecurity compliance
Parameter StoreLưu trữ config/secretsConfiguration management
State ManagerMaintain desired stateCompliance
AutomationAutomate runbooksOperational tasks

1️⃣ SSM Agent

Khái Niệm

SSM Agent là software chạy trên EC2/on-premises để giao tiếp với Systems Manager service.

┌─────────────────────────────────────────────────────────────────────┐
│                    SSM Agent Architecture                           │
│                                                                     │
│   AWS Cloud                           Your EC2/On-prem              │
│   ┌─────────────┐                    ┌─────────────────────┐        │
│   │ Systems     │ ◄─── polling ───── │ SSM Agent           │        │
│   │ Manager     │    (HTTPS 443)     │ (chạy trên server)  │        │
│   │             │                    │                     │        │
│   │             │ ─── commands ────► │  → Execute commands │        │
│   │             │                    │  → Collect inventory│        │
│   │             │ ◄─── results ───── │  → Report status    │        │
│   └─────────────┘                    └─────────────────────┘        │
│                                                                     │
│   Không cần SSH! Chỉ cần HTTPS outbound (443)                       │
└─────────────────────────────────────────────────────────────────────┘

Pre-installed AMIs

AMI TypeSSM Agent
Amazon Linux 2/2023✅ Pre-installed
Ubuntu 16.04+✅ Pre-installed
Windows Server 2016+✅ Pre-installed
Other AMIs❌ Cần install manually

Requirements

┌─────────────────────────────────────────────────────────────────────┐
│                    SSM Requirements                                 │
├─────────────────────────────────────────────────────────────────────┤
│                                                                     │
│  1. SSM Agent installed and running                                 │
│     └── Pre-installed on modern AMIs                                │
│                                                                     │
│  2. IAM Role với managed policy:                                    │
│     └── AmazonSSMManagedInstanceCore                                │
│                                                                     │
│  3. Network connectivity:                                           │
│     └── HTTPS outbound (443) to SSM endpoints                       │
│     └── Hoặc VPC Endpoints cho private subnets                      │
│                                                                     │
└─────────────────────────────────────────────────────────────────────┘

2️⃣ Session Manager (Deep Dive)

[!IMPORTANT] Session Manager là cách được khuyến nghị để access EC2 instances - thay thế hoàn toàn SSH truyền thống.

Khái Niệm

Session Manager cho phép access EC2 instances mà không cần SSH, không cần bastion host, không cần mở port 22.

┌──────────────────────────────────────────────────────────────────────┐
│                 Traditional SSH vs Session Manager                   │
│                                                                      │
│  Traditional SSH:                                                    │
│  ┌──────────┐   SSH (22)   ┌──────────┐   SSH (22)   ┌──────────┐    │
│  │ User     │────────────► │ Bastion  │────────────► │ Private  │    │
│  │          │              │ Host     │              │ EC2      │    │
│  └──────────┘              └──────────┘              └──────────┘    │
│       ⚠️ Cần quản lý SSH keys, mở ports, maintain bastion            │
│                                                                      │
│  Session Manager:                                                    │
│  ┌──────────┐  HTTPS (443) ┌──────────┐              ┌──────────┐    │
│  │ User     │────────────► │ SSM      │◄─────────────│ SSM Agent│    │
│  │ (Console │              │ Service  │   polling    │ (EC2)    │    │
│  │  or CLI) │              └──────────┘              └──────────┘    │
│       ✅ Không cần SSH keys, không mở port, có audit logs            │
└──────────────────────────────────────────────────────────────────────┘

Kiến Trúc Chi Tiết

┌──────────────────────────────────────────────────────────────────────────┐
│                    SESSION MANAGER ARCHITECTURE                          │
├──────────────────────────────────────────────────────────────────────────┤
│                                                                          │
│  USER                        AWS CLOUD                    EC2 INSTANCE   │
│  ┌─────────┐                                              ┌─────────────┐│
│  │ Browser │──┐                                           │   Private   ││
│  │   or    │  │             ┌─────────────────┐           │   Subnet    ││
│  │  CLI    │  │  HTTPS      │                 │  HTTPS    │ ┌─────────┐ ││
│  └─────────┘  └────────────►│  SSM Service    │◄──────────┤ │SSM Agent│ ││
│                             │                 │ (polling) │ └─────────┘ ││
│  ┌─────────┐                │  - Session      │           │             ││
│  │   IAM   │───authenticate─│  - WebSocket    │           │  No port 22 ││
│  │  User   │                │  - Encryption   │           │  No SSH key ││
│  └─────────┘                └────────┬────────┘           └─────────────┘│
│                                     │                                    │
│                              ┌───────┴───────┐                           │
│                              ▼               ▼                           │
│  ┌─────────┐                                              ┌─────────────┐│
│  │CloudWatch│                                              │    S3      ││
│  │  Logs   │                                              │  Bucket     ││
│  │ (Audit) │                                              │ (Audit)     ││
│  └─────────┘                                              └─────────────┘│
│                                                                          │
└──────────────────────────────────────────────────────────────────────────┘

Lợi Ích So Với SSH

CategoryTraditional SSHSession Manager
Inbound Ports❌ Mở port 22✅ Không cần mở port
SSH Keys❌ Phải quản lý, rotate✅ Không cần SSH keys
Bastion Host❌ Cần maintain, cost✅ Không cần bastion
AuthenticationSSH keys✅ IAM (MFA support)
AuthorizationServer-level✅ IAM policies (granular)
Audit❌ Manual logging✅ CloudWatch/S3 auto-log
Session Recording❌ Không có✅ Full session recording
NetworkCần direct access✅ Qua HTTPS, works anywhere
Cross-platformKhác nhau theo OS✅ Same cho Linux/Windows

Requirements

┌──────────────────────────────────────────────────────────────────────┐
│              SESSION MANAGER REQUIREMENTS CHECKLIST                  │
├──────────────────────────────────────────────────────────────────────┤
│                                                                      │
│  ☑️ 1. SSM AGENT                                                     │
│     • Pre-installed on Amazon Linux 2/2023, Ubuntu 16.04+,           │
│       Windows Server 2016+                                           │
│     • Must be running                                                │
│                                                                      │
│  ☑️ 2. IAM INSTANCE PROFILE                                          │
│     • Attach role với policy: AmazonSSMManagedInstanceCore           │
│     • Hoặc custom policy với ssm:* permissions                       │
│                                                                      │
│  ☑️ 3. NETWORK CONNECTIVITY                                          │
│     Option A: Public subnet với Internet Gateway                     │
│     Option B: Private subnet + NAT Gateway                           │
│     Option C: Private subnet + VPC Endpoints (recommended)           │
│               • ssm.region.amazonaws.com                             │
│               • ssmmessages.region.amazonaws.com                     │
│               • ec2messages.region.amazonaws.com                     │
│                                                                      │
│  ☑️ 4. USER IAM PERMISSIONS                                          │
│     • ssm:StartSession                                               │
│     • ssm:ResumeSession                                              │
│     • ssm:TerminateSession                                           │
│                                                                      │
└──────────────────────────────────────────────────────────────────────┘

VPC Endpoints cho Private Subnet

┌─────────────────────────────────────────────────────────────────────────┐
│           ACCESS PRIVATE EC2 WITHOUT INTERNET                           │
│                                                                         │
│     VPC                                                                 │
│     ┌─────────────────────────────────────────────────────────────────┐ │
│     │                                                                 │ │
│     │  Private Subnet (No Internet)          VPC Endpoints            │ │
│     │  ┌───────────────────┐                ┌───────────────────────┐ │ │
│     │  │                   │                │ com.amazonaws.region. │ │ │
│     │  │  EC2 Instance     │       ◄────────│ ssm                   │ │ │
│     │  │  ┌─────────────┐  │                │ ssmmessages           │ │ │
│     │  │  │ SSM Agent   │──┼────────────────│ ec2messages           │ │ │
│     │  │  └─────────────┘  │                └───────────────────────┘ │ │
│     │  │                   │                         │                │ │
│     │  │  • No NAT needed  │                         │                │ │
│     │  │  • No IGW needed  │                         ▼                │ │
│     │  └───────────────────┘                   AWS PrivateLink        │ │
│     │                                          (to SSM Service)       │ │
│     └─────────────────────────────────────────────────────────────────┘ │
│                                                                         │
└─────────────────────────────────────────────────────────────────────────┘

Cách Sử Dụng

1. Qua AWS Console

EC2 Console → Instances → Select Instance → Connect → Session Manager

2. Qua AWS CLI

# Start session
aws ssm start-session --target i-1234567890abcdef0
 
# Start session với specific user (Linux)
aws ssm start-session \
    --target i-1234567890abcdef0 \
    --document-name AWS-StartInteractiveCommand \
    --parameters command="sudo su - ec2-user"
 
# List active sessions
aws ssm describe-sessions --state Active
 
# Terminate session
aws ssm terminate-session --session-id session-id

3. SSH Proxy qua Session Manager

Có thể dùng SSH commands thông qua Session Manager (để tương thích với tools cần SSH):

# ~/.ssh/config
Host i-* mi-*
    ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"
    User ec2-user
 
# Sau đó dùng SSH bình thường
ssh i-1234567890abcdef0
scp file.txt i-1234567890abcdef0:/home/ec2-user/

Advanced Features

1. Port Forwarding

# Forward local port 9999 → remote port 3306 (MySQL)
aws ssm start-session \
    --target i-1234567890abcdef0 \
    --document-name AWS-StartPortForwardingSession \
    --parameters '{"portNumber":["3306"],"localPortNumber":["9999"]}'
 
# Sau đó connect MySQL client đến localhost:9999
mysql -h 127.0.0.1 -P 9999 -u admin -p
┌─────────────────────────────────────────────────────────────────────┐
│                    PORT FORWARDING                                  │
│                                                                     │
│  Your Laptop              SSM Service              EC2 Instance     │
│  ┌──────────┐            ┌──────────┐            ┌──────────────┐   │
│  │localhost │   HTTPS    │          │   HTTPS    │              │   │
│  │:9999     │◄──────────►│ Session  │◄──────────►│ RDS/MySQL    │   │
│  │          │  WebSocket │ Manager  │  Tunnel    │ :3306        │   │
│  └────┬─────┘            └──────────┘            └──────────────┘   │
│      │                                                              │
│       ▼                                                             │
│  ┌──────────┐                                                       │
│  │ MySQL    │  Connect to localhost:9999                            │
│  │ Client   │  → Tunnel through SSM                                 │
│  └──────────┘  → Reach RDS on :3306                                 │
└─────────────────────────────────────────────────────────────────────┘

2. Port Forwarding to Remote Host

# Forward đến host KHÁC thông qua EC2 (như jump host)
aws ssm start-session \
    --target i-1234567890abcdef0 \
    --document-name AWS-StartPortForwardingSessionToRemoteHost \
    --parameters '{"host":["rds-instance.abc123.us-east-1.rds.amazonaws.com"],"portNumber":["3306"],"localPortNumber":["9999"]}'

3. Session Logging (Audit)

┌─────────────────────────────────────────────────────────────────────┐
│                    SESSION LOGGING OPTIONS                          │
├─────────────────────────────────────────────────────────────────────┤
│                                                                     │
│  Session Manager Preferences (console hoặc CLI):                    │
│                                                                     │
│  ┌───────────────────────────────────────────────────────────────┐  │
│  │ CloudWatch Logs:                                              │  │
│  │   • Log Group: /aws/ssm/sessions                              │  │
│  │   • Stream: session-id                                        │  │
│  │   • Contains: All commands executed                           │  │
│  └───────────────────────────────────────────────────────────────┘  │
│                                                                     │
│  ┌───────────────────────────────────────────────────────────────┐  │
│  │ S3 Bucket:                                                    │  │
│  │   • Bucket: my-ssm-session-logs                               │  │
│  │   • Path: /session-id/session.log                             │  │
│  │   • Encrypt with KMS key                                      │  │
│  └───────────────────────────────────────────────────────────────┘  │
│                                                                     │
│  ┌───────────────────────────────────────────────────────────────┐  │
│  │ KMS Encryption:                                               │  │
│  │   • Encrypt session data in transit                           │  │
│  │   • Encrypt logs at rest                                      │  │
│  └───────────────────────────────────────────────────────────────┘  │
│                                                                     │
└─────────────────────────────────────────────────────────────────────┘

IAM Policy Examples

Basic Session Manager Access

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ssm:StartSession"
            ],
            "Resource": [
                "arn:aws:ec2:*:*:instance/*"
            ],
            "Condition": {
                "StringEquals": {
                    "ssm:resourceTag/Environment": "Development"
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "ssm:TerminateSession",
                "ssm:ResumeSession"
            ],
            "Resource": [
                "arn:aws:ssm:*:*:session/${aws:username}-*"
            ]
        }
    ]
}

Restrict Document Types

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "ssm:StartSession",
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "ssm:SessionDocumentAccessCheck": "true"
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": "ssm:StartSession",
            "Resource": [
                "arn:aws:ssm:*::document/AWS-StartInteractiveCommand",
                "arn:aws:ssm:*::document/AWS-StartPortForwardingSession"
            ]
        }
    ]
}

Session Manager vs SSH Comparison

┌───────────────────────────────────────────────────────────────────────────┐
│                SESSION MANAGER vs SSH - WHEN TO USE?                      │
├───────────────────────────────────────────────────────────────────────────┤
│                                                                           │
│  ✅ USE SESSION MANAGER when:                                             │
│     • AWS EC2 instances                                                   │
│     • Need audit logging                                                  │
│     • Want to eliminate SSH key management                                │
│     • Private subnet access without bastion                               │
│     • Compliance requirements (session recording)                         │
│     • Centralized IAM access control                                      │
│                                                                           │
│  ⚠️ CONSIDER SSH when:                                                    │
│     • Non-AWS servers                                                     │
│     • Need SCP file transfer (workaround: use S3)                         │
│     • Legacy tools requiring SSH                                          │
│     • SSH tunneling for specific protocols                                │
│                                                                           │
│  💡 HYBRID: Use SSH through Session Manager proxy                         │
│     → Get benefits of both!                                               │
│                                                                           │
└───────────────────────────────────────────────────────────────────────────┘

Limitations

LimitationWorkaround
No SCP/SFTPUse S3 + aws s3 cp
Session timeoutDefault 20 mins idle, configurable up to 60 mins
File transferUpload to S3, download with aws cli
No X11 forwardingNot supported
Concurrent sessionsDefault limit, can request increase

Exam Tips

TopicRemember
No port 22Session Manager không cần mở inbound port
No bastionKhông cần bastion host
IAM authSử dụng IAM để authenticate, không phải SSH keys
VPC EndpointsCần cho private subnet không có internet
AuditCloudWatch Logs + S3 cho session logging
Port forwardingCó thể tunnel ports (RDS, etc.)

3️⃣ Run Command

Khái Niệm

Run Command cho phép chạy commands trên nhiều instances cùng lúc mà không cần SSH.

┌─────────────────────────────────────────────────────────────────────┐
│                      Run Command Flow                               │
│                                                                     │
│  ┌──────────────┐                                                   │
│  │ Run Command  │ ──────► Select targets (tags, instance IDs)       │
│  │              │                                                   │
│  │ Document:    │ ──────► Choose document (script to run)           │
│  │ AWS-RunShell │                                                   │
│  │ Script       │ ──────► Execute on all targets                    │
│  └──────────────┘                                                   │
│        │                                                            │
│         ▼                                                           │
│  ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐                │
│  │ EC2 #1   │ │ EC2 #2   │ │ EC2 #3   │ │ EC2 #N   │                │
│  │ (Agent)  │ │ (Agent)  │ │ (Agent)  │ │ (Agent)  │                │
│  └──────────┘ └──────────┘ └──────────┘ └──────────┘                │
└─────────────────────────────────────────────────────────────────────┘

Common Documents

DocumentMô tả
AWS-RunShellScriptRun shell commands (Linux)
AWS-RunPowerShellScriptRun PowerShell (Windows)
AWS-UpdateSSMAgentUpdate SSM Agent
AWS-ConfigureAWSPackageInstall/uninstall packages

Target Selection

Targets có thể specify bằng:

1. Instance IDs:
   --targets "Key=instanceids,Values=i-123,i-456"

2. Tags:
   --targets "Key=tag:Environment,Values=Production"

3. Resource Groups:
   --targets "Key=resource-groups:Name,Values=MyGroup"

4️⃣ Parameter Store

Khái Niệm

Parameter Store là secure, hierarchical storage cho configuration data và secrets.

┌─────────────────────────────────────────────────────────────────────┐
│                    Parameter Store                                  │
│                                                                     │
│  Hierarchy:                                                         │
│  /myapp/                                                            │
│  ├── dev/                                                           │
│  │   ├── db/host          = "dev-db.example.com"                    │
│  │   ├── db/password      = "encrypted-value" (SecureString)        │
│  │   └── api-key          = "dev-key-123"                           │
│  ├── prod/                                                          │
│  │   ├── db/host          = "prod-db.example.com"                   │
│  │   ├── db/password      = "encrypted-value" (SecureString)        │
│  │   └── api-key          = "prod-key-456"                          │
│  └── shared/                                                        │
│      └── config           = "common-config"                         │
└─────────────────────────────────────────────────────────────────────┘

Parameter Types

TypeMô tảEncryptionUse Case
StringPlain textKhôngConfig values
StringListComma-separatedKhôngLists
SecureStringEncryptedKMSPasswords, API keys

Tiers

StandardAdvanced
Max size4 KB8 KB
Max params10,000100,000
PoliciesKhôngCó (expiration, notification)
CostFreeCharged

Sử dụng

# Create parameter
aws ssm put-parameter \
    --name "/myapp/prod/db/password" \
    --value "secret123" \
    --type SecureString
 
# Get parameter
aws ssm get-parameter \
    --name "/myapp/prod/db/password" \
    --with-decryption
 
# Get parameters by path
aws ssm get-parameters-by-path \
    --path "/myapp/prod" \
    --recursive \
    --with-decryption

Integration với các Services

┌─────────────────────────────────────────────────────────────────────┐
│                Parameter Store Integrations                         │
│                                                                     │
│  ┌──────────────┐                                                   │
│  │ Parameter    │                                                   │
│  │ Store        │                                                   │
│  └──────┬───────┘                                                   │
│        │                                                            │
│    ┌────┴────┬──────────┬──────────┬──────────┐                     │
│    ▼         ▼          ▼          ▼          ▼                     │
│ ┌──────┐ ┌──────┐ ┌──────────┐ ┌──────┐ ┌──────────┐                │
│ │Lambda│ │ ECS  │ │CodeBuild │ │ EC2  │ │CloudForm.│                │
│ └──────┘ └──────┘ └──────────┘ └──────┘ └──────────┘                │
└─────────────────────────────────────────────────────────────────────┘

Parameter Store vs Secrets Manager

Parameter StoreSecrets Manager
CostFree (Standard tier)$0.40/secret/month
RotationKhông có built-inCó automatic rotation
Cross-accountKhông
Max size8 KB64 KB
Use caseConfig, simple secretsDatabase credentials, API keys với rotation

[!TIP] Dùng Parameter Store cho configs và secrets đơn giản. Dùng Secrets Manager khi cần automatic rotation (ví dụ: RDS passwords).


5️⃣ Patch Manager

Khái Niệm

Patch Manager tự động hóa việc patching OS và applications.

┌─────────────────────────────────────────────────────────────────────┐
│                    Patch Manager Flow                               │
│                                                                     │
│  1. Define Patch Baseline                                           │
│     └── Rules cho approved/rejected patches                         │
│                                                                     │
│  2. Define Patch Groups                                             │
│     └── Group instances bằng tags                                   │
│                                                                     │
│  3. Define Maintenance Window                                       │
│     └── Schedule khi nào patch                                      │
│                                                                     │
│  4. Run Patching                                                    │
│     └── Scan → Install → Reboot (if needed)                         │
│                                                                     │
└─────────────────────────────────────────────────────────────────────┘

Patch Baseline

ComponentMô tả
Operating SystemLinux, Windows, macOS
Approval RulesAuto-approve patches after X days
Approved PatchesExplicitly approved list
Rejected PatchesExplicitly rejected list

6️⃣ Automation

Khái Niệm

Automation cho phép tạo runbooks để automate common maintenance tasks.

Common Use Cases

Use CaseDocument
Restart instanceAWS-RestartEC2Instance
Create AMIAWS-CreateImage
Update CloudFormationAWS-UpdateCloudFormationStack
RemediateCustom remediation runbooks

EventBridge Integration

┌─────────────────────────────────────────────────────────────────────┐
│                  Auto-Remediation Pattern                           │
│                                                                     │
│  ┌──────────────┐    ┌──────────────┐    ┌──────────────┐           │
│  │ AWS Config   │───►│ EventBridge  │───►│ SSM          │           │
│  │ (Non-        │    │ Rule         │    │ Automation   │           │
│  │  compliant)  │    │              │    │ (Remediate)  │           │
│  └──────────────┘    └──────────────┘    └──────────────┘           │
│                                                                     │
│  Example: Security Group mở port 22 → Tự động close port            │
└─────────────────────────────────────────────────────────────────────┘

📋 Exam Tips

TopicRemember
SSM AgentPre-installed on modern AMIs, cần IAM Role
Session ManagerNo SSH, no port 22, uses IAM
Run CommandExecute commands on multiple instances
Parameter StoreFree tier, SecureString encrypted by KMS
Secrets ManagerCó rotation, cross-account, costs money
Patch ManagerBaselines, Patch Groups, Maintenance Windows

Common Scenarios

1. "Access EC2 trong private subnet không có SSH"
   → Session Manager + VPC Endpoints

2. "Lưu database password an toàn"
   → Parameter Store (SecureString) hoặc Secrets Manager

3. "Chạy script trên 100 EC2 instances"
   → Run Command

4. "Auto-patch OS hàng tuần"
   → Patch Manager + Maintenance Windows

5. "Auto-remediate security issues"
   → AWS Config + EventBridge + SSM Automation

🔗 Related Resources

  • EC2 - Compute instances
  • IAM - Identity and Access Management
  • VPC - Networking
  • CloudFormation - Infrastructure as Code