hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.61k stars 9k forks source link

[Bug]: IncorrectSpotRequestState when restarting Spot Instance to apply user_data changes #33399

Open kevinh-canva opened 10 months ago

kevinh-canva commented 10 months ago

Terraform Core Version

1.5.3

AWS Provider Version

5.9.0

Affected Resource(s)

Expected Behavior

When user_data or user_data_base64of a spot instance is changed (or in general, actions that force a spot EC2 instance to be stopped and restarted), Terraform should wait + retry on IncorrectSpotRequestRate until the instance can be started.

Actual Behavior

The spot instance is stopped, but fail to be restarted. This is because there's a delay for the persistent spot request to be updated from state: active, status: marked-for-stop --> state: disabled, status: instance-stopped-by-user.

When Terraform tries to start the instance again, the request is still active => the instance can't be started.

When the request has transitioned to disabled, the instance can be started.

Relevant Error/Panic Output Snippet

No response

Terraform Configuration Files

resource "aws_instance" "example_instance" {
  ami                   = "some-ami"
  instance_type = "t2.micro"

  instance_market_options {
    spot_options {
      instance_interruption_behaviour = "stop"
      spot_instance_type = "persistent"
    }
  }

  user_data = <<-EOF
    #!/bin/bash
    echo "Hello, World!"
  EOF

  user_data_replace_on_change = false
}

Steps to Reproduce

Debug Output

No response

Panic Output

Error: updating EC2 Instance (i-0942bf2c1f1a5bbbd) user data base64: starting EC2 Instance (i-0942bf2c1f1a5bbbd): IncorrectSpotRequestState: You can't start the Spot Instance 'i-0942bf2c1f1a5bbbd' because the associated Spot Instance request is not in an appropriate state to support start.
status code: 400, request id: 382cbde9-1f5c-48f5-b001-f0722e6fee58
on main.tf line 225, in resource "aws_instance" "workspace":
225: resource "aws_instance" "workspace" {

Important Factoids

No response

References

No response

Would you like to implement a fix?

Yes

github-actions[bot] commented 10 months ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue