Terraform for GCP How to create Cloud Spanner SQL Database

Paul Ravvich
Terraform for the Google Cloud Platform
2 min readMay 5, 2024

--

Terraform for GCP How to create Cloud Spanner SQL Database

Hi, this is Paul, and welcome to the #26 part of my Terraform guide. Today we will discuss, how to create a Cloud Spanner using the Terraform script.

Google Cloud Spanner is a fully managed, horizontally scalable relational database that combines the benefits of traditional SQL relational databases with the scalability of NoSQL systems. It’s an ideal solution for global applications that require high availability and consistency on a global scale.

Add Role for Service Account

First, to create a Cloud PubSub you need to add a Role Cloud Spanner Admin to you're Service Account. How to add permissions we already discussed in this article:

Creating a Google Cloud Spanner Instance Using Terraform

The first block of Terraform code describes the creation of a Google Cloud Spanner instance.

resource "google_spanner_instance" "demo_spanner" {
name = "demospanner"
config = "regional-asia-south1"
display_name = "Demo Spanner"
num_nodes = 1
labels = {
"env" = "demo"
}
}

Parameters:

  • name: The unique name of the instance within the project and region.
  • config: The instance configuration, specifies the placement region. The value regional-asia-south1 refers to the South Asia-1 region.
  • display_name: A readable name for the instance that appears in the user interface.
  • num_nodes: The number of nodes in the instance. In this case, one node is created.
  • labels: A set of custom labels assigned to the instance, which can be used to organize and filter resources.

Creating a Spanner Database in the Instance Using Terraform

The second block of Terraform code describes the creation of a database within the previously created instance.

resource "google_spanner_database" "demo_db" {
name = "db"
instance = google_spanner_instance.demo_spanner.name
}

Parameters:

  • name: The name of the database within the Spanner instance.
  • instance: A reference to the name of the instance in which the database will be created. It uses the name attribute of the google_spanner_instance.demo_spanner resource, indicating that the database will be created in the instance established by the first code block.

Full code:

resource "google_spanner_instance" "demo_spanner" {
name = "demospanner"
config = "regional-asia-south1"
display_name = "Demo Spanner"
num_nodes = 1
labels = {
"env" = "demo"
}
}

resource "google_spanner_database" "demo_db" {
name = "db"
instance = google_spanner_instance.demo_spanner.name
}

This approach to the article helps readers understand how to use Terraform to manage Google Cloud infrastructure and provides practical examples that they can modify for their own needs.

Thank you for reading until the end. Before you go:

Paul Ravvich

--

--

Paul Ravvich
Terraform for the Google Cloud Platform

Software Engineer with over 10 years of XP. Join me for tips on Programming, System Design, and productivity in tech! New articles every Tuesday and Thursday!