LoginSignup
0

More than 1 year has passed since last update.

TerraformでAmazon Auroraクラスタを自動構築する(カスタムエンドポイント編)

Posted at

はじめに

Amazon Aurora記事第4弾。

Auroraは、デフォルトのエンドポイントとしてリーダー用エンドポイントとライター用エンドポイントを用意してくれている。
しかし、実際には用途に応じてOLTP用とOLAP用のエンドポイントを分けて運用したくなることもあるだろう。

今回は、上記のようなケースに対応するためにOLAP用のエンドポイントを作ってみよう。

ただし、カスタムエンドポイントには「インスタンスを動的に追加することができない」という制約がある。
※詳細はこちら記事の検証結果を確認いただきたい。
OLAPは動的にインスタンスを増やすというケースは少ないと思うが、AutoScalingしたインスタンスを動的に追加できないのはつらすぎるので、OLTP用にカスタムエンドポイントを作るのは難しいのではないかと思う。

今回の記事も第2,3弾同様、以前の記事で作ったTerraformのHCLに付け加える前提であるため、実際に触ってみる場合はまずは以前の記事から読んでいただきたい。

バッチ用Auroraインスタンスの作成

バッチ用のAuroraインスタンスを以下のように追加する。

なお、カスタムエンドポイントの本題からは少し外れるが、今回、promotion_tier = 0を設定している。これに伴い、リーダーインスタンスはpromotion_tier = 1以上の値にしておくことで、フェイルオーバーのインスタンスを、バッチ用インスタンスに向けることをできる。
ということで、availability_zone を、ライターインスタンスとは異なるAZにしておくことで、AZ障害にも耐えられるようなフェイルオーバーが可能になる。

resource "aws_rds_cluster_instance" "batch" {
  cluster_identifier = aws_rds_cluster.example.id
  identifier         = "${local.aurora_cluster_identifier}-batch-instance"

  engine                  = aws_rds_cluster.example.engine
  engine_version          = aws_rds_cluster.example.engine_version
  instance_class          = "db.t3.small"
  db_subnet_group_name    = aws_db_subnet_group.example.name
  db_parameter_group_name = aws_db_parameter_group.example.name
  promotion_tier = 0

  availability_zone = data.aws_subnet.my_subnet[1].availability_zone

  monitoring_role_arn = aws_iam_role.aurora_monitoring.arn
  monitoring_interval = 60

  publicly_accessible = true
}

カスタムエンドポイントの設定

カスタムエンドポイントの設定には、aws_rds_cluster_endpointのリソースを用いる。

エンドポイントは、cluster_endpoint_identifierで指定した値と.cluster-custom-xxxxxxxxxxxx.[リージョン名].rds.amazonaws.comを連結した文字で作られる。今回のケースで言えば、batch.cluster-custom-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.comだ。

また、custom_endpoint_typeREADERに設定しておくことで、ライターインスタンスに接続して負荷を上げてしまうことを回避できる。

他のリーダーインスタンスに接続しないように、static_membersで接続先を固定しよう。

resource "aws_rds_cluster_endpoint" "batch" {
  cluster_identifier          = aws_rds_cluster.example.id
  cluster_endpoint_identifier = "batch"
  custom_endpoint_type        = "READER"

  static_members = [
    aws_rds_cluster_instance.example[0].id,
    aws_rds_cluster_instance.batch.id,
  ]
}

これでterraform applyすると、以下のように、今回作成したインスタンスのみ接続先としてチェックされているようになっている(aurora-example-cluster-instance-1が表示されていないのは、ライターインスタンスであるため)

キャプチャ.png

前述の通り、バッチ用のインスタンスはクラスタのデフォルトのリーダーインスタンスには含まれてしまっているので、あまり高負荷にすると、OLTPにも影響する可能性があるので、使い方には注意が必要だ。この辺りがもう少し柔軟になってくれると良いのだが……。

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
0