Jump to Content
Developers & Practitioners

Provisioning Cloud Spanner using Terraform

September 13, 2021
Stefan Serban

Software Engineering Manager

Overview

Cloud Spanner is a fully managed relational database built for scale with strong consistency and up to 99.999% availability. Key features include the following: 

  • ACID transactions, SQL queries (ANSI 2011 with extensions) and global scale.
  • Automatic sharding - optimizes performance by automatically sharding the data based on request load and size of the data.
  • Fully managed - Synchronous replication and maintenance are handled automatically.
  • Flexible configurations - Depending on the workload, Cloud Spanner instances can be provisioned as either regional or multi-regional (spanning one continent or three continents).
  • Online schema Changes - Cloud Spanner users can make a schema change, whether it’s adding a column or adding an index, while serving traffic with zero downtime.

In this blog post, we will talk about how to deploy a sample application on Cloud Run with a Cloud Spanner backend using Terraform templates. We will also learn how to manage a production-grade Cloud Spanner instance using Terraform, by starting with a small instance and scaling up by adding more nodes or processing units.

Terraform for Cloud Spanner 

What is Terraform?

Terraform is a popular open-source infrastructure-as-code tool developed by HashiCorp, that provides a consistent CLI to manage hundreds of cloud services. It codifies cloud APIs into declarative configuration files. Terraform allows you to declare an entire GCP environment as code, which can then be version-controlled.

Benefits of using Terraform with Cloud Spanner

Terraform is very useful for provisioning scalable Cloud Spanner instances and databases in real-world, production environments. It allows for easier configuration management and version control and enables repeatability across regions and projects.

Organizations using Terraform to manage their cloud infrastructure today can easily include Cloud Spanner into their existing infrastructure-as-code framework. 

The Cloud Spanner with Terraform codelab offers an excellent introduction to the provisioning of instances, creating and modifying databases, and scaling a Cloud Spanner instance with more nodes. The codelab is a great way to get started. In the next few paragraphs, we discuss a few details that go beyond what we cover in the codelab.

Specifying a regional configuration

Cloud Spanner instances can be launched either regionally or in a multi-region. Take a look at Instances in the official documentation (under “regional configurations” and “multi-region configurations”) for a list of the configuration options available.

For regional instances, add the regional- prefix to the instance names in the list below. Example: to provision an instance in Montreal, your Terraform  template will have  config = "regional-northamerica-northeast1"

For multi-region instances, you can use the region name as-is from the documentation. Example - asia1 for a one-continent instance in Asia or nam-eur-asia1 for a three-continent instance.  

Modifying Cloud Spanner instance properties

Note that properties such as the amount of compute capacity (via num_nodes or processing_units), display name, and labels can be modified when making changes to an existing Cloud Spanner instance via Terraform. However, changing the instance name (the instance identifier on the GCP console) will result in the cluster being destroyed and recreated.

Loading...

As mentioned above, compute capacity (a.k.a. the instance size) can also be defined in terms of processing_units. For example, the compute capacity defined below would be 1/10th of a node, while a value of 1000 would be one full node.

Loading...

Creating databases and executing DDL commands

Finally, Cloud Spanner Terraform resources also support creating databases and executing DDL as part of the template. 

Loading...

The DDL support in the Terraform resource is a useful feature for initializing a database schema for smaller applications. 

Limitations of Terraform with Cloud Spanner

Managing database schemas

Database schemas tend to change periodically. Terraform has very limited support database schema updates - changes to the DDL, particularly those that are not append-only, will require dropping and re-creating the database. Therefore, we don’t recommend using Terraform to manage the schemas of Cloud Spanner databases. Instead, we recommend using a schema versioning tool like Liquibase.  A Liquibase extension with Cloud Spanner support was released recently under the Cloud Spanner Ecosystem. Here is the official documentation

Deploying a sample app

For demonstration purposes, we’re going to use a sample stock price visualization app called OmegaTrade. This application stores the stock prices in Cloud Spanner and renders visualizations using Google Charts

To learn more about the app and its integration with Cloud Spanner, see this blog post. 

Now, to the fun part! We will deploy this application to Cloud Run using Terraform templates. We chose Cloud Run because it abstracts away infrastructure management and scales up or down automatically almost instantaneously depending on traffic. Let’s get started!

As prerequisites, please ensure that you have:

  • Access to a new or existing GCP project with one of the sets of roles listed below:

    • Owner

    • Editor + Cloud Run Admin + Storage Admin 

    • Cloud Run Admin + Service Usage Admin + Cloud Spanner Admin + Storage Admin

  • Enabled billing on the above GCP project.

  • Installed and initialized the Google Cloud SDK.

  • Installed and configured Docker on your machine.

  • Installed and configured Git on your machine.

NOTE - Please ensure that your permissions are not restricted by any organizational policies.

We are deploying this application using Cloud Shell. If you are going through these steps on your local machine, and assuming you have already installed the Cloud SDK, you can execute the following command to authenticate.

gcloud auth application-default login

Choose your Google account with access to the required GCP project and enter the Project ID when prompted. 

Next, we need to ensure the gcloud configuration is set up correctly. You may want to start with a new configuration by using the create command. Below, we are enabling authentication, unsetting any API endpoint URL set previously, and setting the GCP project we intend to use in the default gcloud configuration. Replace [Your-Project-ID] below with the ID of your GCP project.

Loading...

Before continuing further, let’s make sure our Terraform version is up to date (we need Terraform version 0.13.1 and above).

Loading...

If needed, you can go to the official Terraform download page to download and install the latest version.

Next, let’s enable Google Cloud APIs for Cloud Spanner, Container Registry, and Cloud Run. Note: you could also use Terraform to accomplish this, instead of running the commands manually.

Loading...

Now, let’s clone the repository that contains Terraform modules for Cloud Run, Cloud Spanner and GCE (for a Cloud Spanner emulator instance, which will be discussed in a future blog post). 

Loading...

Take a look at the directory structure below. 

Loading...

The examples folder has Cloud Spanner and Cloud Run Terraform examples, with their corresponding Terraform modules located in the modules folder.  

Note: By default we have defined the compute capacity in terms of number of nodes in these templates. In case you want to define your compute capacity using processing units instead of nodes, you will need to uncomment the lines of code for processing_units in each of the examples and modules, and comment out the corresponding lines for num_nodes. The two options are mutually exclusive. To find all the relevant files to modify, you can execute

Loading...

Note that as of the time of this writing, the smallest compute capacity available is 100 processing units (or 1/10th of a node).

Launching Cloud Spanner 

We will now launch a single-node Cloud Spanner instance in the us-west1 region. The template also creates a database and the necessary tables for the OmegaTrade application using the DDL specified in the template.

Loading...

Take a look at the terraform.tfvars file to customize the compute capacity, region, and number of nodes or processing units. 

Loading...

Enter your GCP project ID (without the square brackets), make any other changes you’d like, and save.

Loading...

In case you want to define your compute capacity using processing units instead of nodes, you can specify a value for spanner_processing_units instead of spanner_nodes and follow the instructions at the end of the previous section. 

Next, let’s initialize Terraform, and make sure that we have the correct versions of the providers installed

Loading...

Analyze the execution plan,

Loading...

And apply the changes.

Loading...

Terraform will ask for your confirmation before applying.

https://storage.googleapis.com/gweb-cloudblog-publish/images/image2_kOjhjNm.max-500x500.png

You should see the Cloud Spanner instance successfully provisioned .

https://storage.googleapis.com/gweb-cloudblog-publish/images/image3_Kpny9b6.max-800x800.jpg

Click into the hamburger menu at the top left and select Spanner to verify the outcome on the GCP console. 

https://storage.googleapis.com/gweb-cloudblog-publish/images/image8_PJ7OHs6.max-900x900.jpg
https://storage.googleapis.com/gweb-cloudblog-publish/images/image4_uFTOLaQ.max-1700x1700.png

You can find the instance and database created, along with the necessary tables for the OmegaTrade application. 

The above example provisioned a single node instance in the us-west1 region. If you would like to launch in a different region (different instance configuration) with multiple nodes, simply edit the terraform.tfvars file and set a different instance configuration instead of  spanner_config = "regional-us-west1".  

Deploying the backend to Cloud Run

We are going to be deploying two different services to Cloud Run: omegatrade/frontend and omegatrade/backend.
Loading...

NOTE - Please ensure that your permissions are not restricted by any organizational policies, or you may run into an IAM-related issue at the apply stage later on.

Now that the Cloud Spanner instance, database, and tables are in place, let’s build and deploy the backend service to Cloud Run. The frontend has a dependency on the backend URL, so we will start with the backend.

Loading...

In the backend folder, we will create a .env file and insert some seed data into the database we created in the previous section. We begin by setting the gcp-project-id, spanner-instance-id, and spanner-database-id to the appropriate values that we got from the GCP console (omitting the square brackets).

Loading...

Then, we run the following command to populate the seed data.

Loading...

Next, we build the image from the dockerfile and push it to GCR. We will change the commands below to reflect our GCP project ID and run them.

Loading...

We will now go back to the Terraform examples directory and provision the backend service of the OmegaTrade application.

Loading...

Like the Cloud Spanner example you have seen in the previous section, you can quickly edit the terraform.tfvars file to make changes according to your environment and deploy.  Since the Terraform template adds a suffix to the instance name and DB name, you might want to get the exact instance name and database name from the GCP console. The  backend_container_image_path is the same path that you used in the docker push command above.

NOTE: In these templates, we follow the standard practice of using variables.tf or tfvars files to define variables and values. This is particularly useful when we have multiple resources with similar configuration, as while upgrading them the values need to be changed in only one place.

Loading...

Here is how my file looks with all the details except the project ID filled in.

Loading...

Next, let’s initialize Terraform and validate the plan.

Loading...

Now, we’re ready to deploy backend service. If you get an IAM-related error at this stage, it is likely because of an organizational policy of the organization that the project is hosted in. You may need to contact your organization admin or start over with a project in a different organization that does not have this restriction.

Loading...

Terraform will ask for your confirmation before applying. 

https://storage.googleapis.com/gweb-cloudblog-publish/images/image2_AZaiKAQ.max-500x500.png

You should now see the backend service up and running.

https://storage.googleapis.com/gweb-cloudblog-publish/images/image5_vg7UxPZ.max-900x900.jpg

Check Cloud Run in the GCP console and locate the new service. 

https://storage.googleapis.com/gweb-cloudblog-publish/images/image11_Ztvw9Zn.max-800x800.jpg

Write down its URL. We will use it in the frontend configuration in the next section. 

https://storage.googleapis.com/gweb-cloudblog-publish/images/image10_sdontVW.max-800x800.png

Deploying the frontend to Cloud Run

Before we build the frontend service, we need to update the following file from the repo with the backend URL we got from the above step. 

Loading...

Change the base URL to the backend URL noted down in the previous section.

Loading...

In the frontend folder, build the frontend service and push the image to GCR.

Loading...

Go to the Terraform example for CloudRun (frontend service).

Loading...

We will now provision the frontend service using the image we pushed to GCR above. Open the terraform.tfvars file and add the GCP project ID and frontend image path.

Loading...

Here is what the file should look like after filling in most of the details.

Loading...

Now, let’s deploy the frontend service.

Loading...

Once again, Terraform will ask for your confirmation before applying. 

https://storage.googleapis.com/gweb-cloudblog-publish/images/image2_AZaiKAQ.max-500x500.png

You should now see the frontend service up and running.

https://storage.googleapis.com/gweb-cloudblog-publish/images/image3_Kpny9b6.max-800x800.jpg

Check the services on the GCP console.

https://storage.googleapis.com/gweb-cloudblog-publish/images/image7_bWfbXID.max-800x800.jpg

You will now be able to go to the frontend URL and interact with the application! You can interact with existing visualizations or simulate write activity on Cloud Spanner by visiting the Manage Simulations view in the application. Choose an existing company or add a new company, and choose an interval and number of records. 

Scaling Cloud Spanner using Terraform

To scale the Cloud Spanner instance up or down, go back to the Cloud Spanner Terraform examples folder.

Loading...

We currently have a single node Cloud Spanner instance in the us-west1 region. For production environments, this configuration may not be sufficient. Scaling Cloud Spanner can be achieved using our Terraform template.

Take a look at the terraform.tfvars file. We chose a regional instance with 1 node during the initial provisioning, running in the us-west1 region. We can now scale Cloud Spanner to the compute capacity necessary by running a terraform apply once again with an updated node count.  

An example .tfvars file to scale the instance is available as terraform.scale.tfvars as shown below. Specify the same instance ID and database name as initially used. Note that the Terraform template randomizes the names of the instance and database by adding a random suffix, but while scaling you just need to use the original names. 

Edit the terraform.scale.tfvars file with your project ID

Loading...

In case you defined your compute capacity using processing units instead of nodes, you can follow the same steps to resize it, by specifying an updated value for spanner_processing_units instead of spanner_nodes and commenting/uncommenting the appropriate lines in the script as noted just above the Launching Cloud Spanner section earlier in this post.

Apply changes to scale the instance from 1 node to 2 nodes.

Loading...

It may take a few seconds for the changes to take effect. 

https://storage.googleapis.com/gweb-cloudblog-publish/images/image1_Rh9nf3H.max-500x500.jpg

Verify the changes on the GCP console

https://storage.googleapis.com/gweb-cloudblog-publish/images/image13_YWvXLD1.max-1500x1500.png

Conclusion

We have seen how easy it is to provision Cloud Spanner instances and create databases and tables using Terraform using a sample application deployment. We have also seen how to scale Cloud Spanner using Terraform after the initial deployment. Armed with this knowledge, you are ready to try out the code modules in this repository and set up your own Cloud Spanner instances with Terraform.


To learn more about using Terraform with Cloud Spanner, visit

Posted in