diff --git a/_example/complete/lambda_dashboard.json b/_example/complete/lambda_dashboard.json new file mode 100644 index 0000000..ed04b05 --- /dev/null +++ b/_example/complete/lambda_dashboard.json @@ -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" + } + } + ] +} \ No newline at end of file diff --git a/_example/complete/lambda_packages/index.py b/_example/complete/lambda_packages/index.py new file mode 100644 index 0000000..218cc22 --- /dev/null +++ b/_example/complete/lambda_packages/index.py @@ -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!') + } \ No newline at end of file diff --git a/_example/complete/main.tf b/_example/complete/main.tf index a7bce75..9234e9a 100644 --- a/_example/complete/main.tf +++ b/_example/complete/main.tf @@ -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 +} \ No newline at end of file diff --git a/_example/complete/test-dashboard-lambda-function.zip b/_example/complete/test-dashboard-lambda-function.zip new file mode 100644 index 0000000..69934e1 Binary files /dev/null and b/_example/complete/test-dashboard-lambda-function.zip differ diff --git a/main.tf b/main.tf index ad907ea..e286dc2 100644 --- a/main.tf +++ b/main.tf @@ -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 +} \ No newline at end of file diff --git a/outputs.tf b/outputs.tf index c7ff992..3a052a2 100644 --- a/outputs.tf +++ b/outputs.tf @@ -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) +} \ No newline at end of file diff --git a/variables.tf b/variables.tf index 127aafd..f607968 100644 --- a/variables.tf +++ b/variables.tf @@ -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 = "hello@clouddrove.com" + 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." +} +