0

I'm trying to delete AWS user keys using as bash array.

I store the AWS access keys in an array, and read them out in one loop, and then attempt to delete them in another loop.

The loop that reads the variables prints out correctly:

Listing access keys for "script-test".
AKIAJKWXK6ZI6BPV6KOA
AKIAIHM2MZMQC7KLXSJA

But the loop that is supposed to delete the keys messes up the output of the array:

Deleting user access keys for script-test.
Deleting key: AKIAJKWXK6ZI6BPV6KOA
AKIAIHM2MZMQC7KLXSJA

This is the error that occurs:

An error occurred (ValidationError) when calling the DeleteAccessKey operation: The specified value for accessKeyId is invalid. It must contain only alphanumeric characters.

These are the two loops that I'm using to print and then delete the keys:

echo "Listing access keys for \"$user_name\"."
declare -a keys
keys=$( (aws iam list-access-keys --user-name "$user_name" --profile="$aws_env"  | jq -r '.AccessKeyMetadata[].AccessKeyId') )
for key in "${keys[@]}"
do
  echo "$key"
done

echo; echo
echo "Deleting user access keys for $user_name."
for key in "${keys[@]}"
do
 echo "Deleting key: $key"
  aws iam delete-access-key --user-name "$user_name" --access-key-id "$key" --profile="$aws_env"
done

What am I doing wrong? How can I get this to work correctly?

bluethundr
  • 1,005
  • 17
  • 68
  • 141

0 Answers0