使用aws cli,确定当前区域的最佳方法是什么

Using aws cli, what is best way to determine the current region

以交互方式,我可以使用 "aws configure" 更改或查看默认区域。是否有 "pwd" 之类的功能,无论是否记录在案,都可以让我确定或确认当前区域的中间脚本?即使 AWS_DEFAULT_REGION 没有定义?我想要一个脚本到运行下的多个配置文件。我可以从 aws 配置列表中抓取,但是有没有更整洁的东西?

可能是AWS没有提供获取当前区域。但是,它们不是获取当前区域,而是提供通过实例元数据获取当前可用区。所有可用区都包含当前区域,因此您可以通过在 EC2 实例上的脚本中替换部分当前可用区来确定当前区域。

例如:

curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/\(.*\)[a-z]//'

aws configure get region 将在您的脚本中获取当前区域。

如果您使用的是配置文件,请输入 aws configure get region --profile $PROFILE_NAME

aws configure get region 很整洁,但我希望即使 AWS_DEFAULT_REGION 已设置 也能知道该区域。不幸的是,根据文档:

Note that aws configure get only looks at values in the AWS configuration file. It does not resolve configuration variables specified anywhere else, including environment variables, command line arguments, etc.

相反,假设您安装了 Python 和 boto3,您可以 运行:

python -c 'import boto3; print(boto3.Session().region_name)'

例如

$ AWS_DEFAULT_REGION=us-east-1 python -c 'import boto3; print(boto3.Session().region_name)'
us-east-1

地区如下:

curl http://169.254.169.254/latest/dynamic/instance-identity/document

所以...

curl -s http://169.254.169.254/latest/dynamic/instance-identity/document|jq -r .region

摘自@RichVel 的评论 ,从 AWS_DEFAULT_REGION 或 aws 配置文件中的区域获取已解析的区域集(aws configure get region 仅给出在配置文件)使用:

aws configure list | grep region | awk '{print }'

示例:

未设置 $AWS_DEFAULT_REGION

$ echo $AWS_DEFAULT_REGION

$ cat ~/.aws/config
[foo]
region = us-east-1
$ aws configure list | grep region | awk '{print }'
us-east-1
$ aws configure get region
us-east-1

$AWS_DEFAULT_REGION 设置:

$ export AWS_DEFAULT_REGION=us-west-2
$ echo $AWS_DEFAULT_REGION
us-west-2
$ cat ~/.aws/config
[foo]
region = us-east-1
$ aws configure list | grep region | awk '{print }'
us-west-2
$ aws configure get region
us-east-1

这基于 and 的答案,这些答案具有以下已知限制:

  • aws configure get region 不 return 如果区域是由环境变量 AWS_DEFAULT_REGION.
  • 定义的
  • boto3 可能并不总是可用,即使 python -c 'import boto3; print(boto3.Session().region_name)' 在可用时有效。

要解决上述限制,请考虑 Bash:

AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-$(aws configure get region)}

这使用 AWS_DEFAULT_REGION 的值(如果可用),否则使用 aws configure get region 的输出。

如果您在 .aws/config 中有个人资料的区域信息,您可以执行以下操作:

.aws/config 示例(请注意 dev 配置文件称为 profile dev)。 profile 字是 imporant.

[default]
region = ap-southeast-2

[profile dev] 
region = us-east-1

然后使用cli,你可以:

aws configure get profile.default.region

给出 ap-southeast-2

aws configure get profile.dev.region

这给出 us-east-1.

下面给出无论是否设置环境变量,CLI实际使用的区域:

aws ec2 describe-availability-zones --output text --query 'AvailabilityZones[0].[RegionName]'

CLI 将使用的区域由以下优先顺序确定:

  1. 命令行--region选项
  2. AWS_DEFAULT_REGION 环境变量的值
  3. 'Current profile'中指定的区域,由以下优先顺序决定
    • 命令行--profile选项
    • AWS_PROFILE 环境变量的值
    • 否则配置文件是[默认]

不幸的是,仅使用 aws configure get region returns 您 'Current profile' 指定的区域,并忽略了 AWS_DEFAULT_REGION 环境变量。

无需假设任何优先规则,您可以只使用 CLI 进行实际的 API 调用并从结果中获取区域。我能找到的唯一应该始终有效的调用是 aws ec2 describe-availability-zones。此方法的另一个优点是您可以指定 --region--profile 选项并仍然得到正确答案。

不确定这是否适合您,但我来到此页面是为了寻求类似的建议。我经常在配置为使用不同区域的 AWS 配置文件之间切换。就我而言,我使用的配置文件由环境变量控制 - AWS_PROFILE

要获取当前区域,不管我使用的是哪个配置文件,我发现这个命令很有用:

aws configure get region --profile=$AWS_PROFILE