如何在CDK打字稿中创建Dynamodb指标

时间:2019-07-20 17:16:13

标签: amazon-web-services aws-lambda amazon-cloudformation aws-cdk

我正在编写CDK以创建漂亮的 Cloudwatch仪表板。我想出了如何使用Lambda函数使用指标创建小部件。但是我找不到一种方法来为Dynamodb表创建指标

Lambda函数具有其自己的指标属性

lambdaFunction.metricErrors()
lambdaFunction.metricInvocations()

因此,我可以将lambdaFunction的指标添加到Cloudwatch仪表板中     const console = new Dashboard(this,“ Dashboard”,{         dashboardName:'ImageMetadataServiceCloudWatch'     })

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

dashboard.add(widget);

但是我不知道如何获得Dynamodb表的指标。

2 个答案:

答案 0 :(得分:0)

这是答案。祝未来的CDK用户好运,欢迎您进入CDK世界。

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
    });
}

答案 1 :(得分:0)

在此处发布用于创建仪表板的python示例,因为互联网上使用Python的CDK来源不多:

    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))