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

89 Views Asked by At

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 ?

1

There are 1 best solutions below

2
mklement0 On

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"