Terraform on google cloud

I am trying to create resources and services on google cloud using terraform. following up this guide

https://cloud.google.com/docs/terraform/get-started-with-terraform

 

In this guide we create a vpc network and then a subnet and then the vm.

Then we can attach the vm to the subnet we created. But suppose that I don't want to create a new vpc network instead I want to attach the vm with the default network which is already present. I have to change this parameter

network_interface {
  subnetwork = google_compute_subnetwork.default.id
   
  access_config {
  # Include this section to give the VM an external IP address
  }
}  

How would I do it? What should I provide in subnetwork = google_compute_subnetwork.***********

Solved Solved
0 7 2,004
1 ACCEPTED SOLUTION

Hi,

I've checked this code.

DamianS_1-1667228981699.png

DamianS_2-1667228996409.png

DamianS_3-1667229079938.png

 

View solution in original post

7 REPLIES 7

Hi,

try with subnetwork = google_compute_subnetwork.default.self_link

best,
DamianS

It raises this error

 

Error: Reference to undeclared resource

│ on main.tf line 11, in resource "google_compute_instance" "vm1":
│ 11: subnetwork = google_compute_network.default.self_link

│ A managed resource "google_compute_network" "default" has not been declared in the root module.

Hi,

network_interface is an optional code block. Did you've tried to use it without it ? Also you can check something like that 

DamianS_0-1667228724322.png

 

 

Hi,

I've checked this code.

DamianS_1-1667228981699.png

DamianS_2-1667228996409.png

DamianS_3-1667229079938.png

 

Yes this worked fine as network = "default" now vm is created with the default network. The network interface was important. I couldn't register the vm without it 

Thank you soo much for your help and time. Now I can attach vm to my existing network