BrainyBeeBrainyBee
ExploreBlogStart Studying
HomeAWS Certified DevOps Engineer - Professional (DOP-C02)AWS DevOps Pro Lab: Advanced Log and Metric Aggregation
Hands-On Lab945 words

AWS DevOps Pro Lab: Advanced Log and Metric Aggregation

Configure the collection, aggregation, and storage of logs and metrics.

AWS DevOps Pro Lab: Advanced Log and Metric Aggregation

This hands-on lab guides you through configuring the collection, aggregation, and lifecycle management of logs and metrics on AWS, a core competency for the AWS Certified DevOps Engineer - Professional exam. You will deploy a CloudWatch Agent, create metric filters, and automate log archival to S3.

[!WARNING] Remember to run the teardown commands at the end of this lab to avoid ongoing charges for EC2 instances and CloudWatch storage.

Prerequisites

  • An AWS Account with Administrator access.
  • AWS CLI installed and configured with credentials.
  • A default VPC in your region with at least one public subnet.
  • Basic familiarity with Linux command-line operations.

Learning Objectives

  • Deploy and configure the CloudWatch Agent on an EC2 instance to collect custom system-level metrics.
  • Create CloudWatch Metric Filters to extract actionable data from unstructured log streams.
  • Implement log storage lifecycles using CloudWatch retention policies and Amazon S3 Lifecycle rules.
  • Configure secure log ingestion using IAM roles and Least Privilege principles.

Architecture Overview

Loading Diagram...
Figure 1 — Mermaid diagram

Step-by-Step Instructions

Step 1: Create the CloudWatch Agent IAM Role

The CloudWatch agent requires permissions to write logs and metrics to the CloudWatch service.

bash
# Create the trust policy file cat <<EOF > trust-policy.json { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "ec2.amazonaws.com" }, "Action": "sts:AssumeRole" } ] } EOF # Create the role and attach the managed policy aws iam create-role --role-name CW-Agent-Lab-Role --assume-role-policy-document file://trust-policy.json aws iam attach-role-policy --role-name CW-Agent-Lab-Role --policy-arn arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy aws iam create-instance-profile --instance-profile-name CW-Agent-Lab-Profile aws iam add-role-to-instance-profile --instance-profile-name CW-Agent-Lab-Profile --role-name CW-Agent-Lab-Role
▶Console Alternative
  1. Navigate to IAM > Roles > Create role.
  2. Select AWS Service and EC2.
  3. Search for and check CloudWatchAgentServerPolicy.
  4. Name the role CW-Agent-Lab-Role and finish.

Step 2: Launch EC2 and Install CloudWatch Agent

Launch a T3.micro instance and install the agent using Systems Manager or direct installation.

bash
# Get the latest Amazon Linux 2023 AMI ID AMI_ID=$(aws ec2 describe-images --owners amazon --filters "Name=name,Values=al2023-ami-kernel-6.1-x86_64*" --query 'Images[0].ImageId' --output text) # Launch Instance aws ec2 run-instances --image-id $AMI_ID --count 1 --instance-type t3.micro --iam-instance-profile Name=CW-Agent-Lab-Profile --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=Lab-Monitor-Target}]'

Step 3: Configure Log Collection

Once logged into the instance, create the amazon-cloudwatch-agent.json configuration to track /var/log/messages.

bash
# Example config to be placed at /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json { "logs": { "logs_collected": { "files": { "collect_list": [ { "file_path": "/var/log/messages", "log_group_name": "LabSystemLogs", "log_stream_name": "{instance_id}" } ] } } } }

[!TIP] Use the CloudWatch Agent wizard by running sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard for an interactive setup.

Step 4: Create a Metric Filter

Extract a metric to count "ERROR" occurrences in your logs.

bash
aws logs put-metric-filter \ --log-group-name "LabSystemLogs" \ --filter-name "ErrorCount" \ --filter-pattern "ERROR" \ --metric-transformations \ metricName=ErrorCount,metricNamespace=LabNamespace,metricValue=1

Checkpoints

  • Verification 1: Run sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a status on the instance. Expected output: "status": "running".
  • Verification 2: Navigate to CloudWatch > Log Groups. Verify LabSystemLogs exists and contains data.
  • Verification 3: Run logger "This is an ERROR test" on the instance. Check the Metrics section in CloudWatch for the ErrorCount metric increasing.

Teardown

bash
# Terminate Instance INSTANCE_ID=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=Lab-Monitor-Target" --query "Reservations[].Instances[].InstanceId" --output text) aws ec2 terminate-instances --instance-ids $INSTANCE_ID # Delete Log Group aws logs delete-log-group --log-group-name LabSystemLogs # Delete IAM Role and Profile aws iam remove-role-from-instance-profile --instance-profile-name CW-Agent-Lab-Profile --role-name CW-Agent-Lab-Role aws iam delete-instance-profile --instance-profile-name CW-Agent-Lab-Profile aws iam detach-role-policy --role-name CW-Agent-Lab-Role --policy-arn arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy aws iam delete-role --role-name CW-Agent-Lab-Role

Troubleshooting

IssueLikely CauseSolution
Logs not appearingIAM Role MissingEnsure CW-Agent-Lab-Profile is attached to the EC2 instance.
Agent won't startInvalid JSONRun amazon-cloudwatch-agent-ctl -a fetch-config and check syntax errors.
Metric Filter is 0Case SensitivityFilter patterns are case-sensitive. Use "ERROR" exactly as it appears in the log.

Stretch Challenge

Encryption at Rest: Modify the CloudWatch Log Group to use a Customer Managed Key (CMK) via AWS KMS.

  • Requirement: You must update the KMS Key Policy to allow the CloudWatch Logs service (logs.<region>.amazonaws.com) the kms:Encrypt and kms:Decrypt permissions.

Cost Estimate

ServiceConfigurationEstimated Cost (Monthly)
EC2T3.microFree Tier eligible (or ~$7.50/mo)
CloudWatch LogsFirst 5GB free$0.50 per GB ingested thereafter
S3 StorageStandard-IA/Glacier~$0.01 - $0.02 per GB

Concept Review

Metric Filters vs. Metric Streams

FeatureMetric FiltersMetric Streams
MechanismScans existing log data for patterns.Continuous export of metrics to a destination.
LatencyNear real-time (minutes).High-speed (sub-minute).
Common UseCreating metrics from legacy log files.Exporting metrics to Datadog, New Relic, or S3.
StorageStored within CloudWatch.Delivered to S3/Firehose.

High Resolution Metrics

By default, CloudWatch metrics have a 1-minute resolution. High-resolution metrics allow for data points at 1-second intervals, useful for critical performance monitoring.

Compiling TikZ diagram…
⏳
Running TeX engine…
This may take a few seconds
Figure 2 — TikZ diagram
All AWS Certified DevOps Engineer - Professional (DOP-C02) Study Resources

Related Notes

  • AWS DevOps: Collection, Aggregation, and Storage of Logs and Metrics875 words
  • Mastering AWS Alerting and Automated Remediation1,050 words
  • Study Guide: Analyzing Failed Deployments in AWS940 words
  • Incident Analysis: Troubleshooting Failed Processes in AWS1,050 words
  • Mastering AWS Monitoring & Security Analytics: Logs, Metrics, and Findings1,050 words
  • AWS Log Analysis: Athena, CloudWatch Insights, and OpenSearch920 words
  • Analyzing Real-Time Log Streams with Amazon Kinesis Data Streams985 words
  • CloudWatch Anomaly Detection Alarms: Professional Study Guide820 words
  • AWS Application Storage Patterns: EBS, EFS, and S31,054 words
  • Lab: Automating Security Controls and Data Protection with AWS Secrets Manager and Config942 words
  • Master Study Guide: Automating Security Controls & Data Protection (AWS DOP-C02)1,184 words
  • Mastering AWS CloudFormation StackSets: Multi-Account & Multi-Region Orchestration895 words

Ready to study AWS Certified DevOps Engineer - Professional (DOP-C02)?

Practice tests, flashcards, and all study notes — free, no sign-up.

Start Studying

Ready to study AWS Certified DevOps Engineer - Professional (DOP-C02)?

Practice tests, flashcards, and all study notes — free, no sign-up needed.

Start Studying — Free
AWS Certified DevOps Engineer - Professional (DOP-C02) ResourcesExplore All HivesBlogHome

© 2026 BrainyBee. Free AI-powered exam prep.

Loading Diagram...
Flowchart, top to bottom. EC2 Instance (Amazon Linux 2023) connects to CloudWatch Logs Group ("CloudWatch Agent"). EC2 Instance (Amazon Linux 2023)"] -->|"CloudWatch Agent"| B["CloudWatch Logs Group connects to CloudWatch Metrics ("Custom Metrics"). B connects to C ("Metric Filter"). C connects to SNS Notification ("Alarm"). B connects to Amazon S3 (Archive) ("Log Export"). E connects to S3 Glacier Deep Archive ("Lifecycle Policy").