RDS 파라미터 관리

RDS 생성하기 전에, Parameter Group을 생성합니다.

RDS 파라미터 그룹 설정

  • RDS를 생성할 때 Database 관련 parameter 등을 parameter group을 활용하여 관리할 수 있습니다.

  • 실제 RDS를 생성하기 전에 미리 parameter group을 생성하신 후에 해당 parameter group을 지정하면 이후에 파라미터 값 변경 정보를 코드로 관리하실 수 있습니다.

  • 본 가이드에서는 MySQL aurora를 기준으로 작성되었습니다.

Directory 구조

  • terraform/databases/<계정>/<VPC>/<Service Name>/

  • 편하신 구조로 변경하시고, 변경하신 후에는 backend도 경로에 맞게 수정해주시면 됩니다.

Remote State 생성

  • RDS는 VPC 종속적인 서비스이므로 VPC 관련 정보가 필요합니다.

  • 이전에 설정했던 terraform/variables/var_global.tf 를 Soft Link로 첨부하실 후에 해당 값을 사용하여 remote_state.tf 파일을 생성합니다.

  • 이전에 정리해놓았던 var_global.tf 에서 vpc 정보를 가져와서 세팅합니다. 단순히 var_global.tf 파일만을 soft link로 참조함으로써 손쉽게 remote_state를 구성하실 수 있습니다.

  • Merge 함수는 서로 다른 두 Map을 하나로 합쳐주는 함수입니다. 중복되는 값은 뒤에 정의된 값을 기준으로 합니다.

vim terraform/databases/dayone-prod/dayonep_apnortheast2/remote_state.tf
# Set Configuration to use VPC Information via remote_state
# VPC backend configuration is located in terraform/variables/var_global.tf
data "terraform_remote_state" "vpc" {
  backend = "s3"
  
  # merge function is used to merge the key-value pairs from two different map
  config =  merge(var.remote_state.vpc.dayonepapne2, {"role_arn"=var.assume_role_arn} )
   
  # You can also set like this. 
  # You should define variables to variables.tf and value of them to terraform.tfvars  
  #config = {
  #  bucket       = var.remote_state_bucket
  #  region       = var.remote_state_region
  #  key          = var.remote_state_key
  #  role_arn     = var.assume_role_arn
  #  session_name = var.atlantis_user
  #}
}

파라미터 그룹 생성

  • 아래 표시된 parameter 세팅은 예시입니다.

  • 처음에 모든 parameter 세팅을 하실 필요는 없습니다. Dynamic Parameter는 live인 상태에서 변경이 가능하기 때문에 이후에 변경 가능한 파라미터는 이후에 추가하셔도 됩니다.

  • 첫 생성 이후에 파라미터를 변경하는 경우에는 apply_method 변수를 통해서 적용시점을 지정하실 수 있습니다.

vim terraform/databases/dayone-prod/dayonep_apnortheast2/dayone/parameter_group.tf
# Aurora Parameter Group
# This is for the database instance not the cluster.
resource "aws_db_parameter_group" "dayone_aurora_pg" {
  name   = "dayone-aurora-${data.terraform_remote_state.vpc.outputs.shard_id}-pg"
  
  # Please change this value to version you want to use 
  family = "aurora-mysql5.7"
  
  # From this, you could override the default value of DB parameter
  parameter {
    # Enable Slow query logging
    name  = "slow_query_log"
    value = "1"
  }

  parameter {
    # Set long query time to 1 second 
    name  = "long_query_time"
    value = "1"
  }

  parameter {
    # Set DB connection timeout to 5 seconds
    name  = "connect_timeout"
    value = "5"
  }

  parameter {
    # Increase the maximum number of connections to 16000 which is the maximum of aurora DB connection
    name  = "max_connections"
    value = 16000
  }

  parameter {
    # Disable performance schema
    name         = "performance_schema"
    value        = 0
    apply_method = "pending-reboot" #  Changes applied when DB is rebooted
  }
}

# Aurora Cluster Parameter Group
# This is for the cluster of instances not the instance
resource "aws_rds_cluster_parameter_group" "dayone_aurora_cluster_pg" {
  name        = "dayone-aurora-${data.terraform_remote_state.vpc.outputs.shard_id}-cluster-pg"

  # Please change this value to version you want to use 
  family      = "aurora-mysql5.7"

  description = "dayone RDS cluster parameter group"

  parameter {
    # Set Timezone to Seoul
    name  = "time_zone"
    value = "asia/seoul"
  }

  parameter {
    # Enable Slow query Logging
    name  = "slow_query_log"
    value = "1"
  }

  parameter {
    # Set long query time to 1 second 
    name  = "long_query_time"
    value = "1"
  }
  
  parameter {
    # Set server collation
    name  = "collation_server"
    value = "utf8mb4_bin"
  }

  parameter {
    name  = "character_set_connection"
    value = "utf8mb4"
  }

  parameter {
    name  = "character_set_server"
    value = "utf8mb4"
  }

  parameter {
    name  = "character_set_client"
    value = "utf8mb4"
  }

  parameter {
    name  = "character_set_database"
    value = "utf8mb4"
  }

  parameter {
    name  = "collation_connection"
    value = "utf8mb4_bin"
  }

  parameter {
    name  = "character_set_filesystem"
    value = "utf8mb4"
  }

  parameter {
    name  = "character_set_results"
    value = "utf8mb4"
  }

  parameter {
    # Increase the maximum number of connections to 16000 which is the maximum of aurora DB connection
    name  = "max_connections"
    value = 16000
  }

  parameter {
    apply_method = "pending-reboot"
    name         = "performance_schema"
    value        = "0"
  }
  
  parameter {
    apply_method = "pending-reboot"
    name         = "query_cache_type"
    value        = "0"
  }

  parameter {
    # Set max connection errors to 999999
    apply_method = "immediate" #  Changes applied immediately
    name         = "max_connect_errors"
    value        = "999999"
  }

  parameter {
    # Enabled audit logging
    apply_method = "immediate"
    name = "server_audit_logging"
    value = 1
  }

}

결과 확인

  • Cluster 파라미터 그룹과 instance 파라미터 그룹이 함께 생성되면 정상입니다.

Last updated