Skip to main content

Amazon CloudWatch

Overview

The AWS CloudWatch Module provides a reusable and scalable way to provision and manage Amazon CloudWatch resources using Terraform.
It abstracts the complexity of configuring monitoring, logging, and alerting services, enabling developers and operators to define observability infrastructure with minimal effort while following AWS best practices.

This module supports both resource creation and data sources for managing CloudWatch metrics, alarms, dashboards, and log groups in a consistent and modular way.


Features

  • 🚀 Reusable & Modular – Predefined structure for monitoring and logging resources.
  • 📊 Comprehensive Monitoring – Collects and tracks metrics across AWS services.
  • 🔔 Alerting & Notifications – Supports CloudWatch alarms integrated with SNS for automated notifications.
  • 📝 Centralized Logging – Manage and query application and infrastructure logs with CloudWatch Logs.
  • 📈 Dashboards & Insights – Build visual dashboards and generate insights from metrics and logs.
  • 🧩 Extensible – Easy to integrate with other Terraform modules and AWS services.

Core Concepts

  1. Metrics & Alarms

    • Create and manage custom and AWS-provided metrics.
    • Configure alarms to monitor thresholds and trigger automated actions.
    • Integrate alarms with SNS, Auto Scaling, or other services for automated responses.
  2. Logs Management

    • Manage CloudWatch log groups and log streams.
    • Support for log retention policies.
    • Enable centralized logging for multiple AWS resources and applications.
  3. Dashboards

    • Define CloudWatch dashboards to visualize key performance indicators (KPIs).
    • Support for multiple widgets (metrics graphs, alarms, and text).
    • Provide real-time insights into infrastructure and application performance.
  4. Data Module

    • Fetch existing metrics, alarms, dashboards, or log groups.
    • Useful for referencing CloudWatch resources in other Terraform modules.
  5. Separation of Concerns

    • resources/ handles creation of CloudWatch metrics, alarms, dashboards, and log groups.
    • data/ is focused on reading and referencing existing CloudWatch resources.
    • This separation ensures clarity, reusability, and modular design.

Sample Usage

# DEPLOY ANANTA CLOUD'S CLOUDWATCH MODULE

module "cloudwatch" {
source = "github.com/anantacloud/terraform-aws-cloudwatch-module/modules/resources"

# ==========================
# REQUIRED VARIABLES
# ==========================
log_group_name = "my-app-logs"
retention_days = 30

# ==========================
# OPTIONAL VARIABLES
# ==========================
alarms = [
{
name = "cpu-high"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
comparison_operator = "GreaterThanThreshold"
threshold = 80
period = 300
evaluation_periods = 2
statistic = "Average"
alarm_actions = ["arn:aws:sns:us-east-1:123456789012:my-sns-topic"]
}
]

dashboards = [
{
name = "my-dashboard"
widgets = [
{
type = "metric"
properties = {
metrics = [["AWS/EC2", "CPUUtilization", "InstanceId", "i-1234567890abcdef0"]]
period = 300
stat = "Average"
}
}
]
}
]

tags = {
Environment = "dev"
Project = "my-app"
}
}

Required

NameDescriptionTypeExample
log_group_nameName of the CloudWatch log groupstring"my-app-logs"
retention_daysNumber of days to retain log eventsnumber30

Optional

NameDescriptionTypeDefault
alarmsList of CloudWatch alarms to configurelist[]
dashboardsList of CloudWatch dashboards with widget definitionslist[]
tagsAdd custom tags to CloudWatch resourcesmap{}