Terraform import google access context manager for access_level in GCP Not working as expected

Hi,
I am trying to import existing GCP infra (access level) into terraform state, below is the code.

Import Command:
terraform import google_access_context_manager_access_level.access_level.default accessPolicies/590731782016/accessLevels/test_access_001

Resource used in main.tf

resource “google_access_context_manager_access_level” “access-level” {
parent = “accessPolicies/590731782016”
name = “accessPolicies/590731782016/accessLevels/test_access_001”
title = “test_access_001”
basic {
conditions {
members= [
“user:allan.mark@gmail.com
]
}
}
}

Error:
Error: Invalid address

│ on line 1:
│ 1: google_access_context_manager_access_level.access_level.default

│ Resource instance key must be given in square brackets.

For information on valid syntax, see:

I am using as per the documentation but still getting error. Kindly help.

Thanks,
Amarnath.

Hi @amar2k05,

The argument of:

google_access_context_manager_access_level.access_level.default

is not a valid resource instance address. The resource shown in the configuration is called

google_access_context_manager_access_level.access_level

I’m not sure what you were intending the .default portion of the address to do, but if it was the index for an individual instance, you would need to provide the address as

'google_access_context_manager_access_level.access_level["default"]'

Hi Jbardin,
Thank you very much for your response.
Unfortunately, now it’s giving syntax error. I have tries below two import commands.

terraform import google_access_context_manager_access_level.access_level[“accessPolicies/590731782016/accessLevels/test_access_001”]

terraform import ‘google_access_context_manager_access_level.access_level[“accessPolicies/590731782016/accessLevels/test_access_001”]’

If that is literally what you are running then you are missing the final parameter. The terraform import command is as follows:

terraform import <resource name> <identifier>

Where the resource name has to match the full path to a resource block in the code, and the identifier is something used to match the resource in the cloud provider, etc. That identifier is provider & resource specific (for example in AWS it is often an ARN), so you’d need to lok at the provider docs: Terraform Registry

Hi Stuart,
Thanks for your reply.
As per the documentation I have first tried below in which “accessPolicies/590731782016/accessLevels/test_access_001” is the , but still luck.

terraform import google_access_context_manager_access_level.access_level.default accessPolicies/590731782016/accessLevels/test_access_001

Regards,
Amarnath.

Correcting my message.

As per the documentation I have first tried below in which “accessPolicies/590731782016/accessLevels/test_access_001” is the “identifier” , but still no luck.

terraform import google_access_context_manager_access_level.access_level.default accessPolicies/590731782016/accessLevels/test_access_001

Why are you writing .default in your command - what are you expecting it to do?

Hi @maxb ,
Thank you for your reply.
I am using “. default” as per the below documentation .

https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/access_context_manager_access_level#import

You will find the import usage at the end of above documentation for access level resource

Kindly suggest if anything I can change.

Regards,
Amarnath.

.default is used in that usage only as an example name. You have not called your resource default, you have called yours access-level:

so you should not be adding in an extra .default which is incorrect.

Hi @maxb,
Thanks again for the response.
I am now able to import successfully using below.

import --var-file=$VARS --var-file=$BACKEND --var-file=vars/$ENV/terraform.tfvars google_access_context_manager_access_level.access-level accessPolicies/590731782016/accessLevels/test_access_001

I am now going to import a module and I have a question, do we need to have individual configuration for each resource in the module or Can I use directly the module as the configuration?
I mean if my module uses 10 resources, do I need to have 10 resource blocks as part of configuration, or one module will suffice?
Regards,
Amarnath.

I don’t understand the question. An example may help.

Yes if you have a module which contains 10 resources you will need to run 10 separate terraform import commands.