【问题标题】:How to Check if AWS Named Configure profile exists如何检查 AWS Named Configure 配置文件是否存在
【发布时间】:2021-08-04 08:15:29
【问题描述】:
在尝试使用命名配置文件之前如何检查它是否存在?
如果我尝试使用不存在的配置文件,aws cli 会抛出一个丑陋的错误,所以我想做这样的事情:
$(awsConfigurationExists "${profile_name}") && aws iam list-users --profile "${profile_name}" || echo "can't do it!"
【问题讨论】:
标签:
amazon-web-services
aws-cli
aws-config
【解决方案1】:
方法 1 - 检查.aws/config 文件中的条目
function awsConfigurationExists() {
local profile_name="${1}"
local profile_name_check=$(cat $HOME/.aws/config | grep "\[profile ${profile_name}]")
if [ -z "${profile_name_check}" ]; then
return 1
else
return 0
fi
}
function awsConfigurationExists() {
local profile_name="${1}"
local profile_status=$( (aws configure --profile ${1} list) 2>&1)
if [[ $profile_status = *'could not be found'* ]]; then
return 1
else
return 0
fi
}
用法
$(awsConfigurationExists "my-aws-profile") && echo "does exist" || echo "does not exist"
或
if $(awsConfigurationExists "my-aws-profile"); then
echo "does exist"
else
echo "does not exist"
fi