CDK(Python)でEventBridge構築時にdetail-typeが正しく変換されなかったこと

2023.03.23

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、ゲームソリューション部のsoraです。
今回は、CDK(Python)でEventBridge構築時にdetail-typeが正しく変換されなかったことについて書いていきます。
EventBridgeとは何かについては、ここでは記載しません。

L1コンストラクトでdetail-typeが正しく変換されなかった

以下コードを使用してEventBridgeを定義したところ、detail_typeが本来であればdetail-typeと変換されてほしいところ、detailTypeとなってしまい正常に動作しませんでした。

events.CfnRule(self, "tag-required-eventbridge",
    name="tag-required-rule",
    event_pattern=events.EventPattern(
        source=["aws.config"],
        detail_type = ["Config Rules Compliance Change"],
        detail = {
            "messageType": ["ComplianceChangeNotification"],
            "configRuleName": ["required-tags"],
            "newEvaluationResult": {
                "complianceType": ["NON_COMPLIANT"]
            }
        }
    ),
    targets=[events.CfnRule.TargetProperty(
        arn = topic.topic_arn,
        id = "tag-required-target",
        input_transformer=events.CfnRule.InputTransformerProperty(
            input_template="\"タグが付与されていないリソースを検知しました。\"\n\"<ルール違反のリソース情報>\"\n\"リソースタイプ: <resourceType>\"\n\"リソースID  : <resourceId> \"",
            input_paths_map={
                "resourceId": "$.detail.resourceId",
                "resourceType": "$.detail.resourceType"
            }
        )
    )]
)

以下は先ほどのCDKから生成されたEventBridgeのイベントパターン

{
    "detailType": ["Config Rules Compliance Change"],
    "detail": {
        "configRuleName": ["required-tags"],
        "messageType": ["ComplianceChangeNotification"],
        "newEvaluationResult": {
            "complianceType": ["NON_COMPLIANT"]
        }
    },
    "source": ["aws.config"]
}

detailTypeではなくdetail-typeになってほしいため、本来であれば以下のようになってほしい。

{
    "detail-type": ["Config Rules Compliance Change"],
    "detail": {
        "configRuleName": ["required-tags"],
        "messageType": ["ComplianceChangeNotification"],
        "newEvaluationResult": {
            "complianceType": ["NON_COMPLIANT"]
        }
    },
    "source": ["aws.config"]
}

対処

これについてはCDK側のバグっぽくて、GitHub上にもIsuueで挙げられていました。
CDK(Python)のコードを書き換えることでは改善できなかったため、暫定的な対処として以下で手動で対処しました。

  • CDKでdeployした後に、EventBridgeのイベントパターンを編集する
  • CDKでCloudFormationテンプレートを生成して、detailTypeをdetail-typeに修正した後にテンプレートからデプロイする

最後に

今回は、CDK(Python)でEventBridge構築時にdetail-typeが正しく変換されなかったことについて記載しました。
どなたかの参考になると幸いです。