Building a Cloud Watch Dashboard for Load Balancers: A Terragrunt Approach

Building a Cloud Watch Dashboard for Load Balancers: A Terragrunt Approach

Introduction :-

Amazon CloudWatch Dashboard is a web-based service that allows users to monitor and visualize their AWS resources and applications in real-time.In this guide, we will walk you through the process of creating a CloudWatch dashboard specifically for monitoring your AWS Elastic Load Balancer (ELB) using Terraform. By leveraging the power of Terraform’s infrastructure-as-code approach, you can automate the creation and management of your CloudWatch dashboard, making it easier to maintain consistency and scalability across your infrastructure

Pre-Requestisites:-

  • An AWS account with appropriate permissions to create CloudWatch dashboards.

  • Terraform and Terragrunt should be installed on your local machine.

  • AWS credentials properly configured.

Process :-

Step1: First we need to define the Terraform resources. For that create a directory/folder with name lb_dashboard_terraform. Under the folder create terraform configuration files such as main.tf , variables.tf & outputs.tf.

#main.tf
resource "aws_cloudwatch_dashboard" "main" {
  dashboard_name = var.dashboard_name
  dashboard_body = jsonencode({
    widgets = [
      {
         type = "metric",
         x = 0,
         y = 6,
         width = 12,
         height = 6,
         properties = {
           metrics = [            
            [
             "AWS/ApplicationELB",
             "UnhealthyStateRouting",
             "Load Balancer",
             "${var.load_balancer_arn}",
             "Target Group",
             "${var.target_group_arn}",
             {
               region  = "us-west-2",
             },
          ],
          [
           "...",
            "Load Balancer",
            "${var.load_balancer_arn}",
            "Target Group",
            "${var.target_group_arn}",
          ],
          ]
           period = 300,
           stat = "Sum",
           stacked = false
           view    = "timeSeries" 
           region = "${var.region}"
           title = "Unhealthy State Routing"   
        }
      },
      {       
      type = "metric",
         x = 12,
         y = 12,
         width = 12,
         height = 6,
         properties = {
           metrics = [        
              [
              "AWS/ApplicationELB",
              "ProcessedBytes",
              "LoadBalancer",
              "${var.load_balancer_arn}",
              {
                region = "us-west-2",
              },
              ]
          ],
           period = 300,
           stat = "Average",
           stacked = false
           view    = "timeSeries" 
           region = "${var.region}"
           title = "ProcessedBytes"
        }
      }
    ]
  })
 }


resource "aws_cloudwatch_metric_alarm" "metrics" {
  alarm_name            = var.alarm_name
  comparison_operator   = "GreaterThanOrEqualToThreshold"
  evaluation_periods    = "2"
  metric_name           = "RequestCountPerTarget"
  namespace             = "AWS/ApplicationELB"
  period                = "120"
  statistic             = "Sum"
  threshold             = "20"
  alarm_description     = "This metric monitors load balancer requests exceeding 100 per minute"
  dimensions = {
    LoadBalancer = "${var.load_balancer_arn}"
    TargetGroup  = "${var.target_group_arn}"
  }
  insufficient_data_actions = []
  alarm_actions     = var.alarm_actions
}

step 2: Define the terraform variables for the main.tf file in a file named variables.tf.

#variables.tf
variable "load_balancer_arn" {
  type        = string
  description = "ARN of the load balancer to monitor"
}

variable "target_group_arn" {
  type        = string
  description = "ARN of the target group to monitor"
}

variable "dashboard_name" {
  type        = string
  description = "Name of the CloudWatch dashboard"
}

variable "alarm_name" {
  type        = string
  description = "Name of the CloudWatch alarm"
}

variable "region" {
  type        = string
  description = "AWS region"
}

variable "alarm_actions" {
  type        = set(string)
  description = "Alarm Actions"
  default     = []
}

Step 3:- create one more directory named lb_dashboard_terragrunt and create a file named terragrunt.hcl. Next define the terragrunt code in that file as shown below.

terraform {
  source = "../lb_dashboard_terraform"
}

include "root" {
  path = find_in_parent_folders()
}

inputs = {
  dashboard_name   = "loadbalancer-dashboard"
  target_group_arn  = "Target Group Suffix arn"
  load_balancer_arn = "Load Balancer Suffix arn"
  region            = "us-west-2"
  alarm_name        = "loadbalancer-monitoring"
}

Step 4: Pass your load balancer arn and target group arn values in the place of Target Group Suffix arn and Load Balancer Suffix arn.

Step 5: Apply your changes Once you’ve defined your Terraform resources and Terragrunt configuration, you can apply your changes with the following commands

terragrunt init
terragrunt plan
terragrunt apply

Note: make sure you navigate to the terragrunt file created folder and perform these commands

Conclusion :-

That’s it! You’ve created a CloudWatch dashboard for monitoring a load balancer and target group using Terraform and Terragrunt. You can customize this code to monitor other metrics or add additional resources to your infrastructure.