Creating Spanner Instances and Databases (CLI and Terraform)
SCBL002
Overview
In this lab, you automate the creation of Spanner instances and databases using the Google Cloud SDK, the Command Line Interface (CLI), and Terraform.
Objectives
In this lab, you learn how to:
- Create instances and databases using the gcloud CLI.
- Automate Spanner infrastructure using Terraform.
Setup and Requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.
This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
Activate Cloud Shell
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
- Click Activate Cloud Shell at the top of the Google Cloud console.
When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
- (Optional) You can list the active account name with this command:
-
Click Authorize.
-
Your output should now look like this:
Output:
- (Optional) You can list the project ID with this command:
Output:
Example output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
Task 1. Create instances and databases using the gcloud CLI
-
On the Google Cloud Console title bar, click Activate Cloud Shell (). If prompted, click Continue.
-
Run the following command to set your project ID:
- From the Cloud Shell prompt, run the following command to create a Spanner instance named test-spanner-instance.
Note the parameters for Spanner configuration and capacity. If you are asked to Authorize the command, then do so.
- The command should not take long. In the Console, navigate to the Spanner service and verify the instance was created.
To see the instance, you could also run the command below. Try that now.
- Before creating the Pets database, you need a file that contains the DDL code. Type the following command to create the file and open it in the nano code editor.
- Paste the following code into Nano. Type
Ctrl+X
, thenY
, and then press the ENTER key to save the file.
- Now that you have the Schema file, run the following command to create the database.
- Insert an Owner and all of the dogs owned. The primary keys for Owners and Pets use UUIDs. Enter the following command to create a UUID for the owner and store it in a variable.
- Insert the Owner Doug. Note: the
--data
parameter that allows you to pass the fields in name-value pairs.
- Insert all of Doug's dogs with the following commands.
- Let's see if it worked. Run the following query.
-
You can also go to the Console and view the data. Choose Spanner from the product list. Then select test-spanner-instance > pets-db (under Databases) > Pets (under Tables) > Data from the left menu.
-
Delete the database with the following command.
-
In the Console, verify the database was deleted.
-
Lastly, delete the instance with the following command.
Note: The --quiet
parameter runs the command without prompting the user. This could have been added to the prior command as well. This is useful if you are writing an automated pipeline and there would be no user to ask.
- In the Console, verify that the instance was deleted.
Task 2. Automate Spanner infrastructure using Terraform
- Create a folder for your Terraform files and change to it using the following commands.
- You need a number of files for the Terraform module. Run the following command to create the empty files.
- Click the Open Editor button to open the code editor. From the Explorer pane on the left, find the
terraform-spanner
folder you just created and expand it. Select theprovider.tf
file to open it in the editor and add the following code to it.
Note: The code in the Terraform block downloads the Google provider from Hashicorp's website. The code in the provider block configures the provider to use the correct Project ID and Region which you set as variables later.
- Open the file
main.tf
and add the following resource block. This code creates the Spanner instance.
- In the same file, below the previous code, add the following which creates the Pets database. Note the DDL code which defines the tables.
- Open the file
variables.tf
. In this file, you declare the variables used in the Terraform module. Add the following code.
- All the variables except for
project_id
andregion
have defaults. You use theterraform.tfvars
file to set those variable values. Open that file and add the following.
- Let's see if it works. Click the Open Terminal button. Note, you may have to switch back to the original tab first if your editor opened a new tab or window. At the command prompt, enter the following.
- Assuming there were no errors with the previous command, enter the following and analyze the output. It should say that two resources will be added.
- Lasty, enter the following command to create the Spanner instance and Pets database. You must type
yes
when prompted.
-
Wait for the Terraform command to complete. In the Console, navigate to the Spanner service and verify that the instance and database were created. There is no refresh button, so you may need to click on another product and then return to Spanner to refresh the instances list.
-
Return to the terminal and enter the following command to delete the Spanner instance.
Congratulations! You have automated the creation of Spanner instances and databases using the Google Cloud SDK, the Command Line Interface (CLI), and Terraform.
End your lab
When you have completed your lab, click End Lab. Your account and the resources you've used are removed from the lab platform.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
- 1 star = Very dissatisfied
- 2 stars = Dissatisfied
- 3 stars = Neutral
- 4 stars = Satisfied
- 5 stars = Very satisfied
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2023 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.