1

I'm trying to get a list of the images older than x date. As per the AWS doc, I'm using the YYYY-MM-DDThh:mm:ss.sssZ format.

Here is my code:

DescribeImagesResponse describeImagesResponse = _ec2Client.DescribeImages(new DescribeImagesRequest
{
    Filters = new List<Filter> { new Filter("creation-date", new List<string> { "2021-09-29T*" }) },
    Owners = new List<string> { "self" }
});

return describeImagesResponse.Images;

The DescribeImagesRequest returns a list of 106 images without the filter, all being created from 2019 to 2022, so I was expecting a list of about 50 images or so.

Instead, I get an empty list.

How can I get all images older than a specific date?

Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44
  • The filter is returning images with a creation date of `2021-09-29`, "which matches an entire day". It is _not_ asking for images "older than x date". It is not possible to specify a before/after filter. You would need to describe _all_ the images and then check the date in code. (Keep the `self` parameter, otherwise the listing includes public AMIs too.) – John Rotenstein Sep 16 '22 at 22:34

1 Answers1

2

There is no native 'older than x' or 'newer than x' date filter, for the DescribeImages API endpoint. As mentioned in the docs, the filter is - at max, with a wildcard - for an entire day, not a range.

You will have to do the filtering client-side:

  1. describe all self-owned images with no date filter
  2. manually parse the dates for each image
  3. filter the images based on your creation-date requirements, within the client

For C#, this should work:

var ec2Client = new AmazonEC2Client();
var threshold = new DateTime(2021, 09, 29);

var describeImagesRequest = new DescribeImagesRequest
{
    Owners = new List<string> { "self" }
};

var describeImagesResponse =
    await ec2Client.DescribeImagesAsync(describeImagesRequest);

var imagesOlderThanSpecifiedDate =
    describeImagesResponse.Images.Where(image => DateTime.Parse(image.CreationDate) < threshold);
Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44