Reputation: 41
I am trying to delete some network interfaces not used in aws via aws cli. To get this list of network interfaces, i run this:
aws ec2 describe-network-interfaces --filters Name=status,Values=available --query "NetworkInterfaces[].NetworkInterfaceId" --output json
Then, I need to pass the "NetworkInterfaceId" value to delete-network-interface like this:
aws ec2 delete-network-interface --network-interface-id $(aws ec2 describe-network-interfaces --filters Name=status,Values=available --region eu-west-1 --query "NetworkInterfaces[].NetworkInterfaceId" --output text) --region eu-west-1
This only delete one ENI, only the first.
I have tried getting the --generate-cli-skeleton but the output for describe-network-interfaces give me a list of Ids, but the input json skeleton for delete-network-interface only accepts a string.
Some ideas here? Help is much appreciated. Thanks in advance.
Upvotes: 2
Views: 649
Reputation: 41
Here is the way I have solved the problem.
#!/bin/bash
# Get the network interface IDs for all the ENIs in status available and delete them
aws ec2 describe-network-interfaces --profile my_aws_profile --filters Name=status,Values=available --region my_region --query "NetworkInterfaces[*].NetworkInterfaceId" --output text > list.txt
sed 's/\t/\n/g' list.txt > list2.txt
elements=$(wc -l < list2.txt)
while [ $elements -gt 0 ];
do
aws ec2 delete-network-interface --network-interface-id $(cut -f"$elements" list.txt) --profile my_aws_profile --region my_region
((elements--))
done
echo 'All done!'
I store the list of all the ENI IDs in a list.txt file. But it stores this list with tabs, and the while command got an error from the read -r, so I do a sed for changing all the tabs by "new line" character and stored in another file. Now, I need to count how much lines this file has to know how much iterations of the delete-network-interface I need to run, and stored in a variable. This variable is used to run this deletion as an index and decreased until it arrives to 0.
Any contribution is appreciated!
Upvotes: 1