Batch 4: Cloud Ansible AWS & Azure Docker Apache & NGINX ← Full TOC

← Cloud Index

📊 Cloud Monitoring & Cost — CloudWatch, Cost Explorer, tagging, rightsizing

Cloud costs can spiral without monitoring and governance. Tagging all resources, setting billing alarms, and regularly rightsizing instances are essential practices for production cloud environments.

📋 Reference

Term / ServiceDescription / ValueNotes
CloudWatchAWS monitoring service for metrics, logs, alarmsCollect metrics, create dashboards, trigger alarms
CloudWatch metricsCPU, NetworkIn, DiskReadOps, custom metricsPre-built metrics for all AWS services
CloudWatch alarmTrigger action when metric threshold breachedSend SNS notification, trigger Auto Scaling
CloudWatch LogsCentralized log aggregation serviceLambda logs, EC2 logs, VPC flow logs
CloudWatch DashboardVisual monitoring dashboardCombine metrics, alarms, logs in one view
CloudTrailAudit log of all API calls in your accountWho did what, when — compliance and security
ConfigTrack resource configuration changes over timeCompliance rules, configuration history
Cost ExplorerAnalyze and visualize AWS spendingBreak down by service, tag, region, time
Billing alarmAlert when estimated charges exceed thresholdSet at $10, $50, $100 — catch runaway costs early
BudgetsSet spending budgets with alertsAlert at 80% and 100% of monthly budget
Cost allocation tagsuser:project, user:env, user:ownerTag resources to track costs by team/project
Reserved Instance savingsUp to 75% vs On-Demand3-year all-upfront gives maximum savings
Savings PlansCommit to $/hour in exchange for discountMore flexible than RIs — applies across instance types
Spot Instance savingsUp to 90% off On-DemandFor fault-tolerant batch jobs, CI/CD, dev workloads
RightsizingMatch instance size to actual usageLook at CPU/memory utilization — downsize idle instances
Auto ScalingMatch capacity to demand automaticallyScale out during traffic spikes, in during quiet periods
Trusted AdvisorAWS service for cost/security/performance recommendationsFree for basic checks, Business/Enterprise support for full
Health DashboardAWS Service Health Dashboard + Personal Health DashboardMonitor AWS service outages affecting your account
Azure MonitorAzure monitoring equivalent to CloudWatchMetrics, logs, alerts, dashboards
Azure Cost ManagementAzure cost analysis and budgeting toolEquivalent to AWS Cost Explorer + Budgets
GCP Cloud MonitoringStackdriver Monitoring — GCP equivalentMetrics, alerting, dashboards, uptime checks

💡 Example

# Cloud Monitoring & Cost Management

# ── CloudWatch Alarm (billing threshold) ─────────────────────
aws cloudwatch put-metric-alarm \
  --alarm-name "BillingAlarm-50USD" \
  --alarm-description "Alert when charges exceed $50" \
  --metric-name EstimatedCharges \
  --namespace AWS/Billing \
  --statistic Maximum \
  --period 86400 \
  --threshold 50 \
  --comparison-operator GreaterThanThreshold \
  --evaluation-periods 1 \
  --alarm-actions arn:aws:sns:us-east-1:123456789012:billing-alerts \
  --dimensions Name=Currency,Value=USD \
  --treat-missing-data notBreaching

# ── CloudWatch Metric Alarm (CPU) ────────────────────────────
aws cloudwatch put-metric-alarm \
  --alarm-name "HighCPU-WebServer" \
  --metric-name CPUUtilization \
  --namespace AWS/EC2 \
  --dimensions Name=InstanceId,Value=i-1234567890abcdef0 \
  --period 300 \
  --statistic Average \
  --threshold 80 \
  --comparison-operator GreaterThanThreshold \
  --evaluation-periods 2 \
  --alarm-actions arn:aws:sns:us-east-1:123456789012:ops-alerts \
  --ok-actions     arn:aws:sns:us-east-1:123456789012:ops-alerts

# ── CloudWatch Log Insights query ────────────────────────────
aws logs start-query \
  --log-group-name /aws/lambda/MyWebUniversityAPI \
  --start-time $(date -d '1 hour ago' +%s) \
  --end-time   $(date +%s) \
  --query-string '
    fields @timestamp, @message
    | filter @message like /ERROR/
    | sort @timestamp desc
    | limit 20'

# ── Cost Explorer: last month by service ─────────────────────
aws ce get-cost-and-usage \
  --time-period Start=2026-05-01,End=2026-06-01 \
  --granularity MONTHLY \
  --metrics BlendedCost \
  --group-by Type=DIMENSION,Key=SERVICE \
  --query 'ResultsByTime[0].Groups[*].[Keys[0],Metrics.BlendedCost.Amount]' \
  --output table

# ── Tag resources for cost tracking ──────────────────────────
# Apply tags to ALL resources
aws ec2 create-tags \
  --resources i-1234567890abcdef0 \
  --tags Key=Project,Value=MyWebUniversity \
         Key=Environment,Value=Production \
         Key=Owner,Value=alice \
         Key=CostCenter,Value=Engineering

# ── Python: CloudWatch custom metrics ────────────────────────
import boto3
from datetime import datetime

cloudwatch = boto3.client('cloudwatch', region_name='us-east-1')

# Publish custom metric
cloudwatch.put_metric_data(
    Namespace='MyWebUniversity/App',
    MetricData=[
        {
            'MetricName': 'ActiveUsers',
            'Value':      142,
            'Unit':       'Count',
            'Timestamp':  datetime.utcnow(),
            'Dimensions': [
                {'Name': 'Environment', 'Value': 'Production'},
                {'Name': 'Region',      'Value': 'us-east-1'},
            ],
        },
        {
            'MetricName': 'ResponseTime',
            'Value':      0.245,
            'Unit':       'Seconds',
            'Timestamp':  datetime.utcnow(),
        },
    ]
)
print("Custom metrics published")

# Get metric statistics
response = cloudwatch.get_metric_statistics(
    Namespace='AWS/EC2',
    MetricName='CPUUtilization',
    Dimensions=[{'Name': 'InstanceId', 'Value': 'i-1234567890abcdef0'}],
    StartTime=datetime(2026, 6, 24),
    EndTime=datetime(2026, 6, 25),
    Period=3600,
    Statistics=['Average', 'Maximum']
)
for dp in sorted(response['Datapoints'], key=lambda x: x['Timestamp']):
    print(f"  {dp['Timestamp'].strftime('%H:%M')} avg={dp['Average']:.1f}% max={dp['Maximum']:.1f}%")

# pip install boto3 && python3 monitoring.py

← Compute & Storage  |  🏠 Index  |  Cloud Security →