(Terraform) Error 400: Invalid request: instance name (pg_instance)., invalid

On GCP, I'm trying to create a Cloud SQL instance with this Terraform code below:

resource "google_sql_database_instance" "postgres" {
    name                = "pg_instance"
    database_version    = "POSTGRES_13"
    region              = "asia-northeast1"
    deletion_protection = false

    settings {
        tier      = "db-f1-micro"
        disk_size = 10
    }
}

resource "google_sql_user" "users" {
    name     = "postgres"
    instance = google_sql_database_instance.postgres.name
    password = "admin"
}

But I got this error:

Error: Error, failed to create instance pg_instance: googleapi: Error 400: Invalid request: instance name (pg_instance)., invalid

Are there any mistakes for my Terraform code?

1 answer

  • answered 2022-05-07 01:31 Kai - Kazuya Ito

    For a Cloud SQL instance name, only lowercase letters, numbers, and hyphens are allowed and it must start with a letter.

    So, change this:

    name = "pg_instance"
           # Underscore "_" is not allowed
    

    To this:

    name = "pg-instance"
           # Hyphen "-" is allowed
    

    In addition, on GUI, you can see this message below under the blank for instance ID("name" in Terraform):

    Use lowercase letters, numbers, and hyphens. Start with a letter.

    enter image description here

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum