Home > Blockchain >  combine multiple aws cli calls to get tag values
combine multiple aws cli calls to get tag values

Time:04-20

I have a script that fetches list of instances having tag x having abc value. The count of ec2 instances returned are in hundreds, now for each instance I need to fetch 2 tag values. Not all instances will have both the tags, it could be 1 or both or none. For now I am issuing 2 calls to get the value of each tag (this is a bash shell)

 market=`aws ec2 describe-tags --filters "Name=resource-id,Values=$id" "Name=key,Values=market" --query Tags[].Value --region $aws_region --output text`
service=`aws ec2 describe-tags --filters "Name=resource-id,Values=$id" "Name=key,Values=service" --query Tags[].Value --region $aws_region --output text`

Is there any way to fetch the values of both tags in a single call?

CodePudding user response:

I have 4 instances like this:

  • i-020f43a6253e1dd25 tags:market=1
  • i-0a5c4b42fe3e75c15 tags:service=1
  • i-027ca3de0fe11f1d3 tags:market=4,service=4
  • i-0e77b17601f9b2fd2 tags:none

Server side filtering using --filters returns 4 matching records

% aws ec2 describe-tags --filters "Name=key,Values=market,service"
{
    "Tags": [
        {
            "Key": "market",
            "ResourceId": "i-020f43a6253e1dd25",
            "ResourceType": "instance",
            "Value": "1"
        },
        {
            "Key": "market",
            "ResourceId": "i-027ca3de0fe11f1d3",
            "ResourceType": "instance",
            "Value": "4"
        },
        {
            "Key": "service",
            "ResourceId": "i-027ca3de0fe11f1d3",
            "ResourceType": "instance",
            "Value": "4"
        },
        {
            "Key": "service",
            "ResourceId": "i-0a5c4b42fe3e75c15",
            "ResourceType": "instance",
            "Value": "1"
        }
    ]
}
  • Related