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

Terraform Essentials: Firewall Policy

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

gem-terraform-fw-rule-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 guides you through creating a firewall rule in Google Cloud using Terraform. You will learn how to define a firewall rule resource, configure its properties, and apply it to your Google Cloud project. This lab assumes you have a basic understanding of Google Cloud and Terraform.

Task 1. Configure Google Cloud Project

Before you begin, configure your Google Cloud project. This includes setting the project ID, region, and zone. Also, enable the IAM API.

  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 to

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

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

Task 2. Create a 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 (GCS).

  1. Create a Cloud Storage bucket. Ensure the bucket name is globally unique and prefixed with your project ID:

    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 enables versioning on the bucket.

Task 3. Defining the Firewall Rule in Terraform

Now, you will define the firewall rule using Terraform's configuration language.

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

    mkdir terraform-firewall && cd $_ Note:
    This creates a new directory and changes the current directory to it.
  2. Create a file named firewall.tf and add the following code to define a firewall rule that allows SSH traffic (port 22) to instances with the tag ssh-allowed.

    resource "google_compute_firewall" "allow_ssh" { name = "allow-ssh-from-anywhere" network = "default" project = "{{{ project_0.project_id | "PROJECT_ID" }}}" allow { protocol = "tcp" ports = ["22"] } source_ranges = ["0.0.0.0/0"] target_tags = ["ssh-allowed"] } Note:
    This configuration creates a firewall rule named `allow-ssh-from-anywhere` that allows TCP traffic on port 22 from any source IP address (0.0.0.0/0) to instances tagged with `ssh-allowed`.
  3. Create a variables.tf file to define variables used in firewall.tf and main.tf.

    variable "project_id" { type = string default = "{{{ project_0.project_id | "PROJECT_ID" }}}" } variable "bucket_name" { type = string default = "{{{ project_0.project_id | "PROJECT_ID" }}}-tf-state" } variable "region" { type = string default = "{{{ project_0.default_region | "REGION" }}}" } Note:
    This creates variables for the project ID, bucket name, and region.
  4. Create an outputs.tf file to output the firewall rule name.

    output "firewall_name" { value = google_compute_firewall.allow_ssh.name } Note:
    This outputs the name of the firewall rule.

Task 4. Applying the Terraform Configuration

Now you will apply the Terraform configuration to create the firewall rule in your Google Cloud project.

  1. Run terraform init to enable Terraform.

    terraform init Note:
    This command downloads the Terraform provider for the configuration files.
  2. Run terraform plan to preview the changes Terraform will make.

    terraform plan Note:
    This command shows the planned changes without applying them.
  3. Run terraform apply to apply the configuration and create the firewall rule.

    terraform apply Note:
    Type `yes` when prompted to confirm the changes.
  4. Verify that the firewall rule has been created in the Google Cloud Console.

    Navigate to **VPC network > Firewall** in the Google Cloud Console and verify the existence of the `allow-ssh-from-anywhere` firewall rule. Note:
    This is a manual verification step.

Task 5. Cleaning Up Resources

To avoid incurring unnecessary costs, destroy the resources created in this lab.

  1. Run terraform destroy to remove the firewall rule.

    terraform destroy Note:
    Type `yes` when prompted to confirm the destruction.

Congratulations!

You have successfully created and destroyed a firewall rule in Google Cloud using Terraform. You have learned how to define firewall rules as code, manage their properties, and apply them to your Google Cloud project. This approach enables you to automate and manage your infrastructure in a consistent and repeatable manner.

Additional Resources

Manual Last Updated Jul 22, 2025

Lab Last Tested Jul 22, 2025

시작하기 전에

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

시크릿 브라우징 사용

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

콘솔에 로그인

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

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

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

감사합니다

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

한 번에 실습 1개만 가능

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

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

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