@@ -174,19 +174,21 @@ func (p *DefaultProvider) List(ctx context.Context) ([]*Instance, error) {
174
174
}
175
175
176
176
func (p * DefaultProvider ) Delete (ctx context.Context , id string ) error {
177
- if _ , err := p .ec2Batcher .TerminateInstances (ctx , & ec2.TerminateInstancesInput {
178
- InstanceIds : []string {id },
179
- }); err != nil {
180
- if awserrors .IsNotFound (err ) {
181
- return cloudprovider .NewNodeClaimNotFoundError (fmt .Errorf ("instance already terminated" ))
182
- }
183
- if _ , e := p .Get (ctx , id ); e != nil {
184
- if cloudprovider .IsNodeClaimNotFoundError (e ) {
185
- return e
186
- }
187
- err = multierr .Append (err , e )
177
+ out , err := p .Get (ctx , id )
178
+ if err != nil {
179
+ return err
180
+ }
181
+ // Check if the instance is already shutting-down to reduce the number of terminate-instance calls we make thereby
182
+ // reducing our overall QPS. Due to EC2's eventual consistency model, the result of the terminate-instance or
183
+ // describe-instance call may return a not found error even when the instance is not terminated -
184
+ // https://docs.aws.amazon.com/ec2/latest/devguide/eventual-consistency.html. In this case, the instance will get
185
+ // picked up by the garbage collection controller and will be cleaned up eventually.
186
+ if out .State != ec2types .InstanceStateNameShuttingDown {
187
+ if _ , err := p .ec2Batcher .TerminateInstances (ctx , & ec2.TerminateInstancesInput {
188
+ InstanceIds : []string {id },
189
+ }); err != nil {
190
+ return err
188
191
}
189
- return fmt .Errorf ("terminating instance, %w" , err )
190
192
}
191
193
return nil
192
194
}
0 commit comments