Skip to content

Instantly share code, notes, and snippets.

@aidmax
Last active September 13, 2022 19:01
Show Gist options
  • Save aidmax/5d62c7319c2cace2f82f33423a274dc7 to your computer and use it in GitHub Desktop.
Save aidmax/5d62c7319c2cace2f82f33423a274dc7 to your computer and use it in GitHub Desktop.
Detect AWS region and account_id and save it to .bashrc
# Configure AWS CLI with the current region as default
echo "export AWS_DEFAULT_REGION=$(curl -s 169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region)" >> ~/.bashrc
echo "export AWS_REGION=\$AWS_DEFAULT_REGION" >> ~/.bashrc
echo "export AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)" >> ~/.bashrc
source ~/.bashrc
# Verify that AWS_REGION is set correctly
test -n "$AWS_REGION" && echo AWS_REGION is "$AWS_REGION" || echo AWS_REGION is not set
# Save the AWS_ACCOUNT_ID and AWS_REGION to the bash_profile
echo "export AWS_ACCOUNT_ID=${AWS_ACCOUNT_ID}" | tee -a ~/.bash_profile
echo "export AWS_REGION=${AWS_REGION}" | tee -a ~/.bash_profile
aws configure set default.region ${AWS_REGION}
aws configure get default.region
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment