Terraform import is not importing the GCP app engine details

Hi All,

Please find below the gcloud command output for GCP app engine details.

$ gcloud app describe --project="irn-xxxxx-xxx-xx"
authDomain: xxxx.com
codeBucket: staging.irn-xxxxx-xxx-xx.appspot.com
defaultBucket: irn-xxxxx-xxx-xx.appspot.com
defaultHostname: irn-xxxxx-xxx-xx.ew.r.appspot.com
  splitHealthChecks: true
  useContainerOptimizedOs: true
gcrDomain: eu.gcr.io
id: irn-xxxxx-xxx-xx
locationId: europe-west
name: apps/irn-xxxxx-xxx-xx
serviceAccount: irn-xxxxx-xxx-xx@appspot.gserviceaccount.com
servingStatus: SERVING

We are able to see the app engine details using gcloud command. However, TF import command is not importing the state of App engine using following command. Even we tried with project ID and defaultHostname of app engine at the end of terraform import command ( part of cloud resource id ) still it’s not importing the app engine details.

$ terraform import -var-file irn-xxxxx-xxx.tfvars google_app_engine_application.app apps/irn-xxxxx-xxx-xx

data.google_iam_policy.sa_user_for_root_runner[0]: Reading...
data.google_iam_policy.sa_user_for_root_runner[0]: Read complete after 0s [id=xxxxxxxxx]
data.null_data_source.sshkey[0]: Reading...
data.null_data_source.sshkey[0]: Read complete after 0s [id=static]
data.google_compute_subnetwork.subnet-ifnotconnect2hostproject[0]: Reading...
data.template_file.init_jumpbox_linux_script[0]: Reading...
data.template_file.init_jumpbox_linux_script[0]: Read complete after 0s [id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]
data.google_compute_subnetwork.subnet-ifnotconnect2hostproject[0]: Read complete after 0s [id=projects/host-xxxxx-xxxxx-1/regions/europe-xxxxx/subnetworks/xxxxx-xxxxxxx-xxxxxx-1-subnet3]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

Could you please help me?

Hi @ganesh-pawade,

The output you shared seems like a successful import. Do you have some other output which shows the problem you are asking about? Thanks!

Terraform is claiming the import was successful, but it doesn’t seem to actually mention importing anything. That seems similar to a bug I filed today: `terraform import` claims to succeed, but doesn't import anything, when used on a resource with `for_each`, and an incorrect index · Issue #33292 · hashicorp/terraform · GitHub

However, @ganesh-pawade has not given us the relevant Terraform configuration for the resource being imported, so we can’t tell for sure.

Ahh yes, I think you are right.

In the latest release there is a bug where specifying an instance key that doesn’t match any of the instances declared in the configuration ends up not matching any of the objects after Terraform resolves for_each (or count) and so Terraform never actually visits the address specified on the command line and so doesn’t notice the problem.

I believe that others on the team are currently working on fixing that but in the meantime the solution is to correct your import address to match what’s declared in your configuration. You can find what’s declared in the configuration by running terraform plan and finding the “… will be created” action for the relevant resource instance and use exactly the address Terraform was proposing to create as your import destination.

(If the address contains any characters that your shell considers to be special, such as quotes, you’ll need to escape them to ensure that they get interpreted by Terraform instead of by your shell.)

I am using below configuration for app engine in which for_each is not in use.

Ah, it is as I thought. Because you have used count, google_app_engine_application.app is no longer the correct address to be passing on the import command line.

Once count is in use you must add a square-bracketed index to identify which element of the counted resource - in this case: google_app_engine_application.app[0]

Granted, this seems fairly academic when the count can only be zero or one, but it is still required.

Unfortunately, due to a bug, Terraform currently does not give good feedback when this is forgotten in an import command.