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.***********

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.

Hy check what I found

https://cloud.google.com/docs/terraform/resource-management/import#before_you_begin

Hi,

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

Hi,

I’ve checked this code.

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

1 Like