'Using "count" in aws_route53_record terrafrom resource

I'm starting to use (and learn) terraform, for now, I need to create multiple DO droplets and attach them to the aws route53 zone, what I'm trying to do:

My DO terraform file:

# Configure the DigitalOcean Provider
provider "digitalocean" {
  token = var.do_token
}

# Create a new tag
resource "digitalocean_tag" "victor" {
  name = "victor-fee1good22"
}

resource "digitalocean_droplet" "web" {
  count = 2

  image    = var.do_config["image"]
  name     = "web-${count.index}"
  region   = var.do_config["region"]
  size     = var.do_config["size"]
  ssh_keys = [var.public_ssh_key, var.pv_ssh_key]
  tags     = [digitalocean_tag.victor.name]
}

My route53 file:

provider "aws" {
  version    = "~> 2.0"
  region     = "us-east-1"
  access_key = var.aws_a_key
  secret_key = var.aws_s_key
}

data "aws_route53_zone" "selected" {
  name = "devops.rebrain.srwx.net"
}

resource "aws_route53_record" "www" {
  сount = length(digitalocean_droplet.web)

  zone_id = data.aws_route53_zone.selected.zone_id
  name    = "web_${count.index}"
  type    = "A"
  ttl     = "300"
  records = [digitalocean_droplet.web[count.index].ipv4_address]
}

But I always get The "count" object can be used only in "resource" and "data" blocks, and only when the "count" argument is set. error, what did I wrong?

Thanks!

UPDATE: stack trace



Solution 1:[1]

I've resolved this one like — add ?ount = 2 instead of ?ount = length(digitalocean_droplet.web)

It works but would be better to have the dynamic variable instead of constant count. :)

Solution 2:[2]

you want to get number of services, that not yet created. Terraform couldn't do that.

As I think simplest way use common var with the number of droplets.

resource "digitalocean_droplet" "test" {
  count = var.number_of_vps
  image  = "ubuntu-18-04-x64"
  name   = "test-1"
  region = data.digitalocean_regions.available.regions[0].slug
  size   = "s-1vcpu-1gb"
}

resource "aws_route53_record" "test" {
  count = var.number_of_vps
  zone_id = data.aws_route53_zone.primary.zone_id
  name    = "${local.login}-${count.index}.${data.aws_route53_zone.primary.name}"
  type    = "A"
  ttl     = "300"
  records = [digitalocean_droplet.test[count.index].ipv4_address]
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fee1good
Solution 2 Kirill Morozov