Terraform output shows previous public IP after ec2 instance type change

I have updated the instance type of my EC2 instance on my Terraform resources. It has started a new instance with new public ip. But my outputs are showing the previous public IP

Hi @ruwanvm,

If your output value refers directly to the public_ip attribute of the EC2 instance then Terraform should typically detect that it will change as part of replacing the instance, because the provider will signal that public_ip is a value known only after apply.

It sounds like something unusual happened in your case. To understand more about that, it would help if you can share the relevant parts of your configuration (at least the output value definition and any resource it directly refers to) and, if you still have it, the planned changes Terraform proposed which included the one to replace this EC2 instance, and which would typically also include a plan to change the output value (but perhaps did not, in your case).

Thanks!

Hi,

Something similar happened with me as well.

Steps to reproduce:

  1. Create an aws_instance with user_data in it. Configuration is something like below:

resource “aws_instance” “web” {
ami = data.aws_ami.ububtu.id
instance_type = “t2.micro”

user_data = <<EOF
#!/usr/bin/env bash
sudo apt-get update
sudo apt-get -y install curl nginx
sudo systemctl start nginx.service
sudo systemctl status nginx.service
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh ./get-docker.sh
docker run hello-world
EOF

key_name = aws_key_pair.saurabh_public_key.key_name
network_interface {
network_interface_id = aws_network_interface.my_vpc_network_interface_public.id
device_index = 0
}

tags = {
Name = “HelloWorld”
}
}

  1. terraform apply

  2. (outputs the public ip) the outputs.tf is below:

output “instance_public_dns” {
value = aws_instance.web.public_dns
description = “AWS EC2 Instance Public DNS”
}

output “instance_public_ip” {
value = aws_instance.web.public_ip
description = “AWS EC2 Instance Public IP”
}

  1. Now, update the aws_instance resource defined above (update the user_data - add/remove a command)

  2. terraform apply

  3. the public_ip of the aws_instance from the output will be the Old one.

~ As a workaround - do terraform apply once more - this will show the latest public-ip.

This indeed looks like a bug as it should output the latest public-ip the first time only.

Could anyone please clarify if this is a bug or not?

Hi @saurbhc,

Typically a change to user_data requires entirely replacing the EC2 instance, which will cause it to start fresh with an entirely new IP address. If that’s true for you then it does indeed sound like something strange happened, but I’m not sure what yet.

I suggest opening a bug report in the AWS provider repository and sharing all of the information the provider team requests in their issue template. Then the provider team can hopefully use that information to decide whether what you saw is a bug or if it’s a result of some special behavior I’m not considering yet.

Hi @apparentlymart,

Thanks for the reply, however I figured out the solution.

We can use the flag user_data_replace_on_change in our resource aws_instance and this will fix this problem. :tada: