Skip to main content

cdk-grafana-json-dashboard-handler

A handler Custom Construct for JSON Grafana Dashboards - Deploy to Grafana using AWSCDK.

How it works

Declare the package as a dependency and import the Construct in your stack. Point it to your local Grafana dashboard file so the Construct can calculate an MD5 hash of it. This is needed as otherwise CloudFormation would not know when to redeploy your dashboard to Grafana when it changes. Upload your dashboard file in your CDK stack (s3assets, see example below), and pass the bucket and s3 file path to the Construct as well. Also, give the Construct a secret to resolve from SecretsManager in order to authenticate to your Grafana installation, in combination with the url where to find it. Finally give it a name so it can name your dashboard accordingly. Deploy!

Contents of the Custom Construct

The Construct contains a Lambda Singleton function, which gets wrapped by a CloudFormation Custom Resource.

Before using consider the following

  1. This construct is geared towards deploying json dashboards. This construct does not cater towards DSL for creating and developing Grafana Dashboards. The construct assumes you will place this json dashboard somewhere in S3. Consider deploying it using new s3assets.BucketDeployment and then pass the object path & bucket name to the construct so it knows where to fetch it.
  2. This construct assumes Bearer authorization, in which the value of Bearer is stored in AWS Secretsmanager, either plain or in an object for which you can specify the key, e.g. 'password' or {'pass' : 'password'}
  3. This construct currently does NOT support custom KMS encrypted files in s3 (see roadmap below)

Grafana Handler

Implement as following:

Write your Grafana Dashboard JSON file somewhere to disk.

Use that Dashboard JSON in your stack as follows:

# Example automatically generated from non-compiling source. May contain errors.
# setup the dependencies for the construct, for example like this
bucket = s3.Bucket(self, "pogg",
    auto_delete_objects=True,
    removal_policy=cdk.RemovalPolicy.DESTROY
)

fdp = s3assets.BucketDeployment(self, "pogu",
    sources=[s3assets.Source.asset("test/dashboard")],
    destination_bucket=bucket,
    destination_key_prefix="test/test"
)

secret = sm.Secret.from_secret_partial_arn(self, "smLookup",
    get_required_env_variable("GRAFANA_SECRET_PARTIAL_ARN"))
# Example automatically generated from non-compiling source. May contain errors.
dbr = GrafanaHandler(self, "pog",
    dashboard_app_name="cdkConstructTest",
    grafana_pw_secret=secret,
    grafana_url=get_required_env_variable("GRAFANA_URL"),
    bucket_name=bucket.bucket_name,
    object_key="test/test/dashboard/test-dashboard.json",
    local_file_path="test/dashboard/test-dashboard.json"
)
dbr.node.add_dependency(fdp)

If your handler needs to live inside your projects networking tier:

# Example automatically generated from non-compiling source. May contain errors.
dbr = GrafanaHandler(self, "pog",
    dashboard_app_name="cdkConstructTest",
    grafana_pw_secret=secret,
    grafana_url=get_required_env_variable("GRAFANA_URL"),
    bucket_name=bucket.bucket_name,
    object_key="test/test/dashboard/test-dashboard.json",
    local_file_path="test/dashboard/test-dashboard.json",
    vpc=testing_vpc,
    vpc_subnets={
        "subnets": [testing_private_subnet_iD1, testing_private_subnet_iD2, testing_private_subnet_iD3
        ]
    }
)
dbr.node.add_dependency(fdp)

More permissions

Whenever your handler needs more permissions use the addToRolePolicy on the properties exposed on the construct:

# Example automatically generated from non-compiling source. May contain errors.
dbr = GrafanaHandler(self, "pog",
    dashboard_app_name="cdkConstructTest",
    grafana_pw=process.env.pw,  # pass in a string value. CDK supports resolving to string values from SSM and SecretsManager
    grafana_url=process.env.url,
    path_to_file="../src/test/test-dashboard.json",
    local_file_path="test/dashboard/test-dashboard.json"
)

dbr.grafana_handler_function.add_to_role_policy(
    iam.PolicyStatement(
        actions=["ec2:*"],
        resources=["*"]
    ))

Example deployment

Design

TODO / Roadmap

  1. Add custom KMS key support for the dashboard files in s3.
  2. Reduce SecretsManager permissions

Metadata

Release files for cdk-grafana-json-dashboard-handler 0.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cdk-grafana-json-dashboard-handler 0.0.0
File Size Uploaded
cdk-grafana-json-dashboard-handler-0.0.0.tar.gz 166.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cdk-grafana-json-dashboard-handler 0.0.0
File Interpreter ABI Platform
cdk_grafana_json_dashboard_handler-0.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 331.5 kB

Release files / cdk-grafana-json-dashboard-handler-0.0.0.tar.gz

Download URL cdk-grafana-json-dashboard-handler-0.0.0.tar.gz
Size 166.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c0f12663d4f8a4717e24f8ad5651c612c1875f6c3b5e5d26db88358363547743
BLAKE2b-256 checksum
How to use checksums
0449c9785adf75d537ef842108a6b3b6770f17e1d4049827380687a1838167fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.3

Release files / cdk_grafana_json_dashboard_handler-0.0.0-py3-none-any.whl

Download URL cdk_grafana_json_dashboard_handler-0.0.0-py3-none-any.whl
Size 165.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6468c6e88effe4fe48d9440d4b5e3187f36d5789fbb4a3704ad62eaf46c1b6ef
BLAKE2b-256 checksum
How to use checksums
3acd75d6506303432e0ce24bc52a1d678c47ecba3ae9eed1f84839fd8bf6c7e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page