0
0
GCPcloud~3 mins

Why Variables and outputs in GCP? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how a simple trick can save you hours of repetitive cloud setup work!

The Scenario

Imagine you are setting up a cloud project by typing every detail again and again for each resource, like copying and pasting the same information multiple times.

Then, you want to see the important results after deployment, but you have to dig through many logs and settings to find them.

The Problem

This manual way is slow because you repeat work and can easily make mistakes like typos.

It is painful to update values everywhere if something changes, and finding key results wastes time and causes confusion.

The Solution

Using variables lets you write values once and reuse them everywhere, so you only change them in one place.

Outputs show you the important results clearly after deployment, saving you from searching through details.

Before vs After
Before
resource "google_compute_instance" {
  name = "my-instance-1"
  zone = "us-central1-a"
}
resource "google_compute_instance" {
  name = "my-instance-2"
  zone = "us-central1-a"
}
After
variable "zone" {
  default = "us-central1-a"
}
resource "google_compute_instance" {
  name = "my-instance-1"
  zone = var.zone
}
resource "google_compute_instance" {
  name = "my-instance-2"
  zone = var.zone
}
output "instance_names" {
  value = ["my-instance-1", "my-instance-2"]
}
What It Enables

You can build cloud setups that are easy to change and clearly show key information after deployment.

Real Life Example

A team creates multiple virtual machines in the same zone. Using variables, they set the zone once and reuse it. Outputs show the machine IPs immediately after setup, making access simple.

Key Takeaways

Variables let you reuse and update values easily.

Outputs give clear results after deployment.

This saves time, reduces errors, and improves clarity.