12

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?

2 Answers 2

18

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"
}
Sign up to request clarification or add additional context in comments.

3 Comments

I'm using this also in some setups. The only thing where I dislike it is that the computer executing Terraform needs network access to the database. Not always, what you/we want.
The shiny AWSy way would be probably to create a lambda that runs the code for you and has the correct security groups.. but well.. not too convincing as well.
Spent ages wondering why this didn't work for postgres (after changing all the other settings to match the postgresql provider) when using host = aws_db_instance.default.endpoint, but then realised that it should've been host = aws_db_instance.default.address.
3

The AWS API does not support what you want to do, therefore Terraform does not have it. Some possible workarounds:

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.

Have your application check for the databases and create them.

Create one cluster per database (which is generally how AWS wants things).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.