0

I have created a new gcp vm instance successfully using terraform modules. The contents in my module folder are as follows

  #main.tf

 # google_compute_instance.default:
  resource "google_compute_instance" "default" {
     machine_type         = "${var.machinetype}"
     name                 = "${var.name}"
     project              = "demo"
     tags                 = []
     zone                 = "${var.zone}"

      boot_disk {

         initialize_params {
         image  = "https://www.googleapis.com/compute/v1/projects/centos-cloud/global/images/centos-7-v20210701"
         size   = 20
         type   = "pd-balanced"
        }
      }


network_interface {
    network            = "default"
    subnetwork         = "default"
    subnetwork_project = "gcp-infrastructure-319318"
}


service_account {
    email  = "[email protected]"
    scopes = [
        "https://www.googleapis.com/auth/devstorage.read_only",
        "https://www.googleapis.com/auth/logging.write",
        "https://www.googleapis.com/auth/logging.write",
        "https://www.googleapis.com/auth/monitoring.write",
        "https://www.googleapis.com/auth/service.management.readonly",
        "https://www.googleapis.com/auth/servicecontrol",
        "https://www.googleapis.com/auth/trace.append",
       ]
      }
    }

   -----------

  #variables.tf

  variable "zone" {
        default="us-east1-b"
     }

 variable "machinetype" {
       default="f1-micro"
    }

   ------------------
   #terraform.tfvars

   machinetype="g1-small"
   zone="europe-west1-b"
      

My main code block is follows

    $ cat providers.tf
       provider "google" {
           }

    $ cat main.tf
       module "gce" {
          source = "../../../modules/services/gce"
          name = "new-vm-tf"
        }

With this code I am able create a new vm instance successfully

    $ gcloud compute instances list
     NAME       ZONE        MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP  STATUS
     new-vm-tf  us-east1-b  f1-micro                   10.142.0.3                RUNNING

Now I have a requirement to create a new VM instance of machine type e2-standard. how can I handle that scenario?

If I edit my existing main.tf as shown below, it will destroy the existing vm instance to create the new vm instance .

         $ cat main.tf
         module "gce" {
             source = "../../../modules/services/gce"
             name = "new-vm-tf1"
        }

terraform plan confirms as below

   ~ name                 = "new-vm-tf" -> "new-vm-tf1" # forces replacement
   Plan: 1 to add, 0 to change, 1 to destroy.

I need pointers to resuse the same code to create a new vm existing without any changes to existing one . Please suggest

1
  • 1
    Using the same configuration (HCL) is possible. However, that is a recipe for disaster. Keep your resource configurations independent so that you do not accidentally destroy (recreate) a resource. Commented Jul 11, 2021 at 18:26

1 Answer 1

2

I recommend you to deep dive into terraform mechanism and best practices. I have 2 keywords to start: tfstate and variables.

The tfstate is the deployment context. If you change the deployment and the context isn't consistent, Terraform delete what is not consistent and create the missing pieces.

The variables are useful to reuse piece of generic code by customizing the values in entry.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.