Terraform: ignore_changes conditionally? (temporary EC2 instance public IP address) I've got a couple of EC2 instances managed by Terraform, with public IP addresses in a VPC.

With each Terraform run, I'm updating DNS records based on those public IPs.

I don't need these instances on all the time though, and when I shut them down, they of course lose their public IP address.

At first this was causing associate_public_ip_address to become false, and Terraform wanted to recreate the instances. lifecycle.ignore_changes solves this:

resource "aws_instance" "instance1" {
  associate_public_ip_address = true
  ...

  lifecycle {
    ignore_changes = ["associate_public_ip_address"]
  }
}

resource "cloudflare_record" "record1" {
  domain = "example.com"
  name   = "server1.example.com"
  value  = "${aws_instance.instance1.public_ip}"
  type   = "A"
}


But... the public_ip attribute also becomes blank ;). This causes an attempted update to the A record, which fails because you can't set a blank record.

So, I'm trying to work out a solution. I could use elastic IPs, which I might end up resorting to, but I'd ideally like to avoid that if I can. My next thought was... is there any way I could use ignore_changes conditionally, and thus ignore changes to the DNS record's value if it is blank?

Are there any other solutions you can recommend?
Maybe you could use "count" attribute for this?

resource "cloudflare_record" "record1" {
    count = ${aws_instance.instance1.public_ip == "" ? 0 : 1}"
    ...
}