Mastering EC2 Agents: SSM and CloudWatch Configuration
Installing and configuring agents on EC2 instances (for example, AWS Systems Manager Agent [SSM Agent], CloudWatch agent)
Mastering EC2 Agents: SSM and CloudWatch Configuration
\nMonitoring and managing a fleet of EC2 instances at scale requires more than just standard hypervisor-level metrics. To achieve full observability and automated management, you must master the installation and configuration of the AWS Systems Manager (SSM) Agent and the Unified CloudWatch Agent.
Learning Objectives
\nBy the end of this guide, you should be able to:
- Distinguish between the primary functions of the SSM Agent and the CloudWatch Agent.
- Execute installation procedures for agents on both Linux and Windows environments.
- Configure the necessary IAM roles and permissions for agent communication with AWS services.
- Implement hybrid activations for on-premises servers using the SSM Agent.
- Collect system-level metrics (e.g., Disk/RAM) and application logs using the Unified CloudWatch Agent.
Key Terms & Glossary
- SSM Agent: Amazon software that processes requests from the Systems Manager service and configures the machine as specified.
- Unified CloudWatch Agent: A single agent capable of collecting internal system metrics and logs from both EC2 instances and on-premises servers.
- StatsD / collectd: Open-source protocols used by the CloudWatch Agent to retrieve custom application-level metrics.
- Managed Node: Any machine (EC2 or on-premises) that has the SSM Agent installed and is configured for Systems Manager.
- Activation Code/ID: Credentials used to register a non-EC2 (hybrid) machine with AWS Systems Manager.
The "Big Idea"
\nAgents represent the "inside-out" perspective of cloud infrastructure. While AWS can see that an instance is "running" from the outside, it cannot see how much RAM is being used or what is written in an application log file without an agent. For a DevOps Engineer, these agents are the bridge between static infrastructure and a dynamic, self-healing environment. The SSM Agent provides the control plane (doing things to the instance), while the CloudWatch Agent provides the data plane (observing what is happening inside).
Formula / Concept Box
| Aspect | CloudWatch Agent | SSM Agent |
|---|---|---|
| Primary Goal | Observability (Metrics & Logs) | Management & Automation |
| Key Dependency | CloudWatchAgentServerPolicy | AmazonSSMManagedInstanceCore |
| Protocols | StatsD, collectd, JSON config | EC2 Messaging, HTTPS |
| Default Status | Must be installed manually | Pre-installed on many AMIs |
Hierarchical Outline
- I. AWS Systems Manager (SSM) Agent
- A. Purpose
- Enables remote management via Run Command and Session Manager.
- Facilitates automated patching and inventory collection.
- B. Installation
- Standard EC2: Pre-installed on Amazon Linux 2, Ubuntu 16+, and Windows Server 2016+.
- Hybrid (On-prem): Requires manual installation and registration using Activation ID/Code.
- C. Security
- Runs as
root(Linux) orSYSTEM(Windows). - Requires IAM Instance Profile permissions.
- Runs as
- A. Purpose
- II. CloudWatch Agent
- A. Capabilities
- Collects Memory, Disk, and Network metrics (at the OS level).
- Streams logs to CloudWatch Logs groups.
- B. Configuration
- Driven by a JSON configuration file (usually created via the
amazon-cloudwatch-agent-config-wizard). - Can be stored in SSM Parameter Store for fleet-wide distribution.
- Driven by a JSON configuration file (usually created via the
- A. Capabilities
Visual Anchors
CloudWatch Agent Data Flow
SSM Agent Communication Path
Definition-Example Pairs
- Custom Metrics: Metrics not available via the EC2 hypervisor, such as used memory percentage.
- Example: Using the CloudWatch Agent to monitor
mem_used_percenton an EC2 instance to trigger an Auto Scaling policy.
- Example: Using the CloudWatch Agent to monitor
- Hybrid Activation: The process of extending AWS management capabilities to non-AWS servers.
- Example: Installing the SSM Agent on a local VMWare virtual machine so it appears in the AWS Fleet Manager console alongside cloud-native instances.
- Maintenance Windows: Scheduled periods for automated tasks.
- Example: Creating a window every Sunday at 02:00 AM to automatically run
AWS-RunPatchBaselineon all web servers using the SSM Agent.
- Example: Creating a window every Sunday at 02:00 AM to automatically run
Worked Examples
Example 1: Manual SSM Agent Installation (Ubuntu)\nWhen working with on-premises servers, you must manually install and register the agent.
# Install the agent via snap\nsudo snap install amazon-ssm-agent --classic
# Stop the service to perform registration\nsudo systemctl stop snap.amazon-ssm-agent.amazon-ssm-agent.service
# Register the agent with credentials from AWS Console\nsudo /snap/amazon-ssm-agent/current/amazon-ssm-agent -register \\
-code \"your-activation-code\" \\
-id \"your-activation-id\" \\
-region \"us-east-1\"
# Restart the service\nsudo systemctl start snap.amazon-ssm-agent.amazon-ssm-agent.serviceExample 2: Deploying CloudWatch Agent via SSM\nA DevOps Engineer should never install the CloudWatch agent manually on 100 servers. Instead, use the SSM Agent to install the CloudWatch Agent.
- Store Configuration: Save your CloudWatch JSON config in SSM Parameter Store (e.g.,
AmazonCloudWatch-linux-config). - Run Command: Use the
AWS-ConfigureAWSPackagedocument to install theAmazonCloudWatchAgent. - Apply Config: Use the
AmazonCloudWatch-ManageAgentdocument to start the agent and point it to the Parameter Store name.
Checkpoint Questions
- Which agent is required to enable AWS Systems Manager Session Manager (shell access)?
- You need to collect
/var/log/nginx/access.logfrom an EC2 instance. Which agent should you use? - What specific IAM permission (policy) is the minimum requirement for an EC2 instance to communicate with the SSM service?
- True or False: The CloudWatch Agent can collect metrics from on-premises Linux servers using
collectd.
Muddy Points & Cross-Refs
- SSM vs. CloudWatch Overlap: Historically, SSM could send some logs to CloudWatch. Today, the CloudWatch Agent is the preferred and unified method for log and metric collection. Use SSM for action (updates, scripts) and CloudWatch for analysis (logs, metrics).
- Permission Pitfall: If an instance doesn't show up in SSM, 90% of the time the IAM Instance Profile is missing the
AmazonSSMManagedInstanceCorepolicy or the instance lacks outbound HTTPS (Port 443) access to the SSM endpoints. - Root Permissions: Be aware that the SSM Agent runs as
root. In high-security environments, this may require a compliance review or restricted IAM policies to prevent unauthorized command execution.
Comparison Tables
Installation Methods
| Method | CloudWatch Agent | SSM Agent |
|---|---|---|
| Command Line | Yes (manual/scripted) | Yes (manual/scripted) |
| Systems Manager | Yes (via Run Command) | N/A (SSM installs CW) |
| CloudFormation | Yes (User Data / cfn-init) | Yes (via User Data) |
| AMI | Rarely pre-installed | Pre-installed on most modern AMIs |
| "word_count": 950, | ||
| "suggested_title": "EC2 Agent Installation and Configuration Guide" | ||
| } |