Search code examples
amazon-web-servicesamazon-ec2

Region being overridden by managed credentials when configuring bash script to deploy resources to specified AWS region,


I have a bash script that I run on an AWS EC2 instance (that runs Cloud9 as a development environment), to bootstrap AWS resources for the environment (in this case, my sandbox). I'm configuring the region I'd like to deploy to like this:

aws configure set region "ap-southeast-1"
REGION=$(aws configure get region)
echo $REGION

This isn't correctly setting the $REGION variable. Using set region ... correctly sets the region in ~/.aws/config file on the EC2 instance. My EC2 instance uses AWS managed credentials, which means that my own local AWS region is being stored in the ~/.aws/credentials file on the EC2 instance, which overrides the config file, so aws configure get region returns the region specified in the credentials file. Is there a way of setting and getting the region in an AWS EC2 instance when using managed credentials?


Solution

  • Please view the order of precedence for setting AWS credentials. Note that running aws configure just creates the credentials file, which is the the 6th item in the precedence list. I suggest using environment variables, the 2nd precedence item.