Creating a cloud watch dashboard for monitoring load balancer and target group in terragrunt

Creating a cloud watch dashboard for monitoring load balancer and target group in terragrunt

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 installed on your local machine.

  • AWS credentials properly configured.

Step1: First we need to define your resources in Terraform you’ll need to define the resources you want to create in #Terraform. Here’s a Terraform file that creates a CloudWatch dashboard for monitoring a load balancer and target group I created

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: In the module, cloud watch_dashboard folder, variable.tf file copy paste the code mentioned below into the file

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: Use Terragrunt to manage your Terraform code Terragrunt is a tool that makes it easier to manage multiple Terraform projects by providing additional features like remote state management and automatic dependency resolution. To use Terragrunt, you’ll need to create a new directory for your Terragrunt code and define a Terragrunt configuration file with extension .hcl

But here I have the file already created

terraform {
  source = "../../../../../infrastructure/module/cloudwatch-dashboards/loadbalancer-dashboard"
}


include "root" {
  path = find_in_parent_folders()
}

dependency "loadbalancer" {
  config_path = "../../loadbalancer/loadbalancer"
}


locals {
  common_vars = read_terragrunt_config(find_in_parent_folders("env.hcl"))
  regional_vars = read_terragrunt_config(find_in_parent_folders("regional.hcl"))
 }

inputs = {
  dashboard_name = "loadbalancer-dashboard"
  target_group_arn      = dependency.loadbalancer.outputs.target_group_arn_suffixes
  load_balancer_arn     = dependency.loadbalancer.outputs.lb_arn_suffix
  region                = "us-west-2"
  alarm_name          = "loadbalancer-monitoring"
}

This configuration file specifies that Terragrunt should use the Terraform code in the source location we have created in the first step

Step 4: here we have already created a load balancer so we are using dependency to use that load balancer (so if there is no load balancer created by you you need to create it and you can give that location in dependency)

Note: if you have a set of common resources shared across multiple environments, you can define them in a separate Terraform module and then reference that module in your environment-specific Terragrunt configuration files.

Step 5: in the above figure the input block we are fetching the target group and load balancer arn from the outputs file of the load balancer we created

This is how you need to create it in the module folder create a folder and save terraform file, in the environment folder create the folder and create the terragrunt.hcl file

Step 6: 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.