DS Fever
DS Fever

Reputation: 91

How to create Dynamodb metrics in CDK typescript

I am writing CDK to create my beautiful Cloudwatch dashboard. I figured out how to create a widget with metrics using Lambda functions. But I can't find a way to create metrics for Dynamodb tables.

Lambda function has its own metrics properties in it

lambdaFunction.metricErrors()
lambdaFunction.metricInvocations()

So, I could add lambdaFunction's metrics into my Cloudwatch dashboard const dashboard = new Dashboard(this, 'Dashboard', { dashboardName: 'ImageMetadataServiceCloudWatch' })

const widget = new cloudwatch.GraphWidget({
    title: title,
    left: [
        lambdaFunction.metricErrors(),
        lambdaFunction.metricInvocations()
    ],
    right: [
        lambdaFunction.metricDuration()
    ]
});

dashboard.add(widget);

But I have no clue of getting metrics for Dynamodb tables.

Upvotes: 2

Views: 3984

Answers (2)

DS Fever
DS Fever

Reputation: 91

Here's the answer. Good luck on future CDK users and welcome to the CDK world.

    private metricForDynamoDBTable(table: dynamodb.Table, metricName: string, options: cloudwatch.MetricOptions = { }): cloudwatch.Metric {
        return new cloudwatch.Metric({
            metricName,
            namespace: 'AWS/DynamoDB',
            dimensions: {
              TableName: table.tableName
            },
            unit: cloudwatch.Unit.COUNT,
            label: metricName,
            ...options
        });
    }

Upvotes: 2

BraveNinja
BraveNinja

Reputation: 213

Posting here the python sample for dashboard creation, because there are not many source for CDK using Python on the internet:

        config_table_dashboard = aws_cloudwatch.Dashboard(self, "id", dashboard_name="provisionedthroughtput")
        dynamodb_metrics = []
        dynamodb_WCU_throughput = aws_cloudwatch.Metric(
            metric_name="ConsumedWriteCapacityUnits",
            namespace="AWS/DynamoDB",
            dimensions={"TableName": "table-name"},
            statistic="Average",
            unit=aws_cloudwatch.Unit.COUNT,
            period=core.Duration.minutes(amount=5))
        dynamodb_metrics.append(dynamodb_WCU_throughput)
        config_table_dashboard.add_widgets(aws_cloudwatch.GraphWidget(title="Dynamodb table provision", left=dynamodb_metrics))

Upvotes: 1

Related Questions