From 500aaa5c6dee3d429764e589361420ed1ca9dc38 Mon Sep 17 00:00:00 2001 From: Harrison Affel Date: Fri, 29 Jul 2022 11:11:54 -0400 Subject: [PATCH] graceful deletion of key-pairs --- drivers/amazonec2/amazonec2.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/drivers/amazonec2/amazonec2.go b/drivers/amazonec2/amazonec2.go index 7f1250e905..443f712d02 100644 --- a/drivers/amazonec2/amazonec2.go +++ b/drivers/amazonec2/amazonec2.go @@ -1578,14 +1578,22 @@ func (d *Driver) deleteKeyPair() error { log.Debugf("deleting key pair: %s", d.KeyName) + var deleteInput ec2.DeleteKeyPairInput instance, err := d.getInstance() if err != nil { - return err + // do not return err as we may have generated a key + // but failed to create an instance. We still want to + // delete the key that we have stored locally + log.Infof("Could not retrieve EC2 instance while attempting key-pair deletion, will attempt to delete locally stored key %s", d.KeyName) + deleteInput.KeyName = &d.KeyName + } else { + // if we get an instance we should delete the + // returned key pair as it may have been changed + // outside of machine. + deleteInput.KeyName = instance.KeyName } - _, err = d.getClient().DeleteKeyPair(&ec2.DeleteKeyPairInput{ - KeyName: instance.KeyName, - }) + _, err = d.getClient().DeleteKeyPair(&deleteInput) if err != nil { return err }