0

I'm trying to filter the images that follow the following pattern in their tag value: dev/yyyy-mm-dd eg. : dev/2021-09-11

the best idea that came up to me was something like this:

aws ec2 describe-images --filters Name=tag:tag_name,Values="dev/????-??-??"

is there a more logical approach?

Good Panic
  • 117
  • 1
  • 1
  • 6
  • 1
    I'd recommend a small Python script that would call `describe_images()`, then check whether the tag value can be converted to a date. – John Rotenstein Jul 25 '22 at 02:20
  • @JohnRotenstein Could you please elaborate on this? I have noticed that we can pipe python on top of the aws command, but not sure how to write it properly. – Good Panic Jul 25 '22 at 03:01

1 Answers1

1

You would use the boto3 AWS SDK to call AWS directly, something like:

import boto3
from datetime import datetime

FIND_TAG = 'your-tag'

ec2_resource = boto3.resource('ec2')

images = ec2_resource.images.filter(Owners=['self'])

# Find the matching tag
for image in images:
  values = [tag['Value'] for tag in image.tags if tag['Key'] == FIND_TAG]

if len(values) > 0:
  value = values[0]

  # Is it a date?
  try:
    date = datetime.strptime(value, '%Y-%m-%d')
  except ValueError:
    print('Not a date')
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470