title: How to add a label to my vm instance in gcp via terraform/terragrunt
body: <p>I have an issue in our environment where i cannot add a label to a vm instance in GCP via terraform/terragrunt after creation. We have a google repository that is setup via terraform and we use git to clone and update from a local repository, this will activate a trigger on cloudbuild to push the changes to the repo. We do not use terraform/grunt commands at all. It is all controlled via git. The labels are referenced in our compute module as shown.</p>
  <pre><code>   variable &quot;labels&quot; {
     description = &quot;Labels to add.&quot;
     type  = map(string)
     default = {}
     }
  </code></pre>
  <p>Ok onto the issue. We have in our environment a mix of lift and shift and native cloud vm instances. We recently decided we wanted to add an additional label in the code to identify if the instance was under terraform control - ie terraform = &quot;true/false&quot;</p>
  <pre><code> labels = {
      application      = &quot;demo-test&quot;
      businessunit     = &quot;homes&quot;
      costcentre       = &quot;90imt&quot;
      createdby        = &quot;ab&quot;
      department       = &quot;it&quot;
      disasterrecovery = &quot;no&quot;
      environment      = &quot;rnd&quot;
      contact          = &quot;abriers&quot;
      terraform        = &quot;false&quot;
    }
  }
  </code></pre>
  <p>So i add the label and use the usual git commands to add/commit push etc which triggers the cloudbuild as usual. The problem is, the label does not appear in the console when viewing it.</p>
  <p>It's as if cloudbuild or terraform/terragrunt isn't recognising it as a change. I can change the value of a label no problem, but i cannot seem to add or remove a label after the vm has been created.</p>
  <p>It has been suggested to run terraform/terragrunt plan in vs code but as mentioned, this has all been setup to use git so the above commands do not work.</p>
  <p>For example i run terragrunt init in the directory and get this error</p>
  <pre><code>PS C:\Cloudrepos\placesforpeople&gt; terragrunt init
  time=2022-07-27T09:56:27+01:00 level=error msg=Error reading file at path C:/Cloudrepos/placesforpeople/terragrunt.hcl: open C:/Cloudrepos/placesforpeople/terragrunt.hcl: The system cannot find the 
  file specified.
  time=2022-07-27T09:56:27+01:00 level=error msg=Unable to determine underlying exit code, so Terragrunt will exit with error code 1
  PS C:\Cloudrepos\placesforpeople&gt; cd org
  PS C:\Cloudrepos\placesforpeople\org&gt; cd rnd
  PS C:\Cloudrepos\placesforpeople\org\rnd&gt; cd adam_play_area
  PS C:\Cloudrepos\placesforpeople\org\rnd\adam_play_area&gt; ls
  
  
  
  
  Mode                 LastWriteTime         Length Name
  ----                 -------------         ------ ----
  d-----        20/07/2022     14:18                modules
  d-----        20/07/2022     14:18                test_project_001
  
  
  PS C:\Cloudrepos\placesforpeople\org\rnd\adam_play_area&gt; cd test_project_001
  PS C:\Cloudrepos\placesforpeople\org\rnd\adam_play_area\test_project_001&gt; cd compute
  PS C:\Cloudrepos\placesforpeople\org\rnd\adam_play_area\test_project_001\compute&gt; ls
  
  
      Directory: C:\Cloudrepos\placesforpeople\org\rnd\adam_play_area\test_project_001\compute
  
  
  Mode                 LastWriteTime         Length Name
  ----                 -------------         ------ ----
  d-----        07/07/2022     15:51                start_stop_schedule
  d-----        20/07/2022     14:18                umig
  -a----        07/07/2022     16:09           1308 .terraform.lock.hcl
  -a----        27/07/2022     09:56           2267 terragrunt.hcl
  
  
  PS C:\Cloudrepos\placesforpeople\org\rnd\adam_play_area\test_project_001\compute&gt; terragrunt init    
  Initializing modules...
  - data_disk in ..\compute_data_disk
  
  Initializing the backend...
  
  Successfully configured the backend &quot;gcs&quot;! Terraform will automatically
  use this backend unless the backend configuration changes.
  
  Initializing provider plugins...
  - Reusing previous version of hashicorp/google from the dependency lock file
  - Reusing previous version of hashicorp/google-beta from the dependency lock file
  ╷
  │ Warning: Backend configuration ignored
  │
  │   on ..\compute_data_disk\backend.tf line 3, in terraform:
  │    3:   backend &quot;gcs&quot; {}
  │
  │ Any selected backend applies to the entire configuration, so Terraform
  │ expects provider configurations only in the root module.
  │
  │ This is a warning rather than an error because it's sometimes convenient to
  │ temporarily call a root module as a child module for testing purposes, but
  │ this backend configuration block will have no effect.
  ╵
  
  ╷
  │ Error: Failed to query available provider packages
  │
  │ Could not retrieve the list of available versions for provider
  │ hashicorp/google: could not connect to registry.terraform.io: Failed to
  │ request discovery document: Get
  │ &quot;https://registry.terraform.io/.well-known/terraform.json&quot;: Proxy
  │ Authorization Required
  ╵
  
  ╷
  │ Error: Failed to query available provider packages
  │
  │ Could not retrieve the list of available versions for provider
  │ hashicorp/google-beta: could not connect to registry.terraform.io: Failed
  │ to request discovery document: Get
  │ &quot;https://registry.terraform.io/.well-known/terraform.json&quot;: Proxy
  │ Authorization Required
  ╵
  
  time=2022-07-27T09:57:40+01:00 level=error msg=Hit multiple errors:
  Hit multiple errors:
  exit status 1
  PS C:\Cloudrepos\placesforpeople\org\rnd\adam_play_area\test_project_001\compute&gt;
  </code></pre>
  <p>But as mentioned, we dont use and have never used these commands to push the changes.
  I cannot work out why these labels wont add/remove after the vm has already been created.</p>
  <p>I have tried making a change to an instance to trigger the change such as increase the disk size.
  I have tried to create a block in the module for all the labels needed but this doesn't work as you cannot have labels as a block in this module.</p>
  <pre><code> labels {
      application      = var.labels.application
      businessunit     = var.labels.businessunit
      costcentre       = var.labels.costcentre
      createdby        = var.labels.createdby
      department       = var.labels.department
      disasterrecovery = var.labels.disasterrecovery
      environment      = var.labels.environment
      contact          = var.labels.contact
      terraform        = var.labels.terraform
    }
  }
  </code></pre>
  <p>Any ideas? I know you cannot add a label to a project post creation, does the same apply to vm instances? Is there any alternative method i can test?</p>
  <p>As requested this is the code for the vm instance</p>
  <pre><code>terraform {
    source = &quot;../../modules//compute_instance_static_ip/&quot;
  }
  
  # Include all settings from the root terragrunt.hcl file
  include {
    path = find_in_parent_folders(&quot;org.hcl&quot;)
  }
  
  dependency &quot;project&quot; {
    config_path = &quot;../project&quot;
  
    # Configure mock outputs for the terraform commands that are returned when there are no outputs available (e.g the
    # module hasn't been applied yet.
    mock_outputs_allowed_terraform_commands = [&quot;plan&quot;, &quot;validate&quot;]
    mock_outputs = {
      project_id = &quot;project-not-created-yet&quot;
    }
  }
  
  prevent_destroy = false
  
  inputs = {
  
    gcp_instance_sa_email = &quot;testprj-compute@gc-r-prj-testprj-0001-9627.iam.gserviceaccount.com&quot; # This well tell gcp to use the default GCE service account
    instance_name         = &quot;rnd-demo-test1&quot;
    network               = &quot;projects/gc-a-prj-vpchost-0001-3312/global/networks/gc-r-vpc-0001&quot;
    subnetwork            = &quot;projects/gc-a-prj-vpchost-0001-3312/regions/europe-west2/subnetworks/gc-r-snet-middleware-0001&quot;
    zone                  = &quot;europe-west2-c&quot;
    region                = &quot;europe-west2&quot;
    project               =  dependency.project.outputs.project_id
    os_image              = &quot;debian-10-buster-v20220118&quot;
    machine_type          = &quot;n1-standard-4&quot;
    boot_disk_size        = 100
    instance_scope        = [&quot;cloud-platform&quot;]
    instance_tags         = [&quot;demo-test&quot;]
    deletion_protection   = &quot;false&quot;
  
    metadata = {
      windows-startup-script-ps1 = &quot;Set-TimeZone -Id 'GMT Standard Time' -PassThru&quot;
    }
  
    ip_address_region     = &quot;europe-west2&quot;
    ip_address_type       = &quot;INTERNAL&quot;
  
    attached_disks = {
      data = {
        size = 60
        type = &quot;pd-standard&quot;
      }
    }
   /*/ instance_schedule_policy  = {
  
    name               = &quot;start-stop&quot;
    #region                    = &quot;europe-west2&quot;
    vm_start_schedule         = &quot;30 07 * * *&quot;
    vm_stop_schedule          = &quot;00 18 * * *&quot;
    time_zone                 = &quot;GMT&quot;
    }
  */
    labels = {
      application      = &quot;demo-test&quot;
      businessunit     = &quot;homes&quot;
      costcentre       = &quot;90imt&quot;
      createdby        = &quot;ab&quot;
      department       = &quot;it&quot;
      disasterrecovery = &quot;no&quot;
      environment      = &quot;rnd&quot;
      contact          = &quot;abriers&quot;
      terraform        = &quot;false&quot;
    }
  }
  </code></pre>
  <p>terragrunt validate-inputs result below</p>
  <pre><code>PS C:\Cloudrepos\placesforpeople\org\rnd&gt; terragrunt validate-inputs
  time=2022-07-27T14:25:19+01:00 level=warning msg=The following inputs passed in by terragrunt are unused:
   prefix=[C:\Cloudrepos\placesforpeople\org\rnd]
  time=2022-07-27T14:25:19+01:00 level=warning msg=       - billing_account prefix=[C:\Cloudrepos\placesforpeople\org\rnd]
  time=2022-07-27T14:25:19+01:00 level=warning msg=       - host_project_id prefix=[C:\Cloudrepos\placesforpeople\org\rnd]
  time=2022-07-27T14:25:19+01:00 level=warning prefix=[C:\Cloudrepos\placesforpeople\org\rnd]
  time=2022-07-27T14:25:19+01:00 level=info msg=All required inputs are passed in by terragrunt. prefix=[C:\Cloudrepos\placesforpeople\org\rnd]
  time=2022-07-27T14:25:19+01:00 level=error msg=Terragrunt configuration has misaligned inputs
  time=2022-07-27T14:25:19+01:00 level=error msg=Unable to determine underlying exit code, so Terragrunt will exit with error code 1
  PS C:\Cloudrepos\placesforpeople\org\rnd&gt;
  </code></pre>