Here is the problem,
I have a script which check if AWS credentials are configured then get the configured region and create a VPC.
Here it is:
#!/usr/bin/env bash
if [ -z "$(aws configure get aws_access_key_id)" ]; then
echo "AWS credentials not configured. Aborting.";
exit 1;
fi;
export REGION=$(aws configure get region)
export vpcId=$(aws --region "$REGION" ec2 create-vpc --cidr-block 10.0.0.0/24 --query 'Vpc.VpcId' --output text)
The problem is that $REGION
is empty even though executing aws configure get region
directly from the console returns something: us-west-1
. Inside the script it returns nothing.
The other weird thing is that :
export vpcId=$(aws ec2 create-vpc --cidr-block 10.0.0.0/24 --query 'Vpc.VpcId' --output text)
returns the VPC ID and it is stored successfully in the vpcId
variable.
What's wrong with this: export REGION=$(aws configure get region)
. Is there an async I/O happening there (aws configure get
reads from a config file, aws ec2 create-vpc
reads from the internet) ?
This is the whole script from the beginning:
#!/usr/bin/env bash
# Test availability of aws-cli
hash aws 2>/dev/null
if [ $? -ne 0 ]; then
echo >&2 "'aws' command line tool required, but not installed. Aborting.";
exit 1;
fi;
# Test availability of the AWS AccessKey
if [ -z "$(aws configure get aws_access_key_id)" ]; then
echo "AWS credentials not configured. Aborting.";
exit 1;
fi;
# Directory
export EC2_STARTER_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export WORKING_DIR="$( pwd )"
# File
export AWS_CONFIG_FILE="${WORKING_DIR}/.aws"
CREATE_VPC="${EC2_STARTER_DIR}/setup/create_vpc.sh"
# Defaults
export REGION="$( aws configure get region )" # Empty variable