0
0
GCPcloud~5 mins

Variables and outputs in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When building cloud infrastructure, you often need to reuse values and see important results after deployment. Variables let you set values once and use them many times. Outputs show you key information about what you created.
When you want to set the region or project ID once and use it in many places in your configuration
When you need to pass a secret or password safely into your cloud setup
When you want to see the IP address of a virtual machine after it is created
When you want to reuse a network name across multiple resources without typing it repeatedly
When you want to share important information like URLs or IDs with other teams or scripts
Config File - main.tf
main.tf
variable "project_id" {
  description = "The GCP project ID"
  type        = string
  default     = "example-project"
}

variable "region" {
  description = "The GCP region to deploy resources"
  type        = string
  default     = "us-central1"
}

resource "google_compute_instance" "vm_instance" {
  name         = "example-vm"
  machine_type = "e2-medium"
  zone         = "${var.region}-a"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }

  network_interface {
    network = "default"
    access_config {}
  }
  project = var.project_id
}

output "instance_ip" {
  description = "The external IP address of the VM instance"
  value       = google_compute_instance.vm_instance.network_interface[0].access_config[0].nat_ip
}

This file defines two variables: project_id and region, which set the project and location for resources.

It creates a virtual machine instance using these variables.

The output instance_ip shows the external IP address of the VM after deployment.

Commands
This command sets up Terraform in the current folder, downloading the Google provider plugin needed to manage GCP resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/google... - Installing hashicorp/google v4.0.0... - Installed hashicorp/google v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized!
This command shows what Terraform will create or change based on the configuration and variables.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # google_compute_instance.vm_instance will be created + resource "google_compute_instance" "vm_instance" { + name = "example-vm" + machine_type = "e2-medium" + zone = "us-central1-a" + project = "example-project" ... } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the plan, creating the VM instance in GCP using the variables set earlier.
Terminal
terraform apply -auto-approve
Expected OutputExpected
google_compute_instance.vm_instance: Creating... google_compute_instance.vm_instance: Still creating... [10s elapsed] google_compute_instance.vm_instance: Creation complete after 20s [id=projects/example-project/zones/us-central1-a/instances/example-vm] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: instance_ip = "34.68.123.45"
-auto-approve - Skip manual approval to apply changes immediately
This command shows the external IP address of the VM instance created, using the output defined in the configuration.
Terminal
terraform output instance_ip
Expected OutputExpected
34.68.123.45
Key Concept

If you remember nothing else from this pattern, remember: variables let you reuse values easily, and outputs let you see important results after deployment.

Common Mistakes
Not defining a variable but trying to use it in the configuration
Terraform will fail because it does not know what value to use for the missing variable.
Always declare variables with a type and default or provide values during apply.
Trying to output a resource attribute that does not exist or is misspelled
Terraform will error because it cannot find the attribute to show.
Check resource documentation and use exact attribute names in outputs.
Hardcoding values everywhere instead of using variables
Makes configuration harder to change and reuse for different environments.
Use variables for values that may change or be reused.
Summary
Define variables to set reusable values like project ID and region.
Use variables in resource definitions to avoid repeating values.
Define outputs to show important information like VM IP after deployment.
Run terraform init to prepare, terraform plan to preview, terraform apply to create, and terraform output to see results.