Skip to content

Commit

Permalink
feat: cloudwatch dashboard resource with example (#6)
Browse files Browse the repository at this point in the history
* feat: cloudwatch dashboard resource with example

* fix- tf-checks

* fix- readme action
  • Loading branch information
h1manshu98 committed Nov 21, 2023
1 parent 9654842 commit 4d64d31
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 101 deletions.
48 changes: 35 additions & 13 deletions .github/workflows/readme.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,66 @@
name: 'Create README.md file'
permissions: write-all

on:
pull_request:
push:
branches:
- master
paths:
- 'main.tf'
- 'variables.tf'
- 'outputs.tf'
- 'versions.tf'
workflow_dispatch:

jobs:

readme-create:

name: 'readme-create'
runs-on: ubuntu-latest

steps:
- name: 'Checkout'
- name: Updating GitHub Token
env:
GITHUB_TOKEN: ${{ secrets.GITHUB }}
run: echo "GH_TOKEN=${GITHUB_TOKEN}" >> $GITHUB_ENV

- name: checkout
uses: actions/checkout@master
with:
fetch-depth: 0
ref: master
token: ${{ env.GH_TOKEN }}

- name: Set up Python 3.7.
uses: actions/setup-python@v2
- name: 'Set up Python 3.7'
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: 'create readme'
uses: 'clouddrove/github-actions@v9.0.2'
uses: 'clouddrove/github-actions@9.0.3'
with:
actions_subcommand: 'readme'
github_token: '${{ secrets.GITHUB}}'
github_token: '${{ secrets.GITHUB }}'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN}}
GITHUB_TOKEN: ${{ secrets.GITHUB }}


- name: 'pre-commit check errors'
uses: pre-commit/action@v2.0.0
uses: pre-commit/action@v3.0.0
continue-on-error: true

- name: 'pre-commit fix erros'
uses: pre-commit/action@v2.0.0
uses: pre-commit/action@v3.0.0
continue-on-error: true

- name: 'push readme'
uses: 'clouddrove/github-actions@v9.0.2'
uses: 'clouddrove/github-actions@9.0.3'
continue-on-error: true
with:
actions_subcommand: 'push'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN}}
GITHUB_TOKEN: ${{ secrets.GITHUB }}

- name: 'Slack Notification'
uses: clouddrove/action-slack@v2
Expand All @@ -47,6 +69,6 @@ jobs:
fields: repo,author
author_name: 'CloudDrove'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required
GITHUB_TOKEN: ${{ secrets.GITHUB }} # required
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_TERRAFORM }} # required
if: always()
if: always()
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.
2 changes: 1 addition & 1 deletion _example/complete/versions.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Terraform version
terraform {
required_version = ">= 1.3.6"
required_version = ">= 1.6.2"

required_providers {
aws = {
Expand Down
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)
}
Loading

0 comments on commit 4d64d31

Please sign in to comment.