1

I am trying to write a query in AWS CLI which will provide with the elasticache snapshots names older than a specific creation date.

I tried with a JMESPath query like:

aws elasticache describe-snapshots \
  --region ap-southeast-1 \
  --snapshot-source "manual" \
  --query 'Snapshots[*].NodeSnapshots[?SnapshotCreateTime >`2022-10-01`] | [?not_null(node)]'

But, this is giving me an empty result.

Snippet of aws elasticache describe-snapshots:

{
    "Snapshots": [{
        "SnapshotName": "snapshot-name",
        "ReplicationGroupId": "rep-id",
        "ReplicationGroupDescription": "redis  cluster",
        "CacheClusterId": null,
        "SnapshotStatus": "available",
        "SnapshotSource": "automated",
        "CacheNodeType": "cache.r6g.large",
        "Engine": "redis",
        "EngineVersion": "6.0.5",
        "NumCacheNodes": null,
        "PreferredAvailabilityZone": null,
        "CacheClusterCreateTime": null,
        "PreferredMaintenanceWindow": "sun:20:00-sun:20:00",
        "TopicArn": null,
        "Port": "6379",
        "CacheParameterGroupName": "default.redis6.x.cluster.on",
        "CacheSubnetGroupName": "redis-group",
        "VpcId": "vpc-01bcajghfghj",
        "AutoMinorVersionUpgrade": "true",
        "SnapshotRetentionLimit": "18",
        "SnapshotWindow": "20:00-21:00",
        "NumNodeGroups": "1",
        "AutomaticFailover": "enabled",
        "NodeSnapshots": [{
            "CacheClusterId": "redis-cluster-01",
            "NodeGroupId": "001",
            "CacheNodeId": "001",
            "NodeGroupConfiguration": null,
            "CacheSize": "20 GB",
            "CacheNodeCreateTime": "1632909889675",
            "SnapshotCreateTime": "1667246439000"
        }],
        "KmsKeyId": "kms-id.."
    }]
}
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83

1 Answers1

0

If we take as an example the JSON given in the documentation:

{
    "Snapshots": [
        {
            "SnapshotName": "automatic.my-cluster2-002-2019-12-05-06-38",
            "NodeSnapshots": [
                {
                    "CacheNodeId": "0001",
                    "SnapshotCreateTime": "2019-12-05T06:38:23Z"
                }
            ]
        },
        {
            "SnapshotName": "myreplica-backup",
            "NodeSnapshots": [
                {
                    "CacheNodeId": "0001",
                    "SnapshotCreateTime": "2019-11-26T00:25:01Z"
                }
            ]
        },
        {
            "SnapshotName": "my-cluster",
            "NodeSnapshots": [
                {
                    "CacheNodeId": "0001",
                    "SnapshotCreateTime": "2019-11-26T03:08:33Z"
                }
            ]
        }
    ]
}

Then, you can realise that you need to filter on SnapshotCreateTime, nested under the array NodeSnapshots.

So, what you need, here, is a double filter:

  • One to filter by date:
    [?SnapshotCreateTime > `2022-10-01`]
    
  • Then, one to exclude all snapshots where the NodeSnapshots array have been emptied by the previous filter:
    [?NodeSnapshots[?SnapshotCreateTime > `2022-10-01`]]
    

And, so, if you only care about the name of the snapshot, you could do with the query:

Snapshots[?NodeSnapshots[?SnapshotCreateTime > `2022-10-01`]].SnapshotName

So, your command ends up being:

aws elasticache describe-snapshots \
  --region ap-southeast-1 \
  --snapshot-source "manual" \
  --query 'Snapshots[?
      NodeSnapshots[?SnapshotCreateTime > `2022-10-01`]
    ].SnapshotName'

Now, with what you are showing as an output in your question, your issue is also coming from the fact that your SnapshotCreateTime is in epoch format in liliseconds, so you just have to convert 2022-10-01 in the right format.

If you are on Linux, you can do this within your command, with date:

aws elasticache describe-snapshots \
  --region ap-southeast-1 \
  --snapshot-source "manual" \
  --query "Snapshots[?
      NodeSnapshots[?
        SnapshotCreateTime > \`$(date --date='2022-10-01' +'%s')000\`
      ]
    ].SnapshotName"
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • Thanks for the reply. I tried using the above mentioned command but its not filtering the snapshots by the date mentioned. Its still showing blank after running the query but I have snapshots present after the mentioned date. – Sharan Chhibber Nov 15 '22 at 05:26
  • I assume the issue is with the filtering of dates as JSON does not have a built-in type for date/time values. The general consensus is to store the date/time value as a string . Please help with the possible solutions.. – Sharan Chhibber Nov 15 '22 at 07:31
  • JSON doesn't indeed have a date or date time type. This said, the good thing with the string représentation used here is that is works as string too: `"2022-10-01" > "2022-10-05"` is true. You could try to do ```NodeSnapshots[?SnapshotCreateTime > `2022-10-01T00:00:00Z` ``` to be extra sure, but this should work. – β.εηοιτ.βε Nov 15 '22 at 15:25
  • In order to help you further, you should provide, in an [edit](https://stackoverflow.com/posts/74430857/edit) of your question, an anonymised JSON output of the `aws elasticache describe-snapshots` command without any query. – β.εηοιτ.βε Nov 15 '22 at 15:26
  • I have updated the question with a JSON output. – Sharan Chhibber Nov 15 '22 at 16:22
  • I have tried doing `NodeSnapshots[?SnapshotCreateTime > 2022-10-01T00:00:00Z` but its still showing blank – Sharan Chhibber Nov 15 '22 at 16:30