arrow_back

Terraform Essentials: Google Cloud Storage Bucket

Anmelden Teilnehmen
Zugriff auf über 700 Labs und Kurse nutzen

Terraform Essentials: Google Cloud Storage Bucket

Lab 30 Minuten universal_currency_alt 1 Guthabenpunkt show_chart Einsteiger
info Dieses Lab kann KI-Tools enthalten, die den Lernprozess unterstützen.
Zugriff auf über 700 Labs und Kurse nutzen

gem-terraform-gcs-create

Google Cloud self-paced labs logo

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.

  1. Click Activate Cloud Shell Activate Cloud Shell icon 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:

Your Cloud Platform project in this session is set to YOUR_PROJECT_ID

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

  2. Your output should now look like this:

Output:

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core] project = <project_ID>

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Overview

This lab demonstrates how to create a Google Cloud Storage bucket using Terraform. You will define the bucket resource in a Terraform configuration file, initialize Terraform, and apply the configuration to create the bucket in your Google Cloud project. This lab assumes you have a basic familiarity with Google Cloud and Terraform concepts.

Task 1. Configure Google Cloud SDK

Before you begin, configure the Google Cloud SDK to interact with your Google Cloud project.

  1. Set your Project ID:

    gcloud config set project {{{ project_0.project_id | "PROJECT_ID" }}} Note:
    This command sets your active project.
  2. Set your default region:

    gcloud config set compute/region {{{ project_0.default_region | "REGION" }}} Note:
    This command sets your active compute region.
  3. Set your default zone:

    gcloud config set compute/zone {{{ project_0.default_zone | "ZONE" }}} Note:
    This command sets your active compute zone.

Task 2. Create Cloud Storage Bucket for Terraform State

Terraform uses a state file to track the resources it manages. For collaboration and persistence, it's best to store this state file in a remote backend like Google Cloud Storage.

  1. Create the Cloud Storage bucket:

    gcloud storage buckets create gs://{{{ project_0.project_id | "PROJECT_ID" }}}-tf-state --project={{{ project_0.project_id | "PROJECT_ID" }}} --location={{{ project_0.default_region | "REGION" }}} --uniform-bucket-level-access Note:
    This command creates a Cloud Storage bucket in the specified region to store the Terraform state file.
  2. Enable versioning on the GCS bucket:

    gsutil versioning set on gs://{{{ project_0.project_id | "PROJECT_ID" }}}-tf-state Note:
    This command enables object versioning on the state bucket.

Task 3. Create a Terraform Configuration File

Now, define the Terraform configuration to create the resource.

  1. Create a new directory for your Terraform configuration files.

    mkdir terraform-gcs && cd $_ Note:
    This creates a new directory and changes the current directory to it.
  2. Create main.tf with the following content:

    terraform { required_providers { google = { source = "hashicorp/google" version = "~> 4.0" } } backend "gcs" { bucket = "{{{ project_0.project_id | "PROJECT_ID" }}}-tf-state" prefix = "terraform/state" } } provider "google" { project = "{{{ project_0.project_id | "PROJECT_ID" }}}" region = "{{{ project_0.default_region | "REGION" }}}" } resource "google_storage_bucket" "default" { name = "{{{ project_0.project_id | "PROJECT_ID" }}}-my-terraform-bucket" location = "{{{ project_0.default_region | "REGION" }}}" force_destroy = true storage_class = "STANDARD" versioning { enabled = true } } Note:
    This configuration defines a Google Cloud Storage bucket in the specified region. This configuration also defines a backend for storing Terraform state in Cloud Storage.

Task 4. Initialize Terraform

Initialize Terraform in the directory containing your main.tf file.

  1. Run the terraform init command:

    terraform init Note:
    This command initializes Terraform and downloads the necessary provider plugins.
  2. Plan the changes. This shows you what Terraform will do before it makes any actual changes.

    terraform plan Note:
    Review the planned changes.
  3. Run the terraform apply command:

    terraform apply -auto-approve Note:
    This command applies the configuration and creates the resource. The `-auto-approve` flag automatically approves the changes.

Task 5. Verify Bucket Creation

Verify that the bucket was created successfully in your Google Cloud project.

  1. Enter the following command to validate the bucket has been created:

    gsutil ls gs://{{{ project_0.project_id | "PROJECT_ID" }}}-my-terraform-bucket Note:
    Alternatively, you can use the Google Cloud Console, navigate to Storage > Buckets and verify the cloud storage bucket is present.

Task 6. Clean Up (Optional)

To avoid incurring unnecessary charges, you can destroy the resources created by Terraform.

  1. Run the terraform destroy command:

    terraform destroy Note:
    This command destroys the resources defined in your Terraform configuration. You will be prompted to confirm the action by typing `yes`.

Congratulations!

You have successfully created a Google Cloud Storage bucket using Terraform and stored the Terraform state in a Cloud Storage bucket. This lab demonstrated the basic workflow of defining infrastructure as code using Terraform.

Additional Resources

Manual Last Updated Jul 22, 2025

Lab Last Tested Jul 22, 2025

Vorbereitung

  1. Labs erstellen ein Google Cloud-Projekt und Ressourcen für einen bestimmten Zeitraum
  2. Labs haben ein Zeitlimit und keine Pausenfunktion. Wenn Sie das Lab beenden, müssen Sie von vorne beginnen.
  3. Klicken Sie links oben auf dem Bildschirm auf Lab starten, um zu beginnen

Privates Surfen verwenden

  1. Kopieren Sie den bereitgestellten Nutzernamen und das Passwort für das Lab
  2. Klicken Sie im privaten Modus auf Konsole öffnen

In der Konsole anmelden

  1. Melden Sie sich mit Ihren Lab-Anmeldedaten an. Wenn Sie andere Anmeldedaten verwenden, kann dies zu Fehlern führen oder es fallen Kosten an.
  2. Akzeptieren Sie die Nutzungsbedingungen und überspringen Sie die Seite zur Wiederherstellung der Ressourcen
  3. Klicken Sie erst auf Lab beenden, wenn Sie das Lab abgeschlossen haben oder es neu starten möchten. Andernfalls werden Ihre bisherige Arbeit und das Projekt gelöscht.

Diese Inhalte sind derzeit nicht verfügbar

Bei Verfügbarkeit des Labs benachrichtigen wir Sie per E-Mail

Sehr gut!

Bei Verfügbarkeit kontaktieren wir Sie per E-Mail

Es ist immer nur ein Lab möglich

Bestätigen Sie, dass Sie alle vorhandenen Labs beenden und dieses Lab starten möchten

Privates Surfen für das Lab verwenden

Nutzen Sie den privaten oder Inkognitomodus, um dieses Lab durchzuführen. So wird verhindert, dass es zu Konflikten zwischen Ihrem persönlichen Konto und dem Teilnehmerkonto kommt und zusätzliche Gebühren für Ihr persönliches Konto erhoben werden.