1

I'm trying to parse JSON output from the AWS CLI. What I'm looking for are security group names with specific tags below them. The two commands that work are

$aws ec2 describe-security-groups | jq  -r '.SecurityGroups[].GroupName'
default
mysqlsg
apachesg
default

Then I run

$ aws ec2 describe-security-groups | jq  -r '.SecurityGroups[].Tags[]|select(.Key == "Service")'
{
  "Key": "Service",
  "Value": "default"
}
{
  "Key": "Service",
  "Value": "MySQL"
}
{
  "Key": "Service",
  "Value": "Apache"
}
{
  "Key": "Service",
  "Value": "default"
}

I'd like each group to have the Service Tag below it so I tried this but it didn't work.

$ aws ec2 describe-security-groups | jq  -r '.SecurityGroups[].GroupName,.SecurityGroups[].Tags[]|select(.Key == "Service")'
jq: error (at <stdin>:225): Cannot index string with string "Key"
2
  • what do you mean by "I'd like each group to have the Service Tag"> Commented Jul 17, 2020 at 12:36
  • Each security group has the tag with Key Service. I'd like to display the Group Name with the Tags that have Key Service and it's value below it. Commented Jul 17, 2020 at 12:45

1 Answer 1

1

You can do this with aws-cli query parameters, try the below and it should work.

aws ec2 describe-security-groups --query 'SecurityGroups[].{Tags:Tags[?Key==`Name`].Value|[0],GroupName:GroupName}'

output

    {
        "Tags": "demo",
        "GroupName": "demo"
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.