Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: random_id for mysql instance name #116

Conversation

konstantin-recurly
Copy link
Contributor

There is an issue if you would like to create a DB with the same name. With the current approach, you need to change the name every time or you have to work around and use something like random_id

If I delete my instance, can I reuse the instance name?
Yes, but not right away. The instance name is unavailable for up to a week before it can be reused.

More information is here Managing Your Instances

@konstantin-recurly konstantin-recurly changed the title Added: random_id for cloudsql Added: random_id for mysql instance name May 22, 2020
@morgante
Copy link
Contributor

I'm not sure we want to include this in the module. While it's indeed necessary for recreating instances after deletion, it's also following a very specific pattern which doesn't necessarily match what everyone wants.

@konstantin-recurly
Copy link
Contributor Author

Maybe, it would be nice to have that as an option. Like

resource "random_id" "prefix" {
  byte_length = 4
}

resource "google_sql_database_instance" "default" {
  provider            = google-beta
  project             = var.project_id
  name                = var.instance_name_prefix  ? "${var.name}-${random_id.prefix.hex}"  : var.name

How does it sound to you?

@morgante
Copy link
Contributor

Yeah I think we could make it optional with a random_instance_name boolean variable.

@konstantin-recurly
Copy link
Contributor Author

@morgante Updated the PR

Copy link
Contributor

@morgante morgante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, just some nits. It'd also be nice to have this added to all the submodules.

modules/mysql/variables.tf Outdated Show resolved Hide resolved
modules/mysql/main.tf Outdated Show resolved Hide resolved
Copy link
Contributor

@morgante morgante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are failing as you need to index for the random ID:

Error: Missing resource instance key

  on ../../../modules/mssql/main.tf line 43, in resource "google_sql_database_instance" "default":
  43:   name             = var.random_instance_name ? "${var.name}-${random_id.suffix.hex}" : var.name

Because random_id.suffix has "count" set, its attributes must be accessed on
specific instances.

Copy link
Contributor

@morgante morgante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution.

@morgante morgante merged commit 7c8c799 into terraform-google-modules:master May 28, 2020
@konstantin-recurly
Copy link
Contributor Author

One more thing that I have noticed. Seems that now we don't need this part in examples
https://github.com/terraform-google-modules/terraform-google-sql-db/blob/master/examples/mysql-public/main.tf#L37-L44

resource "random_id" "suffix" {
  byte_length = 5
}

locals {
  /*
    Random instance name needed because:
    "You cannot reuse an instance name for up to a week after you have deleted an instance."
    See https://cloud.google.com/sql/docs/mysql/delete-instance for details.
  */
  instance_name = "${var.db_name}-${random_id.suffix.hex}"
}

Now we should be able to use this

module "safer-mysql-db" {
  source           = "../../modules/mysql"
  name             = var.db_name
  random_instance_name = true
  database_version = "MYSQL_5_6"
  project_id = var.project_id

@morgante
Copy link
Contributor

Indeed! Looks like that example could be cleaned up.

We can also remove random_Id from safer-mysql and just pass through the random_instance_name variable: https://github.com/terraform-google-modules/terraform-google-sql-db/blob/master/modules/safer_mysql/main.tf#L16

Do you mind doing a follow-up PR to fix those issues?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants