Instrukcje i wymagania dotyczące konfiguracji modułu
Chroń swoje konto i postępy. Zawsze używaj okna przeglądania prywatnego i danych logowania do modułu, kiedy go uruchamiasz.

Creating a Remote Backend

Laboratorium 30 godz. universal_currency_alt Punkty: 5 show_chart Wprowadzające
info Ten moduł może zawierać narzędzia AI, które ułatwią Ci naukę.
Te treści nie są jeszcze zoptymalizowane pod kątem urządzeń mobilnych.
Dla maksymalnej wygody odwiedź nas na komputerze, korzystając z linku przesłanego e-mailem.

Overview

In this lab, you will create a local backend and then create a Cloud Storage bucket to migrate the state to a remote backend

Objectives

In this lab, you will learn how to perform the following tasks:

  • Create a local backend.
  • Create a Cloud Storage backend.
  • Refresh your Terraform state.

Task 1. Sign in to the Cloud console

For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Cloud console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).

    The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username below and paste it into the Sign in dialog.

    {{{user_0.username | "Username"}}}

    You can also find the Username in the Lab Details panel.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

    {{{user_0.password | "Password"}}}

    You can also find the Password in the Lab Details panel.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  7. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Google Cloud console opens in this tab.

Note: To view a menu with a list of Google Cloud products and services, click the Navigation menu at the top-left, or type the service or product name in the Search field. Navigation menu icon

Task 2. Verify Terraform is installed

  1. On the Google Cloud menu, click Activate Cloud Shell.

  2. If prompted, click Continue.

  3. Confirm that Terraform is installed by running the following command:

terraform --version

Task 3. Add a local backend

In this section, you will configure a local backend which will then be moved to a Cloud Storage bucket.

  1. In a new Cloud Shell window, create a main.tf configuration file.
touch main.tf
  1. To retrieve your Project ID, run the following command:
gcloud config list --format 'value(core.project)'
  1. On the Cloud Shell toolbar, click Open Editor. To switch between Cloud Shell and the code editor, click Open Editor or Open Terminal as required.
  2. Copy the Cloud Storage bucket resource code into your main.tf configuration file:
provider "google" { project = "{{{project_0.project_id|Project ID}}}" region = "{{{project_0.default_region|Region}}}" } resource "google_storage_bucket" "test-bucket-for-state" { name = "{{{project_0.project_id|Project ID}}}" location = "US" # Replace with EU for Europe region uniform_bucket_level_access = true }
  1. Add a local backend to your main.tf file:
terraform { backend "local" { path = "terraform/state/terraform.tfstate" } }

This will reference a terraform.tfstate file in the terraform/state directory.

The final code in main.tf is as shown below.

provider "google" { project = "{{{project_0.project_id|Project ID}}}" region = "{{{project_0.default_region|Region}}}" } resource "google_storage_bucket" "test-bucket-for-state" { name = "{{{project_0.project_id|Project ID}}}" location = "US" # Replace with EU for Europe region uniform_bucket_level_access = true } terraform { backend "local" { path = "terraform/state/terraform.tfstate" } }

Terraform must initialize any configured backend before use.

  1. On the Cloud Shell toolbar, click Open Terminal, then initialize Terraform using the following command:
terraform init
  1. Apply the changes. Type yes at the prompt to confirm.
terraform apply

The Cloud Shell Editor should now display the state file called terraform.tfstate in the terraform/state directory.

  1. Examine your state file:
terraform show

Your google_storage_bucket.test-bucket-for-state resource should be displayed.

Click Check my progress to verify local backend is created.

Add a local backend

Task 4. Add a Cloud Storage backend

  1. Navigate back to your main.tf file in the editor. You will now replace the current local backend with a gcs backend.
  2. To change the existing local backend configuration, replace the code for local backend with the following configuration in the main.tf file.
terraform { backend "gcs" { bucket = "{{{project_0.project_id|Project ID}}}" prefix = "terraform/state" } }

The final code in main.tf is as shown below:

provider "google" { project = "{{{project_0.project_id|Project ID}}}" region = "{{{project_0.default_region|Region}}}" } resource "google_storage_bucket" "test-bucket-for-state" { name = "{{{project_0.project_id|Project ID}}}" location = "US" # Replace with EU for Europe region uniform_bucket_level_access = true } terraform { backend "gcs" { bucket = "{{{project_0.project_id|Project ID}}}" prefix = "terraform/state" } }
  1. Initialize your backend again. Type yes at the prompt to confirm.
terraform init -migrate-state
  1. In the Google Cloud console, in the Navigation menu, click Cloud Storage and then Buckets.
  2. Click on your bucket and navigate to the file terraform/state/default.tfstate.

Your state file now exists in a Cloud Storage bucket!

remote_backend

Click Check my progress to verify remote backend is created.

Add a Cloud Storage backend

Task 5. Refresh the state

The terraform refresh command is used to reconcile the state Terraform knows about (via its state file) with the real-world infrastructure. This can be used to detect any drift from the last-known state and to update the state file. This does not modify infrastructure, but does modify the state file. If the state is changed, this may cause changes to occur during the next plan or apply.

  1. Return to your storage bucket in the Cloud console. Select the check box next to the name, and click the Labels button on the top. The info panel with Labels tabs will open up.
  2. Click +ADD LABEL. Set the Key2 = key and Value2 = value.
  3. Click Save.
  4. Return to Cloud Shell and use the following command to update the state file:
terraform refresh

Click Check my progress to verify terraform is refreshed.

Refresh the state

Task 6. Clean up the workspace

  1. First, revert your backend to local so you can delete the storage bucket. Copy and replace the gcs configuration with the following:
terraform { backend "local" { path = "terraform/state/terraform.tfstate" } }
  1. Initialize the local backend again. Type yes at the prompt to confirm.
terraform init -migrate-state
  1. In the main.tf file, add the force_destroy = true argument to your google_storage_bucket resource. When you delete a bucket, this boolean option will delete all contained objects.
Note: If you try to delete a bucket that contains objects, Terraform will fail that run.

Your resource configuration should resemble the following:

resource "google_storage_bucket" "test-bucket-for-state" { name = "{{{project_0.project_id|Project ID}}}" location = "US" # Replace with EU for Europe region uniform_bucket_level_access = true force_destroy = true }
  1. Apply the changes. Type yes at the prompt to confirm.
terraform apply
  1. You can now successfully destroy your infrastructure. Type yes at the prompt to confirm.
terraform destroy

Click Check my progress to verify the backend is deleted.

Clean up the workspace

Congratulations!

In this lab, you learned how to manage backends and state with Terraform. You created local and Cloud Storage backends to manage your state file, and also refreshed the state. In this lab, you learned how to perform the following tasks:

  • Create a local backend.
  • Create a Cloud Storage backend.
  • Refresh your Terraform state.

End your lab

When you have completed your lab, click End Lab. Google Skills removes the resources you’ve used and cleans the account for you.

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 2026 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.

Zanim zaczniesz

  1. Moduły tworzą projekt Google Cloud i zasoby na określony czas.
  2. Moduły mają ograniczenie czasowe i nie mają funkcji wstrzymywania. Jeśli zakończysz moduł, musisz go zacząć od początku.
  3. Aby rozpocząć, w lewym górnym rogu ekranu kliknij Rozpocznij moduł.

Użyj przeglądania prywatnego

  1. Skopiuj podaną nazwę użytkownika i hasło do modułu.
  2. Kliknij Otwórz konsolę w trybie prywatnym.

Zaloguj się w konsoli

  1. Zaloguj się z użyciem danych logowania do modułu. Użycie innych danych logowania może spowodować błędy lub naliczanie opłat.
  2. Zaakceptuj warunki i pomiń stronę zasobów przywracania.
  3. Nie klikaj Zakończ moduł, chyba że właśnie został przez Ciebie zakończony lub chcesz go uruchomić ponownie, ponieważ spowoduje to usunięcie wyników i projektu.

Ta treść jest obecnie niedostępna

Kiedy dostępność się zmieni, wyślemy Ci e-maila z powiadomieniem

Świetnie

Kiedy dostępność się zmieni, skontaktujemy się z Tobą e-mailem

Jeden moduł, a potem drugi

Potwierdź, aby zakończyć wszystkie istniejące moduły i rozpocząć ten

Aby uruchomić moduł, użyj przeglądania prywatnego

Najlepszym sposobem na uruchomienie tego laboratorium jest użycie okna incognito lub przeglądania prywatnego. Dzięki temu unikniesz konfliktu między swoim kontem osobistym a kontem do nauki, co mogłoby spowodować naliczanie dodatkowych opłat na koncie osobistym.