0
0
GCPcloud~5 mins

Modules for reusability in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you build cloud infrastructure, you often repeat the same setup steps. Modules let you package these steps once and reuse them easily. This saves time and keeps your setup consistent.
When you need to create multiple similar virtual machines with the same settings.
When you want to deploy the same network setup in different projects or environments.
When you want to share a common storage bucket configuration across several apps.
When you want to organize your infrastructure code into smaller, manageable parts.
When you want to avoid copying and pasting the same code and risk mistakes.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = ">= 4.0"
    }
  }
}

provider "google" {
  project = "example-project"
  region  = "us-central1"
}

module "vm_instance" {
  source       = "./modules/vm"
  instance_name = "my-vm"
  machine_type  = "e2-medium"
  zone          = "us-central1-a"
}

This is the main Terraform file that uses a module called vm_instance.

The terraform block sets the required Terraform and provider versions.

The provider block configures the Google Cloud project and region.

The module block calls the reusable module from the ./modules/vm folder and passes parameters like instance name, machine type, and zone.

Commands
This command initializes Terraform, downloads the Google provider, and prepares the module for use.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding hashicorp/google versions matching ">= 4.0"... - Installing hashicorp/google v4.0.0... - Installed hashicorp/google v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.
This command shows what Terraform will create or change based on the module and parameters.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... 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: # module.vm_instance.google_compute_instance.default will be created + resource "google_compute_instance" "default" { + name = "my-vm" + machine_type = "e2-medium" + zone = "us-central1-a" ... } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the plan and creates the resources defined by the module without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
google_compute_instance.default: Creating... google_compute_instance.default: Still creating... [10s elapsed] google_compute_instance.default: Creation complete after 15s [id=projects/example-project/zones/us-central1-a/instances/my-vm] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skips manual approval to apply changes immediately
This command removes all resources created by Terraform, cleaning up the environment.
Terminal
terraform destroy -auto-approve
Expected OutputExpected
google_compute_instance.default: Destroying... [id=projects/example-project/zones/us-central1-a/instances/my-vm] google_compute_instance.default: Destruction complete after 10s Destroy complete! Resources: 1 destroyed.
-auto-approve - Skips manual approval to destroy resources immediately
Key Concept

If you remember nothing else from this pattern, remember: modules let you write infrastructure code once and reuse it many times to save effort and avoid mistakes.

Common Mistakes
Copying module code instead of calling it as a module
This causes duplicated code that is hard to maintain and update.
Create a module folder and call it using the module block with parameters.
Not running 'terraform init' after adding a module
Terraform won't download the module or provider plugins, causing errors.
Always run 'terraform init' after adding or changing modules.
Hardcoding values inside the module instead of using variables
This makes the module less reusable and flexible.
Use input variables in the module and pass values from the main configuration.
Summary
Use modules to package reusable infrastructure code for Google Cloud.
Initialize Terraform with 'terraform init' to prepare modules and providers.
Use 'terraform plan' to preview changes and 'terraform apply' to create resources.
Clean up resources with 'terraform destroy' when no longer needed.