uwieuwe4
uwieuwe4

Reputation: 153

How to use Regular Expression in AWS CLI Filter

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}[_]

Upvotes: 5

Views: 20947

Answers (2)

pprog ramming
pprog ramming

Reputation: 1

See the gist below. It covers:

  • search ECR images sorted descending by imagePushDate
  • selecting the tag that meets a Regex criteria
  • using that to replace a key/value pair in a yaml

https://gist.github.com/pprogrammingg/69e7c85abede9822f2480e9b5e1e66fd

Upvotes: 0

amsg
amsg

Reputation: 31

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.

Upvotes: 3

Related Questions