Skip to content

Commit

Permalink
Skip keypair delete when lack permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai committed Oct 26, 2023
1 parent 2cf567c commit 6ca202a
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions aws-throwaway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ mod iam;
mod ssh;
mod tags;

use anyhow::anyhow;
use aws_config::meta::region::RegionProviderChain;
use aws_config::SdkConfig;
use aws_sdk_ec2::config::Region;
use aws_sdk_ec2::error::ProvideErrorMetadata;
use aws_sdk_ec2::types::{
BlockDeviceMapping, EbsBlockDevice, Filter, InstanceNetworkInterfaceSpecification, KeyType,
Placement, PlacementStrategy, ResourceType, Subnet, VolumeType,
Expand Down Expand Up @@ -458,17 +460,17 @@ impl Aws {

async fn delete_keypairs(client: &aws_sdk_ec2::Client, tags: &Tags) {
for id in Self::get_all_throwaway_tags(client, tags, "key-pair").await {
if let Err(err) = client
.delete_key_pair()
.key_pair_id(&id)
.send()
.await
.map_err(|e| {
anyhow::anyhow!(e.into_service_error())
.context(format!("Failed to delete keypair {id:?}"))
})
{
tracing::error!("keypair {id:?} could not be deleted: {err}");
if let Err(err) = client.delete_key_pair().key_pair_id(&id).send().await {
let err = err.into_service_error();
tracing::error!("code: {:?}", err.code());
if err.code() == Some("UnauthorizedOperation") {
tracing::error!("{:?}", anyhow!(err).context(format!(
"Did not have permissions to delete keypair {id:?}, skipping all other keypairs since they will also fail."
)));
return;
} else {
panic!("Failed to delete keypair {id:?}: {err:?}")
}
} else {
tracing::info!("keypair {id:?} was succesfully deleted");
}
Expand Down

0 comments on commit 6ca202a

Please sign in to comment.