简体   繁体   中英

Impossible to store aws-cli command into a variable in bash

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

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

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM