简体   繁体   中英

How to filter with value only regardless the name in AWS CLI

My EC2 instance has many tags with a desired value EBM. The thing that this value could be in a different Name, sometimes under tag:Name and sometimes tag:XXX, I tried the below query and it didn't work:

 aws --region sa-east-1 ec2 describe-security-groups --filters 'Name=*,Values=*EBM*'    

An error occurred (InvalidParameterValue) when calling the DescribeSecurityGroups operation: The filter '*' is invalid

any idea how to make the Name as wild card just match the value?

I tried this and it didn't work:

 aws --region sa-east-1 ec2 describe-security-groups --filters 'Name=*,Values=*EBM*'    

An error occurred (InvalidParameterValue) when calling the DescribeSecurityGroups operation: The filter '*' is invalid

I also tried this and didn't work:

aws --region sa-east-1 ec2 describe-security-groups --filters 'Name=*.*,Values=*EBM*' 

Its not possible . You have to get all rules first, then then do filtering yourself.

Use describe-tags

To --filter with a value only regardless of the name in the AWS CLI, you simply can use "Name=value,Values=*tg*" .

  • Keep Name=value so as to look at the value fields only.
  • use Value=*EBM* it will fetch all values having EBM regardless of prefix or suffix.

However, You can combine --filters with the --query option to filter the output and only display specific fields. For example, to only display the tag names and values , you can use the following command:

$ aws ec2 describe-tags --filters "Name=value,Values=*tg*" --query 'Tags[*].{Key_Name: Key, VauleOfKey: Value}' --profile dev
[
    {
        "Key_Name": "SSM_Managed",
        "VauleOfKey": "Stg"
    },
    {
        "Key_Name": "SSM_Managed",
        "VauleOfKey": "Stg"
    },
    {
        "Key_Name": "SSM_Managed",
        "VauleOfKey": "Stg"
    },
    {
        "Key_Name": "SSM_Managed",
        "VauleOfKey": "Stg"
    },
    {
        "Key_Name": "SSM_Managed",
        "VauleOfKey": "Stg"
    },
    {
        "Key_Name": "SSM_Managed",
        "VauleOfKey": "Stg"
    },
    {
        "Key_Name": "SSM_Managed",
        "VauleOfKey": "Stg"
    },
    {
        "Key_Name": "SSM_Managed",
        "VauleOfKey": "Stg"
    },
    {
        "Key_Name": "SSM_Managed",
        "VauleOfKey": "Stg"
    }
]

OR

You can get it as a table to be more readable..

 $ aws ec2 describe-tags --filters "Name=value,Values=*tg*" --query 'Tags[*].{Key_Name: Key, VauleOfKey: Value}' --profile dev --output table
-------------------------------
|        DescribeTags         |
+--------------+--------------+
|   Key_Name   | VauleOfKey   |
+--------------+--------------+
|  SSM_Managed |  Stg         |
|  SSM_Managed |  Stg         |
|  SSM_Managed |  Stg         |
|  SSM_Managed |  Stg         |
|  SSM_Managed |  Stg         |
|  SSM_Managed |  Stg         |
|  SSM_Managed |  Stg         |
|  SSM_Managed |  Stg         |
|  SSM_Managed |  Stg         |
+--------------+--------------+

To make it more explicit before you use above, you can use the following command to be more simplistic. it will return all tags with a value of "VALUE", regardless of the tag name.:

aws ec2 describe-tags --filters "Name=value,Values=VALUE"

If you want to filter the results further, you can include additional filters by adding them to the list in the --filters flag, separated by a comma. For example, to only return tags with a value of "VALUE" that are associated with security groups, you can use the following command:

aws ec2 describe-tags --filters "Name=value,Values=VALUE","Name=resource-type,Values=security-group"

EDIT:

Using describe-security-groups with all values *SecGrpec2* and then get the name of Security group these value belongs to.

$ aws ec2 describe-security-groups --filters "Name=tag-value,Values=*SecGrpec2*" --profile dev | jq -r '.SecurityGroups[].GroupName'
EC2 - SC101
EC2 - SD102
EC2 - ST101

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM