Deploying a cloudwatch dashboard for monitoring Lambda Function Using Terragrunt
Introduction:-
Before we begin creating the CloudWatch dashboard, make sure you have the following prerequisites:
An AWS account with appropriate permissions to create CloudWatch dashboards.
#Terraform and #Terragrunt should be installed on your local machine.
AWS credentials properly configured.
Step 1: Create a directory called cloudwatch-dashboard.
Create a folder with name lambda-dashboard in your homedirectory. with in that lambda-dashboard create a new folder module. In that module create file named main.tf
and add the following code as mention in the below
Along with the dashboard we are also creating cloud watch alarm to trigger and notify you, when the lambda function breaches the threshold.
resource "aws_cloudwatch_dashboard" "lambda_dashboard" {
dashboard_name = var.dashboard_name
dashboard_body = jsonencode({
widgets = [
{
type = "metric",
x = 0,
y = 6,
width = 12,
height = 6,
properties = {
metrics = [
[
"AWS/Lambda",
"GetFunctionEventInvokeConfig",
"FunctionName",
"${var.lambda_function_name}",
"Resource",
"GetFunctionEventInvokeConfig"
],
[
"AWS/Usage",
"CallCount",
"Type",
"API",
".",
"GetFunctionEventInvokeConfig",
"Service",
"Lambda",
"Class",
"None"
],
]
period = 300
stat = "Sum"
visible = false
region = "us-west-2"
title = "Lambda Function Invocations"
}
},
{
type = "metric",
x = 0,
y = 12,
width = 12,
height = 6,
properties = {
metrics = [
[
"AWS/Lambda",
"GetRuntimeManagementConfig",
"FunctionName",
"${var.lambda_function_name}",
"Resource",
"GetRuntimeManagementConfig"
],
[
"AWS/Usage",
"CallCount",
"Type",
"API",
".",
"GetRuntimeManagementConfig",
"Service",
"Lambda",
"Class",
"None"
],
]
period = 300
stat = "Sum"
visible = false
region = "us-west-2"
title = "Lambda Function Runtime Management"
}
}
]
})
}
resource "aws_cloudwatch_metric_alarm" "duration_alarm" {
alarm_name = var.alarm_name
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 1
metric_name = "Duration"
namespace = "AWS/Lambda"
period = 300
statistic = "Average"
threshold = 5000
alarm_description = "This alarm is triggered if Lambda duration exceeds 5 seconds"
alarm_actions = var.alarm_actions
}
resource "aws_cloudwatch_metric_alarm" "invocations_alarm" {
alarm_name = var.alarm_name1
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 1
metric_name = "Invocations"
namespace = "AWS/Lambda"
period = 300
statistic = "Sum"
threshold = 1000
alarm_description = "This alarm is triggered if Lambda invocations exceed 1000"
alarm_actions = var.alarm_actions
}
step 2: In the module folder, create one-more file called variable.tf file and paste the code mentioned below
variable "dashboard_name" {
type = string
description = "Name of the Dashboard"
}
variable "alarm_name" {
type = string
description = "Alarm name"
}
variable "alarm_name1" {
type = string
description = "Alarm name"
}
variable "region" {
type = string
description = "default region"
}
variable "alarm_actions" {
type = set(string)
description = "Alarm Actions"
default = []
}
variable "lambda_function_name" {
type = string
description = "Lambda Function name"
}
variable "lambda_function_arn" {
type = string
description = "lambda function arn"
}
Step 3:- Now Create a one more folder in called lambda in your lambda-dashboard directory and with in that lambda folder create a file called terragrunt.hcl
& then copy paste the below code.
terraform {
source = "../module"
}
include "root" {
path = find_in_parent_folders()
}
locals {
function_name = "example-lambda"
function_arn = "arn:aws:lambda:us-east-1:123456789012:function:my-lambda-function"
sns_topic_arn = "arn:aws:sns:us-east-1:123456789012:my-sns-topic"
}
inputs = {
alarm_name = "lambda-monitor"
alarm_name1 = "lambda-monitor2"
alarm_name2 = "lambda-monitor3"
dashboard_name = "lambda-dashboard"
region = "us-east-1"
alarm_actions = [local.sns_topic_arn]
lambda_function_name = local.function_name
lambda_function_arn = local.function_arn
}
Step 4:- Once all the setup is done locate to your cloud-watch folder and run terragrunt apply to deploy your code using terragrunt.
Step 5:- As you can see that there is a dashboard called lambda dashboard is being created in your aws cloud watch
Source-code Link :- “github.com/MahiraTechnology/Mahira-medium.git”.
Conclusion:-
In this tutorial, we explored how to create and deploy a #CloudWatch dashboard using #Terraform. We leveraged the power of #infrastructure-as-code to define the dashboard #configuration and #automate its #deployment. With the CloudWatch dashboard in place, you can easily monitor the key metrics of your #Lambda functions, such as# invocations, errors, duration, and more.