Score:0

terraform apply error alreadyExists on untouched resources

gy flag

I am starting a new terraform project, following the official guide:

https://learn.hashicorp.com/tutorials/terraform/gke?in=terraform/kubernetes&utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS&_ga=2.91746777.2118895439.1637849824-960084622.1637849824

I have managed to get it to run. (I am running it as part of a google cloud build task triggered on commit)

However, if i change something in a resource (eg i replaced "gke_num_nodes" default from 2 to 1), when i run terraform apply again this is what I get:


Plan: 4 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + kubernetes_cluster_host = (known after apply)
  + kubernetes_cluster_name = "workspace-auto-gke"
  + project_id              = "workspace-auto"
  + region                  = "europe-west4"
google_compute_network.vpc: Creating...
╷
│ Error: Error creating Network: googleapi: Error 409: The resource 'projects/workspace-auto/global/networks/workspace-auto-vpc' already exists, alreadyExists
│ 
│   with google_compute_network.vpc,
│   on vpc.tf line 15, in resource "google_compute_network" "vpc":
│   15: resource "google_compute_network" "vpc" {
│ 
╵

Is there a way to get it to not try to recreate existing untouched resources?

My cloudbuild.json is as follows:

{
  "steps": [
    {
      "name": "hashicorp/terraform",
      "entrypoint": "/bin/sh",
      "args": [
        "./cloudbuild/prepare-terraform.sh"
      ]
    }
  ],
  "logsBucket": "gs://my-bucket/logdir",
  "serviceAccount": "projects/my-proj/serviceAccounts/[email protected]"
}

with prepare-terraform.sh being simply

terraform init
terraform plan
terraform apply -auto-approve
Score:2
cn flag

If you are using Terraform to create resources, do NOT modify them outside of Terraform.

If you change something manually, Terraform will attempt to put it back the way the HCL declares it and the way that Terraform saved it.

This is called declarative. You are trying to make Terraform dynamic which defeats using Terraform in the first place.

gy flag
The modification i did was inside the terraform files not outside. Simply when i run the terraform plan / apply it will try to create everything from scratch regardless even if it was unchanged and cause the error
Score:1
gy flag

The terraform state needs to be stored where it's accessible by all the builds. For example in a google cloud bucket as seen here: https://www.terraform.io/docs/language/settings/backends/gcs.html

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.