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

compute_instance: desired_state can only accept RUNNING value #16586

Closed
eraac opened this issue Nov 22, 2023 · 11 comments · Fixed by GoogleCloudPlatform/magic-modules#11902, #20031 or hashicorp/terraform-provider-google-beta#8515

Comments

@eraac
Copy link
Contributor

eraac commented Nov 22, 2023

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request.
  • Please do not leave +1 or me too comments, they generate extra noise for issue followers and do not help prioritize the request.
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment.
  • If an issue is assigned to the modular-magician user, it is either in the process of being autogenerated, or is planned to be autogenerated soon. If an issue is assigned to a user, that user is claiming responsibility for the issue. If an issue is assigned to hashibot, a community member has claimed the issue already.

Terraform Version

Terraform v1.6.4
on darwin_arm64
+ provider registry.terraform.io/hashicorp/google v5.7.0
+ provider registry.terraform.io/hashicorp/google-beta v5.7.0

Affected Resource(s)

  • google_compute_instance

Terraform Configuration Files

resource "google_compute_instance" "bastion" {
  name         = "bastion"
  machine_type = "f1-micro"
  zone         = "europe-west1-b"

  # instance is started/stopped via a Workflow
  desired_status = "TERMINATED"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }

  service_account {
    email  = google_service_account.bastion.email
    scopes = ["cloud-platform"] # all
  }
}

Debug Output

Panic Output

Expected Behavior

The instance should be created and turn-off

Actual Behavior

The terraform apply return an error

│ Error: 1 error occurred:
│       * When creating an instance, desired_status can only accept RUNNING value
│
│   with google_compute_instance.bastion,
│   on bastion.tf line 37, in resource "google_compute_instance" "bastion":
│   37: resource "google_compute_instance" "bastion" {

Steps to Reproduce

  1. terraform apply

Important Factoids

References

Notes

I was able to create this resource with the same configuration before (probably before v5), furthermore the documentation mention the TERMINATED as a valid value.

b/313620109

@eraac eraac added the bug label Nov 22, 2023
@github-actions github-actions bot added forward/review In review; remove label to forward service/compute-instances labels Nov 22, 2023
@edwardmedia edwardmedia self-assigned this Nov 22, 2023
@edwardmedia
Copy link
Contributor

@eraac I tested on v4.5.0 and received the same error. Can you share the debug log for your apply on pre v5?

@eraac
Copy link
Contributor Author

eraac commented Nov 22, 2023

I just retry with the same version as my first apply (version 4.58) ... and got the same issue. I guess the first time I've probably set the desired_status after the first apply and didn't encounter this issue

I think we can transform this issue from bug to enhancement, is actually impossible to create an instance which is turn off (without a double apply), or worst when you want to add a lifecycle to ignore_changes from desired_status, you need to

  • first apply (without lifecycle and desired_status)
  • set desired_status to TERMINATED
  • second apply
  • set the lifecycle {ignore_changes=[desired_status]}

@edwardmedia
Copy link
Contributor

@eraac I doubt there is much we can do in the Terraform. Are you able to create a brand new TERMINATED instance with other means, like gcloud?

@eraac
Copy link
Contributor Author

eraac commented Nov 22, 2023

@eraac I doubt there is much we can do in the Terraform. Are you able to create a brand new TERMINATED instance with other means, like gcloud?

If Terraform can turn off the instance on the second apply, I presume it can also turn it off in the first apply (I don't mind if the instance turn on for a few seconds). Similar to the way the google_container_cluster handles the default node pool, there are already some "post-processing" capabilities.

The real problem is the double apply + the manual edit required to have the desired state

@edwardmedia
Copy link
Contributor

Theoretically I think yes a second call is added to change the state in the create. But I am not sure if that use case has high demand.

@zli82016 what do you think?

@zli82016
Copy link
Collaborator

Technically it is doable, but I don't think this use case has high demand. Normally, users don't want to create a instance and then immediacy terminate it.

@zli82016 zli82016 added forward/exempt Never forward this issue and removed bug forward/review In review; remove label to forward labels Nov 22, 2023
@zli82016
Copy link
Collaborator

The request seems to be not potentially useful for users. I want the ticket to be retriaged.

@zli82016 zli82016 removed their assignment Nov 22, 2023
@SarahFrench
Copy link
Member

Note from triage: We're ok with the resource being updated to allow creation of terminated VMs

@SarahFrench SarahFrench added enhancement size/s and removed forward/exempt Never forward this issue labels Nov 27, 2023
@SarahFrench SarahFrench added this to the Goals milestone Nov 27, 2023
@Dutta78
Copy link

Dutta78 commented Aug 13, 2024

I saw that this can also hamper terraform deletion. Lets consider that we are creating a gcp instance and also a vault approle simultaneously.

Steps to reproduce

  1. trigger the deletion and make sure that vault resources do not get deleted in first terraform destroy job.
    Make sure that desired_status="TERMINATED" at the time of deletion.
  2. Terraform will destroy all the resources except vault resource.
    In the next destroy job it will throw this error.
Error: 1 error occurred:
	* When creating an instance, desired_status can only accept RUNNING value

Even though the instance has been deleted.
I haven't tested it with other resources but the result should be similar.

@karolgorc
Copy link

Wrote the code for this feature. Will submit a PR after this gets merged because I've already accounted for the SUSPENDED state's support.

@Dutta78 Can you please share your terraform config or more detailed steps to reproduce your error? I wasn't able to hit this while writing and testing the fix.

Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.