0

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
eakl
  • 501
  • 8
  • 22
  • write stderr into stdout. – 123 Apr 04 '17 at 12:21
  • Is `$(aws configure get region 2>&1)` correct? I also tried `$( echo $(aws configure get region) 2>&1 )`. None of them works – eakl Apr 04 '17 at 12:32
  • If you run the command in the script not assigning it to a variable does it output to screen? – 123 Apr 04 '17 at 12:38
  • does it work with export REGION=``aws configure get region`` (shit quote, just use simple `) – Frederic Henri Apr 04 '17 at 12:41
  • @FrédéricHenri Why would you expect that to be different? Also you can escape the backticks – 123 Apr 04 '17 at 12:43
  • no I am not sure why it would work, to me $() and backtick does the same thing - I am not a bash expert but could be differences b/w bash version – Frederic Henri Apr 04 '17 at 12:45
  • @FrédéricHenri Then why suggest it? – 123 Apr 04 '17 at 12:46
  • @123 because for me thats the way I am using it (see [here](http://stackoverflow.com/a/40852848/4296747) a reference to the same) and I know my way is working, but again I am not a bash expert - you right I could google and improve my knowledge to check if all bash version have the same behavior or not - I just suggested the OP to use something that has been working fine for me – Frederic Henri Apr 04 '17 at 12:51
  • @FrédéricHenri Fair enough, both commands run code in a subshell and there is little if any difference in functionality but backticks are practically deprecated and become increasingly difficult to use and read if nested so it is probably always better to use `$()` version. – 123 Apr 04 '17 at 13:00
  • @eakl Should post that edit as an answer incase anyone has the same problem in the future :) – 123 Apr 04 '17 at 13:02
  • 1
    @123 thanks for the tips, makes full sense - agree that `$()` is more practical, will update my script. – Frederic Henri Apr 04 '17 at 13:02

2 Answers2

0

Try sourcing your bash script because export inside will export whatever you have there to the sub-shell.

source your_script 

or you could also try this:

 ./your_script  # Same as source command
Palmer
  • 493
  • 4
  • 13
0

This is the whole script from the beginning:

#!/usr/bin/env bash

aws configure get region # Output us-west-1

# 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;

aws configure get region # Output us-west-1

# 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;

aws configure get region # Output us-west-1

# Directory
export EC2_STARTER_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export WORKING_DIR="$( pwd )"

aws configure get region # Output us-west-1

# File
export AWS_CONFIG_FILE="${WORKING_DIR}/.aws"
CREATE_VPC="${EC2_STARTER_DIR}/setup/create_vpc.sh"

aws configure get region # No output

Turns out that AWS_CONFIG_FILE is the environment variable used by aws to set the path to the config file where the aws configure get region command reads from. So the following line export AWS_CONFIG_FILE="${WORKING_DIR}/.aws" just overwrites it. (Thanks @123).

And sorry for such a silly question. When debugging I did not thought the mistake was a simple variable name conflict and I thought it was due to something else. My bad...

eakl
  • 501
  • 8
  • 22