0

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"')
Faisal Shani
  • 698
  • 1
  • 13
  • 37
  • What do you mean the command is not running. Why don't you try to add some error checking to make sure it is working. Try adding this after the command to see the error code. `echo $?` - If its 0, then its working. – Nathan Getty Jul 26 '22 at 20:15
  • I can confirm if the command worked or not by running " aws sts get-caller-identity " . This will show the new role if the command worked. When I am running from terminal then I am getting the new role but when using the same command inside script and running that script then I am not getting the new role. – Faisal Shani Jul 26 '22 at 20:17
  • 2
    export only affects the current running process, it can not export variables into your parent shell unless you source the script with `.` or `source` to run it in your current shell process. – jordanm Jul 26 '22 at 20:21
  • 1
    I'm almost certain environment variables launched in a child shell script CANNOT be set as environment variables to the parent. IE; if I have a shell script that does `export TEST=1234` I run the script, and then try to `echo $TEST` it will fail. You will most likely have to, in your script, write the credentials to a tempfile, then use those in your current shell – Nathan Getty Jul 26 '22 at 20:22
  • 1
    Use `source ./check.sh` to execute the script in your current shell. – Barmar Jul 26 '22 at 20:26
  • Thanks @Barmar. This worked. Can you please post this as an answer. Thanks nathan and jordanm – Faisal Shani Jul 26 '22 at 20:27
  • 1
    There must be hundreds of similar questions this is a duplicate of. I'm not going to bother answering another one. – Barmar Jul 26 '22 at 20:28

0 Answers0