Relational Database Service (RDS)

The “aws_db_instance” resource type provides an RDS instance resource. A DB instance is an isolated database environment in the cloud.

Example Usage

resource "aws_db_instance" "mydatabase" {
  allocated_storage = 10
  storage_type = "gp2"
  engine = "mysql"
  engine_version = "5.7"
  instance_class = "db.t2.micro"
  name = "mydb"
  username = "root"
  password = "Pa$#W0rD"
  parameter_group_name = "default.mysql5.7"
}

Following are the mandatory arguments :

  • allocated_storage
  • engine
  • instance_class
  • username
  • password

You can create MySQL, MariaDB, PostgreSQL, Oracle, SQL Server DB instances by mentioning engine type and engine_version.

To enable multi-AZ, use multi_az = true.

We can also add maintenance, backup windows and retention period using following attributes

  • backup_window
  • backup_retention_period
  • maintenance_window

There are other attributes to enable encryption, enable delete protection, to assign security groups and subnets and version upgrade etc..

Aurora Cluster

We can provision RDS Aurora Cluster with resource type “aws_rds_cluster” in terraform configuration. Also, to create cluster instances, we have another resource type “aws_rds_cluster_instance”.

Following are the mandatory arguments to create a cluter

  • master_password
  • master_username

Following are the mandatory arguments to create a cluter instances

  • instance_class
  • db_subnet_group_name (if publicly_accessible = false)
  • cluster_identifier 

Aurora cluster engine supports aurora, aurora-mysql, aurora-postgresql and default value is aurora. The engine_mode supported values are global, parallelquery, provisioned, serverless and the default value is provisioned.

Aurora MySQL 2.x (MySQL 5.7)

resource "aws_rds_cluster" "aurora-mysql" {
  cluster_identifier = "aurora-mysql-cluster"
  engine = "aurora-mysql"
  availability_zones = ["us-east-1a", "us-east-1b", "us-east-1c"]
  database_name = "aurora-mysql"
  master_username = "root"
  master_password = "Ro0t#$@19"
  backup_retention_period = 2
  preferred_backup_window = "02:00-03:00"
}

Aurora MySQL 1.x (MySQL 5.6)

resource "aws_rds_cluster" "aurora-default" {
  cluster_identifier = "aurora-cluster-default"
  availability_zones = ["us-east-1a", "us-east-1b", "us-east-1c"]
  database_name = "aurora-default"
  master_username = "root"
  master_password = "Ro0t#$@19"
  backup_retention_period = 2
  preferred_backup_window = "02:00-03:00"
}

Aurora with PostgreSQL engine

resource "aws_rds_cluster" "aurora-postgresql" {
  cluster_identifier = "aurora-postgresql-cluster"
  engine = "aurora-postgresql"
  availability_zones = ["us-east-1a", "us-east-1b", "us-east-1c"]
  database_name = "mydb-aurora-pg"
  master_username = "root"
  master_password = "Ro0t#$@19"
  backup_retention_period = 2
  preferred_backup_window = "01:00-02:00"
}

Aurora Cluster Instances

resource "aws_rds_cluster_instance" "cluster_instances" {
  count = 3
  identifier = "aurora-instance-${count.index}"
  cluster_identifier = "${aws_rds_cluster.aurora-default.id}"
  instance_class = "db.r4.large"
}

Add other arguments as per the requirements. Following are the arguments commonly used :

Cluster

  • deletion_protection
  • final_snapshot_identifier
  • availability_zones
  • backup_retention_period
  • preferred_backup_window
  • preferred_maintenance_window
  • vpc_security_group_ids
  • storage_encrypted
  • db_subnet_group_name
  • db_cluster_parameter_group_name
  • engine
  • engine_mode
  • engine_version
  • tags
  • enabled_cloudwatch_logs_exports
  • scaling_configuration ( if engine_mode = “serverless” )

Cluster Instances 

  • identifier
  • cluster_identifier
  • engine
  • engine_version
  • instance_class
  • publicly_accessible
  • db_subnet_group_name
  • db_parameter_group_name
  • monitoring_interval

 

Advertisements