0

I need to get the terraform state file from the backend GCS and to use it while I update the resource is it possible to overwrite the excising terraform state file and fetch it on need.

this is my main.tf

provider "google" {
    project = var.project
    region  = var.region
    zone    = var.zone
  }

###################################################

########################################################

data "google_compute_default_service_account" "default" {
}


#create instance1
resource "random_id" "bucket_prefix" {
  byte_length = 8
}

#create instance
resource "google_compute_instance" "vm_instance" {
  name = var.instance_name
  machine_type = var.machine_type
  zone         = var.zone
  metadata_startup_script = var.script
  allow_stopping_for_update = true
  #metadata = {
   # enable-oslogin = "TRUE"
  #}
  service_account {
    email  =  data.google_compute_default_service_account.default.email
    scopes = ["cloud-platform"]
  }
 
  boot_disk {
    initialize_params {
    image = var.image
    #image = "ubuntu-2004-lts" # TensorFlow Enterprise
    size  = 30  
    }
  }

  # Install Flask
 
 
  tags = ["http-server","allow-ssh-ingress-from-iap", "https-server"]

  

  network_interface {
    network = "default"
    access_config {
    }
  }
  guest_accelerator{
    #type = "nvidia-tesla-t4" // Type of GPU attahced
    type  = var.type
    count = var.gpu_count
    #count = 2 // Num of GPU attached
  }
  scheduling{
    on_host_maintenance = "TERMINATE" 
    automatic_restart = true// Need to terminate GPU on maintenance
  }
  
}

This is my variables.tfvars:

instance_name   = "test-vm-v5"
machine_type    = "n1-standard-16"
region          = "europe-west4"
zone            = "europe-west4-a"
image           =  "tf28-np-pandas-nltk-scikit-py39"
#image           = "debian-cloud/debian-10"
project         = "my_project"
network         = "default"
type            = ""
gpu_count       = "0"

I wanted to create multiple instances by changing the variables.tfvars and need to modify the instance on the basis of the name of vm.

  • 1
    No, and that would be an anti-pattern. But you could try and give more details otherwise you will not get an answer. – Marko E Oct 25 '22 at 12:40
  • 1
  • It's not a good practice, I agree with my peers. It's dangerous because, you can loose your state. – Mazlum Tosun Oct 25 '22 at 13:42
  • I was creating an automated GCP resource creation set up with terraform ,after each resource creation corresponding state files will be saved in the cloud storage. So in case if the resource need to be be updated I wanted to pull the resource from GCS or local and update the resource instead of creating a new resource or destroying the existing one. How can I do that – meerashine Oct 26 '22 at 07:23

0 Answers0