실습 설정 안내 및 요구사항
계정과 진행 상황을 보호하세요. 이 실습을 실행하려면 항상 시크릿 브라우저 창과 실습 사용자 인증 정보를 사용하세요.

Terraform Essentials: Google Compute Engine Instance

실습 30분 universal_currency_alt 크레딧 1개 show_chart 입문
info 이 실습에는 학습을 지원하는 AI 도구가 통합되어 있을 수 있습니다.
이 콘텐츠는 아직 휴대기기에 최적화되지 않음
최상의 경험을 위해 데스크톱 컴퓨터에서 이메일로 전송된 링크를 사용하여 방문하세요.

gem-terraform-gce-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 use Terraform to create a Google Compute Engine (GCE) instance. You will define your infrastructure as code, allowing you to easily provision and manage your resources. This lab assumes you have basic knowledge of Google Cloud and Terraform.

Task 1. Prerequisites

Before you begin, ensure you have the following:

  1. A Google Cloud project with billing enabled. You will need the Project ID for subsequent steps. The project ID will be:

    Your Project ID is: {{{ project_0.project_id | "PROJECT_ID" }}} Note:
    Make note of this ID; you will need it in the next steps.
  2. Terraform installed on your local machine or in Cloud Shell. If using Cloud Shell, Terraform is pre-installed.

    terraform version Note:
    Verify Terraform is installed.
  3. The Google Cloud SDK (gcloud) installed and configured. If using Cloud Shell, gcloud is pre-installed and authenticated.

    gcloud version Note:
    Verify gcloud is installed.
  4. Authenticate to your Google Cloud project. In Cloud Shell, this might be done automatically.

    gcloud auth login Note:
    Authenticate gcloud to access your Google Cloud resources.
  5. Set your Project ID to be:

    gcloud config set project {{{ project_0.project_id | "PROJECT_ID" }}} Note:
    Set the current project.

Task 2. Create a Cloud Storage Bucket for Terraform State

Terraform uses a state file to track the resources it manages. For collaborative projects and increased reliability, it's best to store this state remotely in a Cloud Storage bucket.

  1. Create a Cloud Storage bucket. The bucket name must be globally unique and should include your project ID as a prefix.

    gsutil mb -l {{{ project_0.default_region | "REGION" }}} gs://{{{ project_0.project_id | "PROJECT_ID" }}}-tf-state Note:
    Create the Cloud Storage bucket to store Terraform state.
  2. Enable versioning on the bucket. This allows you to revert to previous states if necessary.

    gsutil versioning set on gs://{{{ project_0.project_id | "PROJECT_ID" }}}-tf-state Note:
    Enable versioning for state file history.

Task 3. Create Terraform Configuration Files

Now, you will create the Terraform configuration files that define your GCE instance.

  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 = var.project_id region = var.region } resource "google_compute_instance" "default" { name = "terraform-instance" machine_type = "e2-micro" zone = var.zone boot_disk { initialize_params { image = "debian-cloud/debian-12" } } network_interface { subnetwork = "default" access_config { } } } Note:
    This file defines the Terraform provider, backend, and the GCE instance resource. A `variables.tf` configuration will be used to define the PROJECT_ID and REGION.
  2. Create a file named variables.tf (optional, but recommended for defining variables):

    variable "project_id" { type = string description = "The ID of the Google Cloud project" default = "{{{ project_0.project_id | "PROJECT_ID"}}}" } variable "region" { type = string description = "The region to deploy resources in" default = "{{{ project_0.default_region | "REGION" }}}" } variable "zone" { type = string description = "The zone to deploy resources in" default = "{{{ project_0.default_zone | "ZONE" }}}" } Note:
    This file defines variables for your project ID, region, and zone. Note that it contains defaults.

Task 4. Initialize, Plan, and Apply Terraform

With the configuration files created, you can now initialize, plan, and apply your Terraform configuration.

  1. Initialize Terraform. This downloads the necessary provider plugins.

    terraform init Note:
    Initialize Terraform to download 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. Apply the changes. This creates the GCE instance.

    terraform apply -auto-approve Note:
    Apply the Terraform configuration to create the instance. The `-auto-approve` flag automatically approves the changes. Be cautious when using this flag in production environments.

Task 5. Verify the Instance

Once Terraform has finished, verify that the GCE instance has been created.

  1. In the Google Cloud Console, navigate to Compute Engine > VM instances. You should see an instance named 'terraform-instance'.

  2. Alternatively, use the gcloud command to list instances.

    gcloud compute instances list Note:
    Verify the instance using the gcloud CLI.

Task 6. Destroy the Infrastructure

When you are finished, destroy the infrastructure to avoid incurring unnecessary costs.

  1. Destroy the resources created by Terraform.

    terraform destroy -auto-approve Note:
    Destroy the resources created by Terraform.

Congratulations!

In this lab, you learned how to use Terraform to create a Google Compute Engine instance. By defining your infrastructure as code, you can easily manage and provision resources in a consistent and repeatable way. Remember to destroy your resources when you're finished to avoid unnecessary costs. Continue exploring Terraform's capabilities and integrate it into your Google Cloud workflows.

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개만 가능

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

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

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