arrow_back

Terraform Essentials: Cloud Firestore Database

로그인 가입
700개 이상의 실습 및 과정 이용하기

Terraform Essentials: Cloud Firestore Database

실습 30분 universal_currency_alt 크레딧 1개 show_chart 입문
info 이 실습에는 학습을 지원하는 AI 도구가 통합되어 있을 수 있습니다.
700개 이상의 실습 및 과정 이용하기

gem-terraform-firestore-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

In this lab, you will learn how to create a Cloud Firestore database using Terraform. You will define your infrastructure as code, store the Terraform state in a Cloud Storage bucket, and deploy the Firestore database using Terraform commands. This lab assumes you have basic knowledge of Google Cloud and Terraform.

Task 1. Set Up Google Cloud Project and Terraform

Before you begin, configure your Google Cloud project and initialize Terraform.

  1. Set your Project ID:

    gcloud config set project {{{ project_0.project_id | "PROJECT_ID" }}} Note:
    This command sets your active project. This ensures that subsequent commands are executed within the correct project context.
  2. Enable the Cloud Firestore API.

    gcloud services enable firestore.googleapis.com Note:
    This command enables the Cloud Firestore API, allowing you to create and manage Cloud Firestore databases in your project.
  3. Enable the Cloud Build API.

    gcloud services enable cloudbuild.googleapis.com Note:
    This command enables the Cloud Build API. Cloud Build automates the deployment of your Terraform configurations.
  4. Create a Cloud Storage bucket to store the Terraform state.

    gcloud storage buckets create gs://{{{ project_0.project_id | "PROJECT_ID" }}}-tf-state --location=us Note:
    This command creates a Cloud Storage bucket to store your Terraform state file. Storing the state remotely allows for collaboration and versioning. The `--location=us` flag specifies the region for the bucket.

Task 2. Create Terraform Configuration

Define the Cloud Firestore database resource in a Terraform configuration file.

  1. Create a file named 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_firestore_database" "default" { name = "default" project = "{{{ project_0.project_id | "PROJECT_ID"}}}" location_id = "nam5" type = "FIRESTORE_NATIVE" } output "firestore_database_name" { value = google_firestore_database.default.name description = "The name of the Cloud Firestore database." } Note:
    This Terraform configuration file defines a Cloud Firestore database resource. The `backend "gcs"` block configures Terraform to store the state file in the Cloud Storage bucket you created earlier.
  2. Create a variables.tf file with the following content:

    variable "project_id" { type = string description = "The ID of the Google Cloud project." default = "{{{ project_0.project_id | "PROJECT_ID"}}}" } variable "bucket_name" { type = string description = "Bucket name for terraform state" default = "{{{ project_0.project_id | "PROJECT_ID" }}}-tf-state" } Note:
    This defines variables for the Google Cloud Project ID and the bucket name. It's a best practice to keep variables separate for better configuration management and reusability.
  3. Create a outputs.tf file with the following content:

    output "project_id" { value = var.project_id description = "The ID of the Google Cloud project." } output "bucket_name" { value = var.bucket_name description = "The name of the bucket to store terraform state." } Note:
    This defines the outputs for Terraform, allowing you to easily retrieve the values of the project ID and bucket name after the configuration is applied.

Task 3. Apply the Terraform Configuration

Deploy the Cloud Firestore database using Terraform.

  1. Initialize Terraform in your working directory.

    terraform init Note:
    This command initializes Terraform in your current directory, downloads the necessary provider plugins, and prepares the working directory for Terraform operations.
  2. Run terraform plan to review the changes.

    terraform plan Note:
    This command shows the changes that Terraform will make to your infrastructure. Review the plan carefully to ensure that the changes are what you expect.
  3. Run terraform apply to apply the configuration. Type yes when prompted to confirm.

    terraform apply Note:
    This command applies the Terraform configuration and creates the Cloud Firestore database. Terraform will prompt you to confirm the changes before proceeding.
  4. Verify that the Cloud Firestore database has been created in the Google Cloud Console.

    Note:
    Go to the Cloud Firestore section in the Google Cloud Console to verify the database. Ensure that the database is configured as specified in your Terraform configuration.

Task 4. Clean Up Resources

Destroy the resources created by Terraform.

  1. Run terraform destroy to destroy the resources. Type yes when prompted to confirm.

    terraform destroy Note:
    This command destroys the resources created by Terraform, including the Cloud Firestore database and any other resources created by the configuration.

Congratulations!

You have successfully created a Cloud Firestore database using Terraform. You learned how to define your infrastructure as code, store the Terraform state in a Cloud Storage bucket, and deploy the database using Terraform commands. Remember to always clean up your resources after you are done with them to avoid unnecessary costs.

Additional Resources

Manual Last Updated Jul 25, 2025

Lab Last Tested Jul 25, 2025

시작하기 전에

  1. 실습에서는 정해진 기간 동안 Google Cloud 프로젝트와 리소스를 만듭니다.
  2. 실습에는 시간 제한이 있으며 일시중지 기능이 없습니다. 실습을 종료하면 처음부터 다시 시작해야 합니다.
  3. 화면 왼쪽 상단에서 실습 시작을 클릭하여 시작합니다.

시크릿 브라우징 사용

  1. 실습에 입력한 사용자 이름비밀번호를 복사합니다.
  2. 비공개 모드에서 콘솔 열기를 클릭합니다.

콘솔에 로그인

    실습 사용자 인증 정보를 사용하여
  1. 로그인합니다. 다른 사용자 인증 정보를 사용하면 오류가 발생하거나 요금이 부과될 수 있습니다.
  2. 약관에 동의하고 리소스 복구 페이지를 건너뜁니다.
  3. 실습을 완료했거나 다시 시작하려고 하는 경우가 아니면 실습 종료를 클릭하지 마세요. 이 버튼을 클릭하면 작업 내용이 지워지고 프로젝트가 삭제됩니다.

현재 이 콘텐츠를 이용할 수 없습니다

이용할 수 있게 되면 이메일로 알려드리겠습니다.

감사합니다

이용할 수 있게 되면 이메일로 알려드리겠습니다.

한 번에 실습 1개만 가능

모든 기존 실습을 종료하고 이 실습을 시작할지 확인하세요.

시크릿 브라우징을 사용하여 실습 실행하기

이 실습을 실행하려면 시크릿 모드 또는 시크릿 브라우저 창을 사용하세요. 개인 계정과 학생 계정 간의 충돌로 개인 계정에 추가 요금이 발생하는 일을 방지해 줍니다.