Skip to content

Commit

Permalink
feat: cloudwatch dashboard resource with example
Browse files Browse the repository at this point in the history
  • Loading branch information
h1manshu98 committed Nov 15, 2023
1 parent 9654842 commit 0ae20ec
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 87 deletions.
94 changes: 94 additions & 0 deletions _example/complete/lambda_dashboard.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"widgets": [
{
"height": 1,
"width": 24,
"y": 0,
"x": 0,
"type": "text",
"properties": {
"markdown": "### ${lambda_function_name} Metrics"
}
},
{
"type": "metric",
"y": 1,
"x": 0,
"width": 6,
"height": 6,
"properties": {
"view": "timeSeries",
"stacked": false,
"metrics": [
[ "AWS/Lambda", "Duration", "FunctionName", "${lambda_function_name}" ]
],
"region": "${region}",
"title": "${lambda_function_name} Duration"
}
},
{
"type": "metric",
"y": 1,
"x": 6,
"width": 6,
"height": 6,
"properties": {
"view": "timeSeries",
"stacked": false,
"metrics": [
[ "AWS/Lambda", "Errors", "FunctionName", "${lambda_function_name}" ]
],
"region": "${region}",
"title": "${lambda_function_name} Errors"
}
},
{
"type": "metric",
"y": 1,
"x": 12,
"width": 6,
"height": 6,
"properties": {
"view": "timeSeries",
"stacked": false,
"metrics": [
[ "AWS/Lambda", "Invocations", "FunctionName", "${lambda_function_name}" ]
],
"region": "${region}",
"title": "${lambda_function_name} Invocations"
}
},
{
"type": "metric",
"y": 1,
"x": 18,
"width": 6,
"height": 6,
"properties": {
"view": "timeSeries",
"stacked": false,
"metrics": [
[ "AWS/Lambda", "ConcurrentExecutions", "FunctionName", "${lambda_function_name}" ]
],
"region": "${region}",
"title": "${lambda_function_name} ConcurrentExecutions"
}
},
{
"type": "metric",
"y": 2,
"x": 0,
"width": 6,
"height": 6,
"properties": {
"view": "timeSeries",
"stacked": false,
"metrics": [
[ "AWS/Lambda", "Throttles", "FunctionName", "${lambda_function_name}" ]
],
"region": "${region}",
"title": "${lambda_function_name} Throttles"
}
}
]
}
7 changes: 7 additions & 0 deletions _example/complete/lambda_packages/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import json
def lambda_handler(event, context):
print('Lambda function with Python!|')
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
109 changes: 46 additions & 63 deletions _example/complete/main.tf
Original file line number Diff line number Diff line change
@@ -1,70 +1,53 @@
provider "aws" {
region = "us-east-1"
region = local.region
}

module "dashboard" {
source = "../../"
start = "-PT4H"
widgets = [
{
height = 5
width = 14
y = 19
x = 10
locals {
name = "dashboard"
environment = "test"
region = "us-east-1"
}

##-----------------------------------------------------------------------------
## LAMBDA
##-----------------------------------------------------------------------------
module "lambda" {
source = "clouddrove/lambda/aws"
version = "1.3.1"

name = "${local.name}-lambda-function"
environment = local.environment

type = "metric"
properties = {
metrics = [
[ "ContainerInsights", "pod_number_of_container_restarts", "PodName", "api", "ClusterName", "prod-xcheck-eks-cluster", "Namespace", "api-mbj" ],
[ "...", "testing", ".", ".", ".", "testing" ],
]
view = "pie"
stacked = false
region = "us-east-1"
liveData = true
title = "Number of container restarts"
period = 300
setPeriodToTimeRange = false,
stat = "Average"
legend = {
"position": "bottom"
}
sparkline = true
trend = true
labels = {
"visible": "true"
}
}
},
{
height = 14
width = 10
y = 5
x = 0
enable = true
timeout = 60
runtime = "python3.8"
handler = "index.lambda_handler"
filename = "./test-dashboard-lambda-function.zip"
layer_filenames = ["./test-dashboard-lambda-function.zip"]
names = ["python_layer"]
compatible_runtimes = [["python3.8"]]
iam_actions = ["logs:CreateLogStream", "logs:CreateLogGroup", "logs:PutLogEvents"]
statement_ids = ["AllowExecutionFromCloudWatch"]
actions = ["lambda:InvokeFunction"]
principals = ["apigateway.amazonaws.com"]
reserved_concurrent_executions = null
}

##-----------------------------------------------------------------------------
## CLOUDWATCH DASHBOARD
##-----------------------------------------------------------------------------
module "cloudwatch_dashboard" {
source = "../../"

type = "metric"
properties = {
metrics = [
[ "ContainerInsights", "pod_memory_utilization", "PodName", "api", "ClusterName", "test-xcheck-eks-cluster", "Namespace", "api-puj" ],
[ "...", "api-test" ],
]
view = "pie"
region = "us-east-1"
title = "Tet pod Memory Utilization"
period = 300
trend = true
liveData = true
sparkline = true
setPeriodToTimeRange = false,
labels = {
"visible": "true"
}
}
}
]
enable = true
name = local.name
environment = local.environment
dashboard_body = templatefile("${path.module}/lambda_dashboard.json", {
region = local.region
lambda_function_name = split(":", module.lambda.arn)[6]
})
}

resource "aws_cloudwatch_dashboard" "dashboard" {
dashboard_body = module.dashboard.json_map_encoded
dashboard_name = "test-dashboard"
}
output "dashboard_arn" {
value = module.cloudwatch_dashboard.dashboard_arn
}
Binary file added _example/complete/test-dashboard-lambda-function.zip
Binary file not shown.
25 changes: 17 additions & 8 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
#Module : CLOUDWATCH DASHBOARD MODULE
#Description : Terraform module creates Cloudwatch Dashboard on AWS for monitoriing AWS services.
locals {
dashboard = {
start = var.start
end = var.end
periodOverride = var.periodOverride
widgets = var.widgets
}
json_map = jsonencode(local.dashboard)

##-----------------------------------------------------------------------------
## Labels module callled that will be used for naming and tags.
##-----------------------------------------------------------------------------
module "labels" {
source = "clouddrove/labels/aws"
version = "1.3.0"
name = var.name
repository = var.repository
environment = var.environment
managedby = var.managedby
label_order = var.label_order
}

resource "aws_cloudwatch_dashboard" "default" {
count = var.enable ? 1 : 0
dashboard_name = module.labels.id
dashboard_body = var.dashboard_body
}
10 changes: 6 additions & 4 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
output "json_map_encoded" {
description = "AWS CloudWatch Dashboard JSON template"
value = local.json_map
}
#Module : CLOUDWATCH DASHBOARD MODULE
#Description : Terraform module creates Cloudwatch Dashboard on AWS for monitoriing AWS services.

output "dashboard_arn" {
value = join("", aws_cloudwatch_dashboard.default[*].dashboard_arn)
}
48 changes: 36 additions & 12 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
variable "widgets" {
description = "The list of widgets in the dashboard."
type = any
#Module : LABEL
#Description : Terraform label module variables.
variable "name" {
type = string
default = ""
description = "Name (e.g. `app` or `cluster`)."
}

variable "end" {
description = "The end of the time range to use for each widget on the dashboard when the dashboard loads. If you specify a value for end, you must also specify a value for start. For each of these values, specify an absolute time in the ISO 8601 format. For example, 2018-12-17T06:00:00.000Z."
default = null
variable "repository" {
type = string
default = "https://github.com/clouddrove/terraform-aws-s3"
description = "Terraform current module repo"
}

variable "start" {
description = "The start of the time range to use for each widget on the dashboard."
default = null
variable "environment" {
type = string
default = ""
description = "Environment (e.g. `prod`, `dev`, `staging`)."
}

variable "periodOverride" {
description = "Use this field to specify the period for the graphs when the dashboard loads. Specifying auto causes the period of all graphs on the dashboard to automatically adapt to the time range of the dashboard. Specifying inherit ensures that the period set for each graph is always obeyed."
default = null
variable "label_order" {
type = list(any)
default = []
description = "Label order, e.g. `name`,`application`."
}

variable "managedby" {
type = string
default = "[email protected]"
description = "ManagedBy, eg 'CloudDrove'."
}

# Module : CLOUDWATCH DASHBOARD
# Description : Terraform CloudWatch Dashboard module variables.
variable "enable" {
type = bool
default = true
description = "Conditionally create CloudWatch Dashboard."
}

variable "dashboard_body" {
type = any
default = null
description = "Json file with Environment(optional) variables."
}

0 comments on commit 0ae20ec

Please sign in to comment.