にゃみかんてっくろぐ

猫か百合を見守る壁になりたい

AWS CDK for .NET: C# で CloudWatch Metric Filter を設定する

.NET サポートあるじゃん」と思ったのでちょっと触ってみました。

手順とコード

  1. Getting Started With the AWS CDK - AWS Cloud Development Kit (AWS CDK) を参照して npm で AWS CDK をインストールする。
  2. Working with the AWS CDK in C# - AWS Cloud Development Kit (AWS CDK) を参照してプロジェクトを新規作成し、 NuGet から Amazon.CDK と Amazon.CDK.AWS.Logs パッケージを追加する。
  3. 書く。
  4. 先程のドキュメントや新規作成したプロジェクトの README.md に記載されている通り、 cdk deploy で適用する。
using Amazon.CDK;
using Amazon.CDK.AWS.Logs;

namespace P2PQuakeCDK
{
    public class P2PQuakeCDKStack : Stack
    {
        internal P2PQuakeCDKStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
        {
            new CfnMetricFilter(this, "www.p2pquake.net/p2pquake-server/error", new CfnMetricFilterProps()
            {
                FilterPattern = "ERROR",
                LogGroupName = "/vps/p2pquake.net/p2pquake-server",
                MetricTransformations = new CfnMetricFilter.IMetricTransformationProperty[]
                {
                    new CfnMetricFilter.MetricTransformationProperty()
                    {
                        MetricName = "www.p2pquake.net/p2pquake-server/error",
                        MetricNamespace = "app",
                        MetricValue = "1"
                    }
                }
            });
        }
    }
}

触ってみて

Node.js からは逃れられない

いきなり残念なお知らせですが、 .NET バインディングを使う場合でも Node.js からは逃れられません。

All CDK developers need to install Node.js (>= 10.3.0), even those working in languages other than TypeScript or JavaScript. The AWS CDK Toolkit (cdk command-line tool) and the AWS Construct Library are developed in TypeScript and run on Node.js.

Getting Started With the AWS CDK - AWS Cloud Development Kit (AWS CDK)

公式ドキュメントが若干怪しい

例えば CfnMetricFilter クラスのコンストラクタに投げ込む ICfnMetricFilterProps 型を見てみると、 MetricTransformations がなんと object 型になっています。

object MetricTransformations { get; }

Interface ICfnMetricFilterProps

コンソールでは、メトリクス名やメトリクス値を指定するはずの部分です。

f:id:no_clock:20200402000017p:plain
コンソールでの表示

困り果ててしまってアセンブリ ブラウザーで覗いてみると、 CfnMetricFilter.MetricTransformationProperty 型を投げ込めそうに見えます。実際、これで解決しました。

f:id:no_clock:20200402000613p:plain
アセンブリブラウザー ICfnMetricFilterProps

言語共通の API Reference に記載されている Type name も間違っているようです(ネストされていることが表現されていない)。

.NET Amazon.CDK.AWS.Logs.MetricTransformationProperty

interface MetricTransformationProperty · AWS CDK

コントリビューションチャンスか? ちょっと様子を伺ってみようと思います。

参考