How to use Regular Expression in AWS CLI Filter

Viewed 13322

I am using AWS Command Line Interface (CLI) to list some AMI Images from AWS. The Name of an Image is like:

XY_XYZ_Docker_1.13_XYZ_XXYY

When using

aws ec2 describe-images --filters 'Name=name,Values="*_Docker_1.13_*"'

it works as expected.

Now i want to use Regular Expression instead of static value for the Name-Filter. In the AWS-Docs I read that filtering by RegEx is possible My approach is:

1:

aws ec2 describe-images --filters 'Name=name,Values="[_]Docker[_][0-9][.][0-9]{2}[_]"'

The result is always null for this. I tried different ways of quoting the RegEx.

2:

[_]Docker[_][0-9][.][0-9]{2}[_]

(without quotes) leads to

Error parsing parameter '--filters': Expected: ',', received: 'D' for input: Name=name,Values=[]Docker[][0-9][.][0-9]{2}[_]

3:

 *[_]Docker[_][0-9][.][0-9]{2}[_]*

(with Asterisk) leads to

Error parsing parameter '--filters': Expected: ',', received: ']' for input: Name=name,Values=[_]Docker[_][0-9][.][0-9]{2}[_]

1 Answers

I wasn't able to find if Jmespath or the --filters flag can support regex, so instead I just piped to Python to run through regex.

aws ec2 describe-images --filters 'Name=name,Values="*Docker*"' | \
python -c '
import json, sys, re
obj = json.load(sys.stdin)
matched_images = {"Images":[]}
for image in obj["Images"]:
  if len(re.findall(r"[Dd]ocker\s?[0-9][.][0-9]{2}", image["Name"])) > 0:
    matched_images["Images"].append(image)
print json.dumps(matched_images)
'

You can pipe the output (which is just a JSON string) to your next bash command if needed with a pipe character following the closing quote. Maybe this can address concerns with using grep since it returns a JSON string instead or regular text.

Related