amazon web services -如何在AWS CLI响应上分页



我正在尝试对EC2保留实例产品进行分页,但似乎无法通过CLI进行分页(见下文)。

% aws ec2 describe-reserved-instances-offerings --max-results 20                                                                                 
{
    "NextToken": "someToken", 
    "ReservedInstancesOfferings": [
        {
             ...
        }
    ]
}
% aws ec2 describe-reserved-instances-offerings --max-results 20 --starting-token someToken
Parameter validation failed:
Unknown parameter in input: "PaginationConfig", must be one of: DryRun, ReservedInstancesOfferingIds, InstanceType, AvailabilityZone, ProductDescription, Filters, InstanceTenancy, OfferingType, NextToken, MaxResults, IncludeMarketplace, MinDuration, MaxDuration, MaxInstanceCount

在[1]中找到的文档说使用start-token。我该怎么做?

[1] http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-reserved-instances-offerings.html

考虑到marjamis 2017年的解决方案必须在以前的CLI版本上运行,请参阅从Mac笔记本电脑和AWS - CLI/2.1.2使用bash从AWS分页的工作方法

# The scope of this example requires that credentials are already available or
# are passed in with the AWS CLI command.  
# The parsing example uses jq, available from https://stedolan.github.io/jq/
# The below command is the one being executed and should be adapted appropriately.
# Note that the max items may need adjusting depending on how many results are returned.
aws_command="aws emr list-instances --max-items 333 --cluster-id $active_cluster"
unset NEXT_TOKEN
function parse_output() {
  if [ ! -z "$cli_output" ]; then
    # The output parsing below also needs to be adapted as needed.
    echo $cli_output | jq -r '.Instances[] | "(.Ec2InstanceId)"' >> listOfinstances.txt
    NEXT_TOKEN=$(echo $cli_output | jq -r ".NextToken")
  fi
}
# The command is run and output parsed in the below statements.
cli_output=$($aws_command)
parse_output
# The below while loop runs until either the command errors due to throttling or
# comes back with a pagination token.  In the case of being throttled / throwing
# an error, it sleeps for three seconds and then tries again.
while [ "$NEXT_TOKEN" != "null" ]; do
  if [ "$NEXT_TOKEN" == "null" ] || [ -z "$NEXT_TOKEN" ] ; then
    echo "now running: $aws_command "
    sleep 3
    cli_output=$($aws_command)
    parse_output
  else
    echo "now paginating: $aws_command --starting-token $NEXT_TOKEN"
    sleep 3
    cli_output=$($aws_command --starting-token $NEXT_TOKEN)
    parse_output
  fi
done  #pagination loop

看起来像一些被破坏的文档。

如果你运行下面的命令,它可以工作:

aws ec2 describe-reserved-instances-offerings --max-results 20 --next-token someToken

翻译错误信息,它说它期望NextToken,在CLI上可以表示为next-token

如果您继续阅读您提供的参考文档,您将了解到:

——starting-token(字符串)

指定从哪里开始分页的标记。这是来自先前截断的响应的NextToken。

此外:

——max-items(整数)

要返回的项目总数。如果可用条目的总数大于max-items中指定的值,则输出中将提供一个NextToken,您可以使用它来恢复分页。

相关内容

  • 没有找到相关文章

最新更新