Google Cloud Spanner Database

This page shows how to write Terraform for Cloud Spanner Database and write them securely.

google_spanner_database (Terraform)

The Database in Cloud Spanner can be configured in Terraform with the resource name google_spanner_database. The following sections describe 5 examples of how to use the resource and its parameters.

Example Usage from GitHub

main.tf#L13
resource "google_spanner_database" "db1"{
    name = "db1"
    instance =  google_spanner_instance.spanner_tf.name
cloudspanner.tf#L6
resource "google_spanner_database" "database" {
  instance            = google_spanner_instance.main.name
  name                = var.config["name"]
  ddl                 = var.databases
  deletion_protection = false
}
main.tf#L1
resource "google_spanner_database" "database" {
  project  = var.project_id
  instance = var.spanner_instance_name
  name     = var.name
  ddl      = var.ddl

spanner.tf#L5
resource "google_spanner_database" "database" {
    instance = google_spanner_instance.main.name
    name = var.config["name"]
    ddl = var.databases
    deletion_protection = false
}
spanner.tf#L5
resource "google_spanner_database" "database" {
    instance = google_spanner_instance.main.name
    name = var.config["name"]
    ddl = var.databases
    deletion_protection = false
}

Review your Terraform file for Google best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

  • ddl optional - list of string

An optional list of DDL statements to run inside the newly created database. Statements can create tables, indexes, etc. These statements execute atomically with the creation of the database: if there is an error in any statement, the database is not created.

The instance to create the database on.

A unique identifier for the database, which cannot be changed after the instance is created. Values are of the form [a-z][-a-z0-9]*[a-z0-9].

An explanation of the status of the database.

Explanation in Terraform Registry

A Cloud Spanner Database which is hosted on a Spanner instance. To get more information about Database, see:

  • API documentation
  • How-to Guides
    • Official Documentation

      Warning: On newer versions of the provider, you must explicitly set deletion_protection=false (and run terraform apply to write the field to state) in order to destroy an instance. It is recommended to not set this field (or set it to true) until you're ready to destroy. On older versions, it is strongly recommended to set lifecycle { prevent_destroy = true } on databases in order to prevent accidental data loss. See Terraform docs for more information on lifecycle parameters.

Frequently asked questions

What is Google Cloud Spanner Database?

Google Cloud Spanner Database is a resource for Cloud Spanner of Google Cloud Platform. Settings can be wrote in Terraform.

Where can I find the example code for the Google Cloud Spanner Database?

For Terraform, the ankit25587/course-material, farrukh90/gcp and bhupeshdavesar/kpmg source code examples are useful. See the Terraform Example section for further details.

security-icon

Automate config file reviews on your commits

Fix issues in your infrastructure as code with auto-generated patches.