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?
CodePudding user response:
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:
- describe all self-owned images with no date filter
- manually parse the dates for each image
- 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);