generated from clouddrove/terraform-module-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cloudwatch dashboard resource with example (#6)
* feat: cloudwatch dashboard resource with example * fix- tf-checks * fix- readme action
- Loading branch information
1 parent
9654842
commit 4d64d31
Showing
10 changed files
with
254 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.