Scope:
- Intro,
- The General Setup steps,
- Link to official documentation,
- Key Components,
- Data Flow,
- Sample Agent Configuration (Linux),
- Use Cases,
- Reference Architectures,
- Bottom line.
Intro:
- To monitor twtech EC2 instance using Amazon CloudWatch Logs, it needs to set up an agent on the instance to stream log data to the CloudWatch service.
- EC2 instances generate system logs, application logs, and OS-level logs.
- By default, EC2 instance logs live inside the instance (e.g., /var/log/messages, /var/log/syslog, /var/log/httpd/).
- CloudWatch Logs provides a way to centralize, retain, analyze, and stream logs without manually SSH’ing into instances.
- Create an IAM Role: Create an AWS Identity and Access Management (IAM) role that grants permissions for EC2 instances to write logs to CloudWatch Logs. The required policy is
CloudWatchLogsFullAccessor a custom policy with actions likelogs:CreateLogGroup,logs:CreateLogStream, andlogs:PutLogEvents. - Attach the Role to Instance: Associate the newly created IAM role with twtech specific EC2 instance.
- Install the CloudWatch Agent: Connect (SSH) to twtech EC2 instance and install the unified Amazon CloudWatch agent. The agent is available for both Linux and Windows instances. https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/QuickStartEC2Instance.html
- Configure the Agent: Create a configuration file (typically in JSON format) that specifies which log files, metrics, and log groups should be monitored and where they should be sent within CloudWatch Logs.
- Start the Agent: Start the CloudWatch agent using the configuration file you created.
- Verify Logs in CloudWatch: Navigate to the Amazon CloudWatch console in twtech AWS Management Console under "Logs" > "Log groups" to view the incoming log streams from your EC2 instance.
- For detailed instructions and to download the necessary agent software, refer to the official Amazon CloudWatch documentation.
- Or review a specific EC2 setup guide provided by AWS.
Key Components
- CloudWatch Agent
- Installed on EC2 to push logs/metrics.
- Replaces the older CloudWatch Logs Agent.
- Supports both logs + metrics in one unified agent.
- Log Groups & Streams
- Each application/system log file → mapped to a log
group.
- Each instance → creates its own log stream
within the group.
- Example:
- Log Group: /EC2/twtchAppLogs
- Log Stream: i-1234567890abcdef/httpd-access.log
- IAM Role / Instance Profile
- EC2 needs permissions (logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents) to send logs.
- CloudWatch Logs Agent Config
- JSON file defining:
- Which log files to collect.
- Log group and stream names.
- Retention policy.
Data
Flow
- EC2 generates logs
(OS, system, application).
- CloudWatch Agent
tails configured log files.
- Agent batches log events → sends to CloudWatch Logs
service.
- Logs stored in Log Groups/Streams.
- Optional:
- CloudWatch Logs Insights → search/query logs.
- Subscriptions
→ stream logs to Lambda, Kinesis, Firehose.
- Metric Filters
→ turn log patterns into CloudWatch metrics (e.g., count errors).
- Alarms → trigger on those metrics.
# twtech-Sample Agent Configuration (Linux)
{
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/messages",
"log_group_name": "/EC2/twtechSystemLogs",
"log_stream_name": "{twtechinstance_id}"
},
{
"file_path": "/var/log/httpd/access.log",
"log_group_name": "/EC2/HttpdAccess",
"log_stream_name": "{twtechinstance_id}"
}
]
}
}
}
}
Use Cases
- Centralized Logging:
Collect logs from all EC2s into one place.
- Security & Compliance: Forward logs to SIEM (Security Information & Event Management) or S3 (via Firehose).
- Troubleshooting: Use CloudWatch Logs Insights to query across multiple servers.
- Monitoring: Use metric filters to detect repeated errors, failed logins, or service crashes.
Reference Architecture
(High-Level)
EC2 Instance (generates logs) → CloudWatch Agent (sends logs) → CloudWatch Log Groups (Logs Insights / Subscriptions / Metric Filters) → monitoring,Observability analytics Tools → Streaming Kinesis Data Streams → Kinesis Firehose (Optional) → Data-lake / S3 Storage → Redshift / OpenSearch (for centralized analytics)
No comments:
Post a Comment