aws / aws-tools-for-powershell

The AWS Tools for PowerShell lets developers and administrators manage their AWS services from the PowerShell scripting environment.
Apache License 2.0
235 stars 78 forks source link

Get-EC2Instance with use of nexttoken fails to work correctly #177

Closed jrich523 closed 3 years ago

jrich523 commented 3 years ago

Description

I have 1700 EC2 instances and I'd like to pull all of them. As per the readme, I used the -NextToken with $AWSHistory.LastServiceResponse.NextToken

Reproduction Steps

$ec21 = Get-EC2Instance -MaxResult 1000 -NextToken $null $ec22 = while($t = Get-EC2Instance -MaxResult 1000 -NextToken $AWSHistory.LastServiceResponse.NextToken){write-host "fount $($t.count)";$t}

Environment

ashishdhingra commented 3 years ago

Hi @jrich523,

I tried to simulate your scenario with 7 EC2 instances (since it is not feasible for me to spin up 1000 instances) using the following commands:

$ec21 = Get-EC2Instance -MaxResult 5 -NextToken $null

$ec21 contains the following output:

GroupNames    : {}
Groups        : {}
Instances     : {testkeypair}
OwnerId       : 139480602983
RequesterId   :
ReservationId : r-0fa2a832c0f8e79c1

GroupNames    : {}
Groups        : {}
Instances     : {testkeypair}
OwnerId       : 139480602983
RequesterId   :
ReservationId : r-053bbec924dc3278e

GroupNames    : {}
Groups        : {}
Instances     : {testkeypair}
OwnerId       : 139480602983
RequesterId   :
ReservationId : r-05300cb80917c822b

GroupNames    : {}
Groups        : {}
Instances     : {testkeypair}
OwnerId       : 139480602983
RequesterId   :
ReservationId : r-09e1a5c8f234a111a

GroupNames    : {}
Groups        : {}
Instances     : {testkeypair}
OwnerId       : 139480602983
RequesterId   :
ReservationId : r-09298e501b07a548b

$AWSHistory.LastServiceResponse.NextToken returns the next token:

eyJ2IjoiMiIsImMiOiJwSklBazBEemRwWnR0c0o5SWFxWlV3NTFyU1dDZ0FoZ0k2NG4xVWtrMjNUNlBCbWZ4SDVFOVdOT2NEVkZIRHJpeUszZW9PRERyZzk4aE5vRlRLUnNLOG5icFl4Z1BmSDd3d0ZqUW5hNVhUMW9LOW16a0RkbU9oUlBRa1BjbUFBQkNxelJMMkZEZW1jNm9iOXRuckM4ZkVjYjl4N28wQ1ZrTG80Yi9SOXVaQndmaUc3T05Famw1ZWhDbHkzcFBHbjNyL3cvMHc9PSIsInMiOiIxIn0=
$ec22 = Get-EC2Instance -MaxResult 5 -NextToken $AWSHistory.LastServiceResponse.NextToken

$ec22 contains:

GroupNames    : {}
Groups        : {}
Instances     : {testkeypair}
OwnerId       : 139480602983
RequesterId   :
ReservationId : r-02f9ff0159a8528e8

GroupNames    : {}
Groups        : {}
Instances     : {testkeypair}
OwnerId       : 139480602983
RequesterId   :
ReservationId : r-023cfe00fdbc2dede

$AWSHistory.LastServiceResponse.NextToken returns nothing since last command was for the last page.

Hence your code:

$ec21 = Get-EC2Instance -MaxResult 5 -NextToken $null
$ec22 = while($t = Get-EC2Instance -MaxResult 5 -NextToken $AWSHistory.LastServiceResponse.NextToken){write-host "found $($t.count)";$t}

executes endlessly producing the following output:

found 2
found 5
found 2
found 5
found 2
found 5
found 2
found 5
found 2
found 5
found 2
found 5
found 2
...

Output of Get-AWSPowerShellVersion:

AWS Tools for Windows PowerShell
Version 4.0.6.0
Copyright 2012-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Amazon Web Services SDK for .NET
Core Runtime Version 3.3.107.9
Copyright 2009-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Release notes: https://github.com/aws/aws-tools-for-powershell/blob/master/CHANGELOG.md

This software includes third party software subject to the following copyrights:
- Logging from log4net, Apache License
[http://logging.apache.org/log4net/license.html]

Everything appears to be working fine. Could you please share the exact issue you are facing so that we could troubleshoot the same?

Thanks, Ashish

github-actions[bot] commented 3 years ago

This issue has not recieved a response in 2 weeks. If you want to keep this issue open, please just leave a comment below and auto-close will be canceled.