这是问题,
我有一个脚本,用于检查是否配置了AWS凭据,然后获取配置的区域并创建VPC。
这是:
#!/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)
问题是$REGION
为空,即使直接从控制台执行aws configure get region
返回的内容:us-west-1
。在脚本里面它什么都不返回。
另一个奇怪的是:
export vpcId=$(aws ec2 create-vpc --cidr-block 10.0.0.0/24 --query 'Vpc.VpcId' --output text)
返回VPC ID,并成功存储在vpcId
变量中。
这有什么问题:export REGION=$(aws configure get region)
。那里是否发生异步I / O(aws configure get
从配置文件读取,aws ec2 create-vpc
从Internet读取?
这是从头开始的整个脚本:
#!/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
答案 0 :(得分:0)
尝试寻找您的bash脚本,因为导出内部会将您拥有的任何内容导出到子shell。
source your_script
或者你也可以试试这个:
./your_script # Same as source command
答案 1 :(得分:0)
这是从头开始的整个脚本:
#!/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
事实证明AWS_CONFIG_FILE
是aws使用的环境变量,用于设置aws configure get region
命令读取的配置文件的路径。所以下面的行export AWS_CONFIG_FILE="${WORKING_DIR}/.aws"
只是覆盖它。 (谢谢@ 123)。
抱歉这么愚蠢的问题。调试时,我没想到错误是一个简单的变量名称冲突,我认为这是由于其他原因。我的坏......