Resource x already exists during terraform apply

Hi there,
I’m setting up snowplow in GCP, following the guide on Quick Start Installation Guide on GCP - Snowplow Docs

While running the terraform apply command the first time, the Cloud SQL Admin API wasn’t enabled, so I got an error about that. After enabling the API, I ran the command again, and got the same error message. Then, as suggested in the message, I waited a few minutes and tried once more, but this time I got an error message telling me that the database instance already existed.

My knowledge of Terraform is close to non-existing, so my next attempt involved deleting the tfstate and tfstate.backup file before running terraform plan and terraform apply again.

Now, I’m getting error messages telling me that a bunch of resources (loadbalancer, service-account, firewall etc) already exist.

I would think there was a way to tell terraform to skip creation of existing resources, or that the terraform plan command could accommodate something similar, but from what I can gather, this seems not possible…

How would you suggest I proceed?

Changing the prefix in the terraform.tfvars file seems to have worked around the issue.

Hey there! So, my first question is whether or not you’re running terraform init at all? You’ll want to make sure you’re saving your lock and state file somewhere that is accessible every run. Otherwise it’ll continue to try and recreate the same resources every run.

Another thing you can do is manually import the resources into your state file by using the terraform import command. More information on that can be found here: Importing Existing Infrastructure into Terraform - Step by Step

Hi Katie,
yes, I did run the init command first, but thanks for pointing me in direction of the import command, which might be handy in case I ever run into something like this again.

Changing the prefix did resolve the conflict in this case, but that may not always be the most desirable approach :slight_smile: