I am trying to generate random hostnames for a couple of servers. However when running terraform apply I get an error message "Error: Cycle: random_pet.swm01_managers, hcloud_server.swm01_managers"

This is what I am trying to do:

variable "swm01_managers_count" {
  default = 5

# ─── CREATE RANDOM SERVER HOSTNAMES ─────────────────────────────────────────────

resource "random_pet" "swm01_managers" {
  count = "${var.swm01_managers_count}"
  keepers = {
    id = "${hcloud_server.swm01_managers[count.index].id}"

# ─── CREATE DOCKER SWARM MANAGER NODES ──────────────────────────────────────────

resource "hcloud_server" "swm01_managers" {
  count = "${var.swm01_managers_count}"

  name = "${random_pet.swm01_managers[count.index]}"
  image = "ubuntu-18.04"
  server_type = "cx11"
  ssh_keys = "${var.ssh_keys}"
  labels = {
    "env" = "prd"
    "docker_swarm_role" = "manager"


Ans here's the error I get:

terraform apply --var-file=prd/prd.tfvars

Error: Cycle: random_pet.swm01_managers, hcloud_server.swm01_managers

The two resources are referring to each other, by interpolating the others attributes, so Terraform can't work out which needs to be created first.

Terraform can't create the random hostnames because it needs the ID's of the servers, and it can't create the servers because it needs their random hostnames.

You need to break the cyclic dependency for it to work, I would say by making the random hostnames not dependent on the server ID's.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.