<p>Maybe you could use "count" attribute for this?</p>

<pre><code>resource "cloudflare_record" "record1" {
    count = ${aws_instance.instance1.public_ip == "" ? 0 : 1}"
    ...
}
</code></pre>

CC BY-SA 4.0
2018-07-15T21:26:18.667Z
Thanks for the suggestion. I should clarify - I do keep the instances active, but just shut them down when I'm not using them. So unfortunately this wouldn't work as to Terraform, the instances still exist. I'll edit my post to clarify this.
Tim Malone
CC BY-SA 4.0
2018-07-16T21:13:44.120Z
Actually - I do apologise - I read your answer in a hurry and _completely_ misinterpreted it!! I think this should work actually and I’m not sure why I didn’t try it. Will give it a go shortly!
Tim Malone
CC BY-SA 4.0
2018-07-17T08:38:30.287Z
Great, hope it works for you!
Yekta Leblebici
CC BY-SA 4.0
2018-07-15T19:23:04.923Z
2018-07-15T19:23:04.923Z
Yekta Leblebici
<p>I've got a couple of EC2 instances managed by Terraform, with public IP addresses in a VPC.</p>

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

<p>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.</p>

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

<pre><code>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"
}
</code></pre>

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

<p>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 <code>ignore_changes</code> conditionally, and thus ignore changes to the DNS record's <code>value</code> if it is blank?</p>

<p>Are there any other solutions you can recommend?</p>

<amazon-ec2><terraform>
Terraform: ignore_changes conditionally? (temporary EC2 instance public IP address)
Tim Malone
# Terraform: ignore_changes conditionally? (temporary EC2 instance public IP address)

(Asked by: username_0 on 2018-04-28)

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?

**Tags:** amazon-ec2, terraform


---

## 1 answers

### Answer by username_1 on 2018-07-15. Score: 1

Maybe you could use "count" attribute for this?
resource "cloudflare_record" "record1" {
    count = ${aws_instance.instance1.public_ip == "" ? 0 : 1}"
    ...
}

> **Comments:**
> **username_0**: Thanks for the suggestion. I should clarify - I do keep the instances active, but just shut them down when I'm not using them. So unfortunately this wouldn't work as to Terraform, the instances still exist. I'll edit my post to clarify this.  
> **username_0**: Actually - I do apologise - I read your answer in a hurry and _completely_ misinterpreted it!! I think this should work actually and I’m not sure why I didn’t try it. Will give it a go shortly!  
> **username_1**: Great, hope it works for you!  

---

