DEV Community

Cover image for How-to setup a HA/DR database in AWS? [3 - Simple database]
Maxime Guilbert
Maxime Guilbert

Posted on • Updated on

How-to setup a HA/DR database in AWS? [3 - Simple database]

In this post, we will see how to create a simple database in AWS with Terraform and how we can configure it.


How to create a database ?

The minimum configuration looks like this:

resource "aws_db_instance" "default" {
  allocated_storage    = 10
  engine               = "mysql"
  engine_version       = "5.7"
  instance_class       = "db.t3.micro"
  name                 = "mydb"
  username             = "random_user"
  password             = "random_pwd"
  parameter_group_name = "default.mysql5.7"
  skip_final_snapshot  = true
}
Enter fullscreen mode Exit fullscreen mode
parameter definition
allocated_storage Size of your database in Go
engine Which kind of database you want. AWS allow at least "aurora", "MySql", "PostgreSQL", "MariaDB" and "Oracle"
engine_version Version of the engine to install on the database
instance_class Type of machine you want for your database. Depending the performances you need it can be really important to choose the right one. Check the AWS documentation about the instance types available : https://aws.amazon.com/rds/instance-types/
name Name of the database
username Username of the root account
password Password of the root account
parameter_group_name Group of parameters to use to configure the database. You can use a group with default values defined by AWS.
skip_final_snapshot Set to false if you want to generate a snapshot of the database on deletion. Otherwise, set to true.

How to create a custom parameter group ?

We saw that we can define a parameter group to override some database configurations.

To do it create an aws_db_parameter_group, and like the following example, you can change the encoding.

resource "aws_db_parameter_group" "default" {
  name   = "rds-group-perso"
  family = "mysql5.6"

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

  parameter {
    name  = "character_set_client"
    value = "utf8"
  }
}
Enter fullscreen mode Exit fullscreen mode

All the available parameters are listed in the AWS documentation :

Then, you can update the value properly in your aws_db_instance to use this newly created parameter group.

resource "aws_db_instance" "default" {
  ...
  parameter_group_name = aws_db_parameter_group.aws_db_parameter_group.id
  ...
}
Enter fullscreen mode Exit fullscreen mode

NOTE: You can set parameter_group_name = "rds-group-perso" in the aws_db_instance and it will also work. But using the parameter from the created resource is cleaner.


Other interesting configurable parameters

  • port - If you want to change the default port to access to the database
  • storage_encrypted - If you want to encrypt the data stored
  • kms_key_id - The KMS key id of the key to use for the encryption
  • enabled_cloudwatch_logs_exports - To set all the logs types you want to export in CloudWatch.
  • backup_window - Period of the day where daily backups are generated
  • backup_retention_period - Number of days you want to retain backups.
  • security_group_names - List of security groups to add to the database. This one is a really important one to control correctly who can access to your database.
  • maintenance_window - The window to perform maintenance in.

There are some other parameters available, please check the Terraform documentation to see them.


Links

Terraform


I hope it will help you! 🍺

And see you soon for the next part of this serie. 😀


Serie link


You want to support me?

Buy Me A Coffee

Top comments (0)