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
}
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"
}
}
All the available parameters are listed in the AWS documentation :
- MySQL : https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraMySQL.Reference.html
- PostgreSQL : https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Reference.ParameterGroups.html
- Maria DB : https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Appendix.MariaDB.Parameters.html
- PostgreSQL : https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Appendix.PostgreSQL.CommonDBATasks.html#Appendix.PostgreSQL.CommonDBATasks.Parameters
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
...
}
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
- aws_db_instance : https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/db_instance
- aws_db_parameter_group : https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/db_parameter_group
I hope it will help you! 🍺
And see you soon for the next part of this serie. 😀
Serie link
- 1 - Start : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-1-1ko7
- 2 - Definitions : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-2-definitions-93p
- 3 - Simple database : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-3-simple-database-a9o
- 4 - HA Database : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-4-ha-database-4kek
- 5 - DR database : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-5-dr-database-278b
- 6 - Create from snapshot : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-6-create-from-snapshot-2mbf
- 7 - Dynamic Terraform backend definition : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-7-dynamic-terraform-backend-definition-3aga
- 8 - Multiple instances in multiple regions : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-8-multiple-instances-in-multiple-regions-210d
- 9 - Generate a random value : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-9-generate-a-random-value-5g8a
Top comments (0)