Search code examples
databaseamazon-web-servicesterraformamazon-auroraterraform-provider-aws

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?


Solution

  • Terraform has a Mysql 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 database:

    # 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 the second database beside "initial_db"
    # using the aws_db_instance resource above.
    resource "mysql_database" "app" {
      name = "another_db"
    }