Google Cloud Spanner Database

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


Fix issues in your cloud & app configurations

Test for misconfigurations of this resource in your cloud.


Terraform Example (google_spanner_database)

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.


  • ddl optional - list / 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.

  • deletion_protection optional - bool
  • id optionalcomputed - string
  • instance required - string
    • The instance to create the database on.

  • name required - string
    • 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].

  • project optionalcomputed - string
  • state requiredcomputed - string
    • An explanation of the status of the database.

Example Usage (from GitHub)

resource "google_spanner_database" "db1"{
    name = "db1"
    instance =
resource "google_spanner_database" "database" {
  instance            =
  name                = var.config["name"]
  ddl                 = var.databases
  deletion_protection = false
resource "google_spanner_database" "database" {
  project  = var.project_id
  instance = var.spanner_instance_name
  name     =
  ddl      = var.ddl

resource "google_spanner_database" "database" {
    instance =
    name = var.config["name"]
    ddl = var.databases
    deletion_protection = false
resource "google_spanner_database" "database" {
    instance =
    name = var.config["name"]
    ddl = var.databases
    deletion_protection = false

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.