Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refresh doesn't update the fields of stopped AWS instances #3251

Closed
fatih opened this issue Sep 16, 2015 · 4 comments · Fixed by #3257
Closed

refresh doesn't update the fields of stopped AWS instances #3251

fatih opened this issue Sep 16, 2015 · 4 comments · Fixed by #3257

Comments

@fatih
Copy link
Contributor

fatih commented Sep 16, 2015

Hi,

I've search the issue tracker but couldn't find any related issue. Please feel free to close if this is a duplicate. Having a simple aws_instance resource (full version: https://gist.github.com/fatih/e93aa6f6e9a763ec5957) in the form of:

resource "aws_instance" "web" {
  instance_type = "t2.micro"
  ami = "ami-9e5cff9e"
}

Because it doesn't have an eip, the Public IP will change in every stop/start switch. So the weird thing is, when the instance is stopped, and I call terraform refresh, it still shows the old public IP:

aws_instance.web:
  ...
  public_ip = 54.65.84.208
  ...

If I start the instance manually and call terraform refresh again, it goes and picks up the IP (which changed because it wasn't using eip, as expected):

aws_instance.web:
  ...
  public_ip = 53.35.164.216
  ...

So my question is, why is the state not set to an empty value, if the instance is stopped and I call terraform refresh? I think this is a bug, but I'm not sure. So basically all I want is that terraform show to display the following when the instance is stopped and doesn't have any public IP anymore:

aws_instance.web:
  ...
  public_ip = 
  ...

Is this a wrong assumption?

@phinze
Copy link
Contributor

phinze commented Sep 16, 2015

Hi @fatih - This sounds like reasonable logic to me, but I'd imagine it would be difficult to implement if it diverges from the behavior of the AWS API. Does aws ec2 describe-instances yield an empty or a last-known public IP for stopped instances? If it's empty, then yeah definitely a bug. If it's populated, then I'm not sure if we want to special-case away from the API behavior.

@phinze
Copy link
Contributor

phinze commented Sep 16, 2015

Tagging as bug tentatively and we can see what we find out. 😀

@fatih
Copy link
Contributor Author

fatih commented Sep 16, 2015

I've tested it with the aws-cli and it doesn't return it, aka it's empty. Which makes sense because a stopped instance really doesn't have any IP at all. And terraform show shows the old IP, which theoretically is invalid because it's being released to the IP pool when you switch it. So there is the probability it's being used by someone else.

So I've dived into the code and found that the API call returns a null response, instead of an empty string. For Example the public_dns field returns an empty string, but for public_ip it returns a null. So because of that it doesn't update the state correctly.

I've tried to debugged it and found that we need to set it explicitly to an empty value. This is already the case for maps(https://github.com/hashicorp/terraform/blob/master/helper/schema/field_writer_map.go#L139) , but not for the primivite values. Here is the fix: #3257

@ghost
Copy link

ghost commented Apr 29, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants