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
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"
}
3 Comments
host = aws_db_instance.default.endpoint, but then realised that it should've been host = aws_db_instance.default.address.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).