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?

5
  • 3
    You don't have an array. Use keys=( $(aws iam list-access-keys --user-name "$user_name" --profile="$aws_env" | jq -r '.AccessKeyMetadata[].AccessKeyId') ) for that. ShellCheck recognizes such issues Commented Jun 9, 2018 at 2:07
  • Good catch @thatotherguy Commented Jun 9, 2018 at 2:10
  • yeah, good catch @thatotherguy ! That was it. Problem solved. Thanks! Commented Jun 9, 2018 at 2:16
  • Someone post a real answer or dupe, I'm not near a computer :P Commented Jun 9, 2018 at 2:26
  • Possible duplicate of Arrays in unix shell? Commented Jun 9, 2018 at 2:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.