AuroraMysql8.0のAutoScalling設定

## あ

- オートスケーリングの設定をするとリーダーインスタンスが最低2台必要になる
- リーダーインスタンスを1台で構築した場合、オートスケーリング機能により1台自動的に追加され、自動的に2台体制になる


```terraform
locals {
  time = formatdate("YYYYMMDDhhmmZZZ", timestamp())
}

resource "aws_rds_cluster" "default" {
  cluster_identifier = "${var.system_name}-${terraform.workspace}-${var.service_name}-aurora-cluster"
  engine             = "aurora-mysql"
  engine_version     = "8.0.mysql_aurora.3.01.0"
  availability_zones = [
    "ap-northeast-1a",
    "ap-northeast-1c",
    "ap-northeast-1d"
  ]
  database_name                   = "initial_database"
  master_username                 = "admin"
  master_password                 = var.db_default_password
  backup_retention_period         = 7
  db_subnet_group_name            = data.aws_db_subnet_group.default.name
  db_cluster_parameter_group_name = "${var.system_name}-${terraform.workspace}-db-cluster-parameter-group"
  vpc_security_group_ids = [
    aws_security_group.db_security_group.id
  ]
  enabled_cloudwatch_logs_exports = [
    "audit",
    "error",
    "general",
    "slowquery"
  ]
  final_snapshot_identifier = "${var.system_name}-${terraform.workspace}-${var.service_name}-finalsnapshot-${local.time}"
  skip_final_snapshot       = false
  deletion_protection       = false
  storage_encrypted         = true

  tags = {
    Name = "${var.system_name}-${terraform.workspace}-${var.service_name}-aurora-cluster"
  }
  lifecycle {
    ignore_changes = [
      final_snapshot_identifier,
    ]
  }
}

resource "aws_rds_cluster_instance" "instances" {
  count              = var.instance_count
  identifier         = "${aws_rds_cluster.default.cluster_identifier}-${terraform.workspace}-instance-${count.index}"
  cluster_identifier = aws_rds_cluster.default.id
  instance_class     = var.instance_class
  engine             = aws_rds_cluster.default.engine
  engine_version     = aws_rds_cluster.default.engine_version
  promotion_tier     = count.index

  tags = {
    Name = "${aws_rds_cluster.default.cluster_identifier}-${terraform.workspace}-instance-${count.index}"
  }
}

# ここからオートスケーリングの設定
resource "aws_appautoscaling_target" "replicas" {
  service_namespace  = "rds"
  scalable_dimension = "rds:cluster:ReadReplicaCount"
  resource_id        = "cluster:${aws_rds_cluster.default.id}"
  min_capacity       = 1
  max_capacity       = 15
}

resource "aws_appautoscaling_policy" "replicas" {
  name               = "${var.system_name}-${terraform.workspace}-${var.service_name}-cpu-auto-scaling"
  service_namespace  = aws_appautoscaling_target.replicas.service_namespace
  scalable_dimension = aws_appautoscaling_target.replicas.scalable_dimension
  resource_id        = aws_appautoscaling_target.replicas.resource_id
  policy_type        = "TargetTrackingScaling"

  target_tracking_scaling_policy_configuration {
    predefined_metric_specification {
      predefined_metric_type = "RDSReaderAverageCPUUtilization"
    }

    target_value       = 30
    scale_in_cooldown  = 300
    scale_out_cooldown = 300
  }
}

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "= 3.74.1"
    }
  }
}
```


## 負荷をかける
- mysqlspapというMysql標準の負荷ツールを使う
```
mysqlslap --user=admin --password='<password>' --concurrency=1000 --iterations=10000 --no-drop --query="select * from test.user;" --host='aiueo-instance-1.xxxx.ap-northeast-1.rds.amazonaws.com' --port=3306 --engine=innodb
```