to get the instance count of a certain period via LaunchTime wrt Powershell
Asked Answered
A

1

0

I have tried with the below

Is my Date format correct as per awscli LaunchTime ?

To get the last 5 Days Instance count which was created via LaunchTime with of AWS cli in Powershell:

    $getDays = Get-Date
    $5Days = $getDays.AddDays(-5).ToString('yyyy-MM-dd')
    $instancesCreated = aws ec2 --profile $awsProfile --region $awsRegion describe-instances --query "Reservations[*].Instances[*].[InstanceId,LaunchTime>='$5Days']"
    Write-Output (" Total instances count in last 5days : " +$instancesCreated)

But I am not getting the actual count here, getting the Total counts - anything if I am missing here ?

Allegorical answered 16/11, 2023 at 18:58 Comment(0)
A
1

Note:

  • I do not have access to AWS, so the following is untested.
  • I'm assuming that the Reservations[*].Instances[*] part of your query is correct.

Assuming that you're looking to extract just the .InstanceId property values of the matching instances, using a JMESPath query, as supported by the aws CLI's --query parameter:

Replace:

--query "Reservations[*].Instances[*].[InstanceId,LaunchTime>='$5Days']"

with:

--query "(Reservations[*].Instances[])[?LaunchTime>='$5Days'].InstanceId"
Aspersion answered 17/11, 2023 at 23:27 Comment(2)
Thank you Sir for your observations again @Aspersion !! But here is two things happening as I changed with the replaced query <a> now it's not showing any result(but earlier it was fetching the whole instance's record) <b>when I was modifying with .AddDays and then I checked with the dataType of the modified date - it is showing me String type but if I check the describe-instance api it says the datatype of date is ISO 8601 [docs.aws.amazon.com/cli/latest/reference/ec2/… - Am I missing anything while converting the launchDate(launch-time) ?Allegorical
@MoneyTimes, see if my update helps. Yes, the date format is ISO 8601, but dates are represented as strings in JSON. This format that ensures that lexical sorting is equivalent to chronological sorting.Aspersion

© 2022 - 2025 — McMap. All rights reserved.