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

[Bug]: "Terraform detected the following changes made outside" for changes made inside. #37149

Open
halberom opened this issue Apr 29, 2024 · 6 comments
Labels
bug Addresses a defect in current functionality. service/ec2 Issues and PRs that pertain to the ec2 service. service/iam Issues and PRs that pertain to the iam service. service/vpc Issues and PRs that pertain to the vpc service. upstream-terraform Addresses functionality related to the Terraform core binary.

Comments

@halberom
Copy link

halberom commented Apr 29, 2024

Terraform Core Version

1.6.2

AWS Provider Version

5.45.0

Affected Resource(s)

  • aws_instance and aws_ec2_instance_state
  • aws_instance and aws_ebs_volume
  • aws_security_group and aws_vpc_security_group_ingress_rule or aws_vpc_security_group_egress_rule
  • aws_iam_role if attaching managed policies via aws_iam_role_policy_attachment

that I'm aware of so far.

Expected Behavior

Terraform should only display changes under Terraform detected the following changes made outside of Terraform since the last "terraform apply"... when someone or something changes resources managed by Terraform outside of Terraform.

Also

Entities and their metadata should be listed only once in state or all matching entities should be updated at the same time if a change occurs.

Actual Behavior

Terraform shows Terraform detected the following changes made outside of Terraform since the last "terraform apply"... for changes applied by Terraform.

Probably because entities are saved in state inline and within standalone resources, but only the standalone resource gets updated during an applied change. Subsequent plans show "Terraform detected the following changes..." because the inline data has not been updated.

example combinations I've found so far include

  • aws_instance and aws_ec2_instance_state
  • aws_instance and aws_ebs_volume
  • aws_security_group and aws_vpc_security_group_ingress_rule or aws_vpc_security_group_egress_rule
  • aws_iam_role if attaching managed policies via aws_iam_role_policy_attachment

Relevant Error/Panic Output Snippet

The following output is after changing instance state from stopped to running.

plan output:

Terraform detected the following changes made outside of Terraform since the
last "terraform apply" which may have affected this plan:
  # aws_instance.this has changed
  ~ resource "aws_instance" "this" {
        id                                   = REDACTED
      ~ instance_state                       = "running" -> "stopped"

excerpt from state file:

  {
      "mode": "managed",
      "type": "aws_ec2_instance_state",
      "name": "this",
      "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
      "instances": [
        {
          ...
          "attributes": {
            ...
            "state": "running",
  ...
 {
      "mode": "managed",
      "type": "aws_instance",
      "name": "this",
      "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
      "instances": [
        {
            ...
            "instance_state": "stopped",

Terraform Configuration Files

https://gist.github.com/halberom/4a4aaabda189df511d32926986968268

Steps to Reproduce

See the example gist, or you can verify/test with any of the combinations (and probably others) mentioned above.

  • use aws_instance to create an ec2 instance
  • use aws_ec2_instance_state to set the state to "running"
  • apply
  • run plan, note no changes
  • update aws_ec2_instance_state to set the state to "stopped"
  • apply
  • run plan, Terraform detected the following changes, aws_instance state has changed outside of terraform.
  • review the state file, you should see two different instance running states as pasted above in the example output.

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

related to #22317 and #26161

I also raised https://discuss.hashicorp.com/t/query-re-entities-listed-twice-in-state-when-they-can-be-inline-but-are-created-as-individual-resources/64794 to ask about this.

Would you like to implement a fix?

No

@halberom halberom added the bug Addresses a defect in current functionality. label Apr 29, 2024
Copy link

Community Note

Voting for Prioritization

  • Please vote on this issue by adding a 👍 reaction to the original post to help the community and maintainers prioritize this request.
  • Please see our prioritization guide for information on how we prioritize.
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.

Volunteering to Work on This Issue

  • If you are interested in working on this issue, please leave a comment.
  • If this would be your first contribution, please review the contribution guide.

@github-actions github-actions bot added service/ec2 Issues and PRs that pertain to the ec2 service. service/iam Issues and PRs that pertain to the iam service. service/vpc Issues and PRs that pertain to the vpc service. labels Apr 29, 2024
@terraform-aws-provider terraform-aws-provider bot added the needs-triage Waiting for first response or review from a maintainer. label Apr 29, 2024
@halberom
Copy link
Author

I think #22317 and #26161 are related, you could argue this is a duplicate. But they only mention specific cases, whereas this bug seems to be much broader.

@aristosvo
Copy link
Contributor

aristosvo commented Apr 29, 2024

@halberom Just out of curiosity, does it help when putting these arguments in lifecycle.ignore_changes?

@halberom
Copy link
Author

halberom commented Apr 30, 2024

@aristosvo I was pretty sure it wouldn't, but double checked to be sure and no luck.

Things like instance_state in aws_instance is a returned attribute, not a parameter. So while it can be added without error, I'm not sure it'd do anything regardless.

Also ignore_changes is intended for use when external systems/users might make changes to a managed resource - to stop it from overriding/reverting back to state. That's not the case here - Terraform isn't trying to apply/revert changes, it has in fact already made them.

My current workaround is to use the following logic in my Makefile

apply:
	terraform apply -auto-approve ${PLAN_OUTPUT_DIR}/${PLAN_OUTPUT_FILE}
	@terraform apply -auto-approve -refresh-only >/dev/null

which successfully forces a state refresh silently. adds a few seconds to runtime but I can live with it.

@aristosvo
Copy link
Contributor

aristosvo commented Apr 30, 2024

I didn't expect that to work as well, but wasn't sure 👍🏽

It feels like this is a Terraform Core issue, not something a provider will be able to resolve. This issue seems to cover it, but only offers your already used suggestion.

@halberom
Copy link
Author

Yes quite possible. I was assuming that the provider code determined what state would be recorded for a given resource, given it's what takes the parameters, makes the api calls, and receives the responses.

@justinretzolk justinretzolk added upstream-terraform Addresses functionality related to the Terraform core binary. and removed needs-triage Waiting for first response or review from a maintainer. labels May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Addresses a defect in current functionality. service/ec2 Issues and PRs that pertain to the ec2 service. service/iam Issues and PRs that pertain to the iam service. service/vpc Issues and PRs that pertain to the vpc service. upstream-terraform Addresses functionality related to the Terraform core binary.
Projects
None yet
Development

No branches or pull requests

3 participants