I am using the below command to assume a role to access AWS EKS from EC2 Ubuntu. ( I found this command from here )
export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" \
$(aws sts assume-role \
--role-arn arn:aws:iam::123456789231:role/someRole\
--role-session-name MySessionName \
--query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" \
--output text))
When I am running the above command directly from terminal it is working fine and changing the role. But if I am running this command through a script then it is not working, not even showing any error. For example I created a sample bash script with name check.sh . Below is the content of this script
#!/bin/bash
export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" \
$(aws sts assume-role \
--role-arn arn:aws:iam::123456789231:role/someRole\
--role-session-name MySessionName \
--query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" \
--output text))
and when I am running this script using ./check.sh
or sudo ./check.sh
then the command is not running. Can someone please help me what I am doing wrong?
Similarly I tried another command from the above mentioned answer but the same thing is happening. Running it from terminal works fine but not from the script. Below is the second command which I tried
eval $(aws sts assume-role --role-arn arn:aws:iam::123456789123:role/myAwesomeRole --role-session-name test | jq -r '.Credentials | "export AWS_ACCESS_KEY_ID=\(.AccessKeyId)\nexport AWS_SECRET_ACCESS_KEY=\(.SecretAccessKey)\nexport AWS_SESSION_TOKEN=\(.SessionToken)\n"')