title: GCE: Setting Environment Variables from Metadata
body: <p>I am using Packer to generate an image on Google Compute Engine, and Terraform to create the instance. I have set this metadata:</p>
  <p>key: <code>env_vars</code></p>
  <p>value: <code>export test=10</code></p>
  <p>Packer is using a script with something like this inside:</p>
  <pre><code>curl &quot;http://metadata.google.internal/computeMetadata/v1/project/attributes/env_vars?recursive=tru&amp;alt=text&quot; -H &quot;Metadata-Flavor: Google&quot; -o /tmp/env_vars.sh
  
  source /tmp/env_vars.sh # or . /tmp/env_vars.sh
  </code></pre>
  <p>The problem is that when I create an instance using this image through Terraform the env variables are not available. That means, If I run <code>printenv</code> or <code>echo $test</code>, it is empty.</p>
  <p>Even if I write a <code>startup-script</code> for the instance, it doesn't work.</p>
  <p>But, if I run the same exact script inside the instance via SSH, it does work.</p>
  <p>In all scenarios described above, the file <code>env_vars.sh</code> is created.</p>
  <p>I just want to set the env vars from my metadata for any instance.</p>
  <p>Any suggestion on how can I achieve this?</p>
  <p>EDIT:</p>
  <p>Here's the terraform code:</p>
  <pre><code># create instance
  resource &quot;google_compute_instance&quot; &quot;default&quot; {
    count        = 1
    name         = var.machine_name
    machine_type = var.machine_type
    zone         = var.region_zone
  
    tags = [&quot;allow-http-ssh-rule&quot;]
   
    boot_disk {
      initialize_params {
        image = var.source_image
      }
    }
   
    network_interface {
      network = &quot;default&quot;
      access_config {
        // Ephemeral IP
      }
    }
  }
  </code></pre>