0
votes

I am using upcloud provider plugin and terraform 0.12.7 to create new instance of server. Unfortunately plugin is off the main TF site, in https://github.com/UpCloudLtd/terraform-provider-upcloud so it has to be installed using go language, but this part went fine.

    provider "upcloud" {
    # Your UpCloud credentials are read from the environment variables
    # export UPCLOUD_USERNAME="Username for Upcloud API user"
    # export UPCLOUD_PASSWORD="Password for Upcloud API user"
}

resource "upcloud_server" "test" {
    # System hostname
    hostname = "test.example.com"

    # Availability zone
    zone = "de-fra1"

    # Number of CPUs and memory in MB
    cpu = "4"
    mem = "8192"

    storage_devices {
          # OS root disk size
          size = "20"
          action = "clone"

          # Template Ubuntu 18.04
          storage = "01000000-0000-4000-8000-000030080200"
          tier = "maxiops"
    }

    # Include at least one public SSH key
    login {
        user = "root"
        keys = [
            # File module reads file as-is including trailing linefeed that breaks during terraform apply
            "${replace(file("${var.ssh_pubkey}"), "\n", "")}"
        ]
    }

    # Configuring connection details
    connection {
        host = "${self.ipv4_address}"
        type = "ssh"
        user = "root"
        private_key = "${file("${var.ssh_privkey}")}"
    }

    # Remotely executing a command on the server
    provisioner "remote-exec" {
        inline = [
            "echo 'Hello world!'"
        ]
    }
}

output "server_ip" {
    value = "${upcloud_server.test.ipv4_address}"
}

And it creates the resource successfully. Now I change resource name to "test5" (also hostname and other hardcoded values) run terraform plan, and then it tells me that it want to destroy "test" resource even though it has it defined in tf state file and I did not request to delete it. I tried with defining terraform backend "local", keep a workspace persistent at all times, but it did not change a thing. What I am doing wrong? It works the same way if I use OS template or a snapshot volume ID.

Also is there a way to parametrize this resource name (or in output section, self is not working in here), so I don't have to run sed command to keep the names consistent? Other variables may be adjusted in terraform plan or apply - not a problem. Thanks in advance for the response

1

1 Answers

1
votes

You cannot rename a resource just like that...

If you do, terraform recognizes the following changes two in the resource definitions regarding its state file:

  • resource test should be removed (as the definition for the resource stored in the state file is gone)
  • resource test5 should be created (as the resource does not exist in the state file)

If you want to rename the resource, you have to update the state file accordingly. Check our Terraform Command 'state mv'.

In your case you'd need to run terraform state mv upcloud_server.test upcloud_server.test5 and rename the resource in your resource definition.

Then, the definition would match the state file again.