Skip to content

Commit

Permalink
Merge pull request #190 from HarrisonWAffel/ec2-nil-instance-key-dele…
Browse files Browse the repository at this point in the history
…tion

Graceful deletion of key-pairs
  • Loading branch information
HarrisonWAffel authored Jul 29, 2022
2 parents a468db1 + 500aaa5 commit 88a638a
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions drivers/amazonec2/amazonec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 88a638a

Please sign in to comment.