1

I have the following code:

foreach ($region in (Get-AWSRegion)) {
    $images = aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[ImageId]' | sort | uniq -c
}

$images

Above code gives me something like:

3 ami-123**

But I am trying to get all AMI's inside all regions together so it should be something like:

9 ami-123**

When I try something like:

$images += aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[ImageId]' | sort | uniq -c

I get: 3 ami-123** 3 ami-123** 3 ami-123**

Is there a way to do this?

2
  • While you iterate Regions your aws command doesn't take the currently iterated Region into account, so why do you do this 1st place? Commented Aug 8, 2019 at 14:36
  • LotPings - So how can I change this? I am basically trying to get all unique ami's in all regions and run an operation on that. My thought was if I get all unique ami's from all regions then it is less overhead to run a query on each ami. Commented Aug 8, 2019 at 14:40

1 Answer 1

3

Three immediate problems - first, you keep overwriting the value of $images inside the loop, and as you've found you can solve that by adding to the previous result set.

Second problem is that you're not actually targeting the $region, so you keep getting the same result from the default region configured for aws - specify --region $region inside the loop (assuming that Get-AWSRegion returns the region identifier as a string).

Third problem is a bit more... fundamental - the aws cli returns a string, so you'll need to manually parse that and aggregate the results - you could do so with a [hashtable] and a simple regex pattern:

# Create hashtable to aggregate results
$ImageIDCounts = @{}

foreach($region in Get-AWSRegion){
    $perRegionCount = aws ec2 describe-instances --region $region --output text --query 'Reservations[*].Instances[*].[ImageId]' | sort | uniq -c
    foreach($entry in $perRegionCount){
        if($entry -match '^\s*(\d+)\s+(.*)$'){
            $Count = $Matches[1] -as [int]
            $ImageID = $Matches[2].Trim()

            $ImageIDCounts[$ImageID] += $Count 
        }
    }
}

$ImageIDCounts now contains one entry per unique ImageID, so you can do:

$ami123count = $ImageIDCounts['ami-123']
Sign up to request clarification or add additional context in comments.

4 Comments

Awesome thank you! I was not looking for count though only unique image id's. I will try to get that from above solution.
I've no clue, but I guess there are other options for --output, not just text ;-) Reading the docs could help AWS Command Line Interface - User Guide
@NoviceMe That's as easy as just grabbing the hashtable keys: $UniqueImageIDs = $ImageIDCounts.Keys
@LotPings indeed, although you'll still need to do some level of post-processing on the output (ConvertFrom-JSON, XmlDocument.Load() etc.) - aws is an external executable so the output is always strings :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.