Open BrianMusson opened 2 years ago
Hope I am not adding noise as we are experiencing this issue as well: terraform -replace="aws_instance.<name>"
command. After instance is terminated, the aws_ec2_host goes immediately into pending state. So when terraform attempts to re-deploy the aws_instance, (as part of -replace workflow) it returns an error "InvalidHostState". It generally takes about 30-40 minutes for the aws_ec2_host to change state to "available" after aws_instance termination.
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/how-dedicated-hosts-work.html
InvalidHostState | The Dedicated Host must be in the available state to complete the operation. -- | --https://docs.aws.amazon.com/AWSEC2/latest/APIReference/errors-overview.html
It worked without depends_on
meta-argument.
But, be sure there has host_id
set up properly.
I think this is still an issue, as of February 2024. Dedicated hosts in the mac2 instance family can spend up to two hours in the Pending state.
When I update the AMI in terraform and run an "apply" I get an error as soon as the old instance is successfully terminated and the underlying dedicated host has gone into Pending state:
Error: creating EC2 Instance: InsufficientCapacityOnHost: Dedicated host h-xxxxxxxxxxxxxxx has insufficient capacity to launch the instances in this request.
Ideally, terraform would wait for the dedicated host to become Available, and then bring up the new instance. As it is, I have to watch for this in the AWS console, and then re-run my apply once the host is ready.
Ideally, terraform would wait for the dedicated host to become Available
It isn't an issue. Not a Terraform issue. It's an async event of AWS EC2 bare metal instances.
So we can't expect some "timeout-less" apply.
Ps. Even when the dedicated host isn't in pending status anymore, it sill takes a time to be able to run/allocate the instance again.
Ps. If you need to reconcile that automatically, you can try some GitOps approach like FluxCD to run Terraform and solve drifts as well.
Thanks for the reply, @hakuno!
It isn't an issue. Not a Terraform issue. It's an async event of AWS EC2 bare metal instances.
It's certainly an issue I experience through terraform. I guess whether it should be fixed in the aws provider, or not, is in some ways a philosophical question.
Ps. If you need to reconcile that automatically, you can try some GitOps approach like FluxCD to run Terraform and solve drifts as well.
It doesn't need to be automatic for me, thankfully, as that seems like quite an elaborate workaround.
Community Note
Description
Mac instances in particular will put a dedicated host in 'pending' status when stopping or terminating a macOS instance. aws_instance does not have the logic to wait for the instance to become 'available'. Please add a status output so that we can use depend_on on the aws_instance resource.
New or Affected Resource(s)
Potential Terraform Configuration
Ideally we would wait until the dedicated host was "available" before attempting to provision an instance.
References
0000