2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

RDSのイベントサブスクリプションはChatbot非対応

前回の記事でも触れたのですが、RDSのイベントサブスクリプションはChatbotに対応していません。

RDSのイベント通知を行うためのEventBridgeで実装した内容を記載します。

サンプルコード

// CDK For Terraform EventBridge SNS

import { Construct } from 'constructs';
import * as aws from '@cdktf/provider-aws';
import { List } from 'aws-sdk/lib/model';


export function eventBridgeSns(scope: Construct,
 SourceClusterIdentifier: List, // RDS Aurora Cluster
 SourceIdentifier: List, // RDS Aurora Cluster Instances
 snsTopicName: string,

) {

 // EventBridge Slack 通知

 // RDS Cluster Event
 const eventBridgeRdsCluster = new aws.cloudwatchEventRule.CloudwatchEventRule(scope, "snsRdsClusterNotification", {

  name: `${process.env.ENV_ID}-sns-rds-cluster-notification`,
  eventPattern: JSON.stringify({
   source: [
    "aws.rds"
   ],
   "detail-type": [
    "RDS DB Cluster Event"
   ],
   detail: {
    "SourceIdentifier": SourceClusterIdentifier,
    "SourceType": [
     "DB_CLUSTER"
    ],
    EventCategories: [
     "failure",
     "failover",
     "maintenance",
    ]
   }
  }),
 });

 // RDS Instance Event
 const eventBridgeRdsInstance = new aws.cloudwatchEventRule.CloudwatchEventRule(scope, "snsRdsInstanceNotification", {
  name: `${process.env.ENV_ID}-sns-rds-instance-notification`,
  eventPattern: JSON.stringify({
   source: [
    "aws.rds"
   ],
   "detail-type": [
    "RDS DB Instance Event"
   ],
   detail: {
    "SourceIdentifier": SourceIdentifier,
    "SourceType": [
     "DB_INSTANCE"
    ],
    EventCategories: [
     "availability",
     "creation",
     "failover",
     "failure",
     "maintenance",
     "recovery",
     "restoration",
    ]
   }
  }),
 });




 const snsRds = new aws.dataAwsSnsTopic.DataAwsSnsTopic(scope, "snsRdsName", {
  name: snsTopicName

 });

 new aws.cloudwatchEventTarget.CloudwatchEventTarget(scope, "snsRdsClusterNotificationTarget", {
  rule: eventBridgeRdsCluster.name,
  arn: snsRds.arn,
 });


 new aws.cloudwatchEventTarget.CloudwatchEventTarget(scope, "snsRdsInstanceNotificationTarget", {
  rule: eventBridgeRdsInstance.name,
  arn: snsRds.arn,
 });



}
  • 入力パラメータ
    eventBridgeSns(
      this,
      [
        "sample-aurora-cluster"
      ], // 監視対象RDSクラスター
      [
        "sample-aurora-instance1",
        "sample-aurora-instance2",
      ],// 監視対象RDSインスタンス
      "SampleSNSName" // SNS Topic Name 
    )

解説

SNSはコード内でARN情報を取得し設定するようにしています。

このサンプルだとRDSのインスタンスをハードコードしていますが、
実際はインスタンスの増減が発生した場合を考慮して動的に
取得するようにしています。

    const auroraInstanceNameBase = "sample-aurora-instance"
    // Aurora Instance 内のインスタンス情報取得function
    function createAuroraInstanceNames(auroraInstanceNameBase: string, instanceCount: number): string[] {
      // ステップ 1: 初期配列の作成
      const names = [auroraInstanceNameBase];

      // ステップ 2: ループ処理
      for (let i = 0; i < instanceCount; i++) {
        // ステップ 3: 配列への追加
        names.push(`${auroraInstanceNameBase}${i + 1}`);
      }

      // ステップ 4: 結果の配列を返す
      return names;
    }

    // 関数を呼び出し、結果を変数に格納
    const auroraInstanceNames = createAuroraInstanceNames(auroraInstanceNameBase, instanceCount);

利用した場合のコードは以下になります。

    eventBridgeSns(
      this,
      [
        "sample-aurora-cluster"
      ], // 監視対象RDSクラスター
      auroraInstanceNames// 監視対象RDSインスタンス
      "SampleSNSName" // SNS Topic Name 
    )
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?