Example:
EC2-Logs, and within it, have separate log streams for each instance like i-12345678 and i-87654321.Use amazon-cloudwatch-agent for EC2 Logging (the newer solution)
Install the CloudWatch Agent:
Amazon Linux:
sudo yum install -y amazon-cloudwatch-agent
Ubuntu:
sudo apt-get update
sudo apt-get install -y amazon-cloudwatch-agent
Create the Configuration File:
Define your logging and metrics configuration in amazon-cloudwatch-agent.json (usually placed in /opt/aws/amazon-cloudwatch-agent/bin).
Example configuration:
{
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/syslog",
"log_group_name": "MyAppLogGroup",
"log_stream_name": "{instance_id}/syslog"
},
{
"file_path": "/var/log/app.log",
"log_group_name": "MyAppLogGroup",
"log_stream_name": "{instance_id}/app"
}
]
}
}
}
}
Start the CloudWatch Agent:
Apply the configuration and start the agent:
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \\
-a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent.json -s
Verify in CloudWatch Console:
MyAppLogGroup and ensure logs are streaming from EC2.Lambda functions automatically send logs to CloudWatch, which you can view in the Lambda console.