What is the correct ways to write Boto3 filters to use customise tag name?

后端 未结 4 604
温柔的废话
温柔的废话 2020-12-17 15:51

I am trying to list the instances on tag values of different tag keys For eg> one tag key - Environment, other tag key - Role. My code is given below :

imp         


        
4条回答
  •  臣服心动
    2020-12-17 16:33

    This looks familiar, did I modify this for somebody somewhere ;-) . Actually the code I wrote is in rush and not tested properly (And I don't bother to amend the % string formating and replace it with str.format() ) . In fact,using Filters parameter is not properly documented in AWS.

    Please refer to Russell Ballestrini blog Filtering AWS resources with Boto3 to learn more about correct boto Filters method.

    1. Filters accept list value, and info inside the tag should be dict. thus [{}]
    2. Boto3 documentation is pretty ambiguous on how to use specify the tag name. It is confusing without examples when they say you may use tag:key. So many people will just do [{"tag:keyname","Values": [""] }] and it doesn't work. (Actually the origin code I assume the developer know how the filters works, so I just amend the structure only).
    3. Actually, You MUST explicitly specify "Name" and "Values" pair. So the correct way to specify tag name is [{"Name" :"tag:keyname", "Values":[""] }]. It is tricky.

    So the correct way of formatting a filters if you want to use for your example

    filters = [{'Name':'tag:environment', 'Values':[Env]},
               {'Name':'tag:role', 'Values':[Role]}
              ]
    

    (Update) And to make sure argparse take up string value, you just enforce the argument to take string values

    parser.add_argument('Env', type=str, default="environment",
                        help='value for   tag:environment');
    parser.add_argument('Role', type=str,default="role",
                        help='value for tag:role');
    

提交回复
热议问题