Question

Terraform config to create domain, record, certificate and loadbalancer at once results in a cycle error

I’m trying to create a Terraform config that will set up a load balancer with Let’s encrypt’s certificate installed for the particular domain.

The configuration I have resulted in

“Error: Cycle: digitalocean_record.static, digitalocean_certificate.cert, digitalocean_loadbalancer.www-lb”

Is there any other way to handle it?

resource "digitalocean_domain" "default" {
  // Note: in reality, the domain name is not "example.com".
  name = "example.com"
}

resource "digitalocean_record" "static" {
  domain = digitalocean_domain.default.name
  type   = "A"
  name   = "static"
  value  = digitalocean_loadbalancer.www-lb.ip
}

resource "digitalocean_certificate" "cert" {
  name    = "examplecert"
  type    = "lets_encrypt"
  domains = [digitalocean_record.static.fqdn]

  lifecycle {
    create_before_destroy = true
  }
}

resource "digitalocean_loadbalancer" "www-lb" {
  name = "example-lb"
  region = var.region
  vpc_uuid = digitalocean_vpc.vpc.id

  forwarding_rule {
    entry_port = 443
    entry_protocol = "https"

    target_port = 80
    target_protocol = "http"

    certificate_id = digitalocean_certificate.cert.id
  }

  healthcheck {
    port = 80
    protocol = "http"
  }

  droplet_ids = digitalocean_droplet.www.*.id
}

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

I’ve sorted this out by hardcoding the “static” subdomain name like this:

domains = [“static.${digitalocean_domain.default.id}”]