Unable to import subnets using google module


I am trying to manage google cloud infrastructure using terraform modules. We have existing infrastructure I would like to use modules to import them as well.

I got this VPC

name: test-vpc project: project-1 subnets: subnet-01,subnet-02

I used this google cloud module to import VPC and it worked fine, it imported only vpc but not subnets.

terraform import module.vpc.module.vpc.google_compute_network.network projects/project-1/global/networks/test-vpc

Next, I tried to import subnet as well using below command following google and some other documents. What ever I do I am unable to import the subnet, I saw the subnets module got for and for_each loops, Could someone advise how can I import subnets using gcloud modules?

terraform import module.vpc.module.subnets.google_compute_subnetwork.subnetwork projects/project-1/regions/europe-west3/subnetworks/subnet-01

Thank you

Whenever you import anything into Terraform, you need to give the correct resource address identifying where you want it imported.

For something that uses for_each, that means understanding which strings are being used to identify the elements of the for_each, and including them:

terraform import module.vpc.module.subnets.google_compute_subnetwork.subnetwork["some string here"] projects/project-1/regions/europe-west3/subnetworks/subnet-01

Additionally, since most shells will treat double-quote characters as some form of shell syntax, you likely need to quote or escape them in some way, dependent on the shell being used.

Hi Max, I generated terraform plan bases on that I tried all these combinations and I get this error.

terraform import module.vpc.module.subnets.google_compute_subnetwork.subnetwork[“subnet-01”] “projects/project-1/regions/europe-west3/subnetworks/subnet-01”


no matches found: module.vpc.module.subnets.google_compute_subnetwork.subnetwork[subnet-01]

terraform import module.vpc.module.subnets.google_compute_subnetwork.subnetwork["europe-west3/subnet-01"] “projects/project-1/regions/europe-west2/subnetworks/subnet-01”


no matches found:

I’m just going to point out the obvious here… Neither of the things you said you tried match the module address displayed in your screenshot.

Thank you, I grabbed screenshot of one of the resources. I the resource I tried to import is definitely in the plan as shown in the screenshot below. The subnet module got for and for_each loops and I am not sure what kind of input it accepts, Could you help?

looks correct to me. There’s no more help I can give based on the limited information shown.

Hi Max,

Thank you for your help with this, Could you advise if you have imported google VPC network completely before and how you did it? I also attached screenshot of config files, please let me know if you see any issue.

Hi Max,

This issue is fixed, one of my colleague pointed out it’s the shell issue, I need to use single quotes in the module option. Thank you for your contribution for this topic.

terraform import 'module.vpc.module.subnets.google_compute_subnetwork.subnetwork["europe-west3/subnet-01"]' projects/project-1/regions/europe-west3/subnetworks/subnet-01