1

I want to parse the output of AWS CLI command

aws ec2 describe-transit-gateways

enter image description here

enter image description here

From the above, the output I want to get is the name of the transit gateway which is given as a tag example: ("Name": "dev-tgw")

I am able to retrieve TGW ID and Transit Gateway Owner from the below script

result=`aws ec2 describe-transit-gateways --profile $acc`

  for tgw in $(echo "${result}" |jq -r '.TransitGateways[] | @base64'); do

    _jq() {

      echo ${tgw} | base64 --decode | jq -r ${1}

    }
    
    Tgw=$(_jq '.TransitGatewayId')
    Tgw_owner=$(_jq '.OwnerId')
    Tgw_name=$(_jq '.Tags.Name')

    echo "$acc.Name"
    echo "Transit_Gateway": "$Tgw"
    echo "TGW_Owner_ID": "$Tgw_owner"
    echo "TGW_name": "$Tgw_name"

But I am not able to retrieve the Name of the TGW using

 Tgw_name=$(_jq '.Tags.Name')
 echo "TGW_name": "$Tgw_name"

What am I missing or am I looping it wrong?

1 Answer 1

1

You can try the following version:

result=$(aws ec2 describe-transit-gateways --profile $acc)

for tgw in $(echo "${result}" | jq -r '.TransitGateways[] | @base64'); do

    _jq() {
      echo ${tgw} | base64 --decode | jq -r "${1}"
    }
    
    Tgw=$(_jq '.TransitGatewayId')
    Tgw_owner=$(_jq '.OwnerId')
    Tgw_name=$(_jq '.Tags[] | select(.Key == "Name").Value')
    
    echo "$acc.Name"
    echo "Transit_Gateway": "$Tgw"
    echo "TGW_Owner_ID": "$Tgw_owner"
    echo "TGW_name": "$Tgw_name"
    
done
Sign up to request clarification or add additional context in comments.

3 Comments

``` jq: error: Could not open file |: No such file or directory jq: error: Could not open file select(.Key: No such file or directory jq: error: Could not open file ==: No such file or directory jq: error: Could not open file "Name").Value: No such file or directory
@Balakrishna did you copy exactly all code, not only one line of it?
@Balakrishna No problem. Glad it worked out:-)

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.