Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provision multiple logical databases with Terraform on AWS RDS cluster instance

So, i've got Aurora MySql cluster with one RDS MySql instance provisioned. The obstacle occurs with the AWS underlying API allowing only for 1 logical DB to be created. Thus, I was wondering if any of you already had experience with such deployment coz I am running away from having to use Mysql client CLI for this step, would really like to automate it if possible. Any ideas?

like image 370
eastwood Avatar asked Sep 27 '18 17:09

eastwood


People also ask

How do I create a multiple database in RDS instance using terraform?

Have Terraform do a local-exec as part of the provisioning process. You can use this to call the SQL client to connect and create a second database. Have a Lambda function that connects to the RDS instance and sets up the database as you need. Trigger the Lambda after the RDS is deployed.

Can an RDS instance have multiple databases?

Amazon RDS currently supports MySQL, MariaDB, PostgreSQL, Oracle, Microsoft SQL Server, and Amazon Aurora database engines. When creating a DB instance, some database engines require that a database name be specified. A DB instance can host multiple databases, or a single Oracle database with multiple schemas.

How many databases can be created in AWS RDS?

By default, customers are allowed to have up to a total of 40 Amazon RDS DB instances. Of those 40, up to 10 can be Oracle or SQL Server DB instances under the "License Included" model. All 40 can be used for Amazon Aurora, MySQL, MariaDB, PostgreSQL, and Oracle under the "BYOL" model.


1 Answers

Terraform has a Myql provider https://www.terraform.io/docs/providers/mysql/index.html:

# Configure the MySQL provider
provider "mysql" {
  endpoint = "my-database.example.com:3306"
  username = "app-user"
  password = "app-password"
}

# Create a Database
resource "mysql_database" "app" {
  name = "my_awesome_app"
}

So you can create your AWS db cluster/instance and then use the mysql provider to create another db:

# Create a database server
resource "aws_db_instance" "default" {
  engine         = "mysql"
  engine_version = "5.6.17"
  instance_class = "db.t1.micro"
  name           = "initial_db"
  username       = "rootuser"
  password       = "rootpasswd"

  # etc, etc; see aws_db_instance docs for more
}

# Configure the MySQL provider based on the outcome of
# creating the aws_db_instance.
provider "mysql" {
  endpoint = "${aws_db_instance.default.endpoint}"
  username = "${aws_db_instance.default.username}"
  password = "${aws_db_instance.default.password}"
}

# Create a second database, in addition to the "initial_db" created
# by the aws_db_instance resource above.
resource "mysql_database" "app" {
  name = "another_db"
}
like image 125
Jonathan Avatar answered Nov 09 '22 23:11

Jonathan