1

I need to get the Internet Gateways which are NOT attached to any of the vpc networks in aws.

Here, this below command:

igws=(aws ec2 describe-internet-gateways --query 'InternetGateways[*]' --region $REGION

echo $igws

This produces the output as below:

  1. for an attached internet gateway the Attachments block is not empty
[
    {
        "Attachments": [
            {
                "State": "available",
                "VpcId": "vpc-2e83a147"
            }
        ],
        "InternetGatewayId": "igw-5a61f333",
        "OwnerId": "<some-id>",
        "Tags": []
    }
]
  1. for an detached internet gateway the Attachments block is empty
[
    {
        "Attachments": [],
        "InternetGatewayId": "igw-0c8abdc3cb147c04d",
        "OwnerId": "<some-id>",
        "Tags": [
            {
                "Key": "Name",
                "Value": "VPNTEST"
            }
        ]
    }
]

How can I check if the Attachment value is empty or not?

I used the below command but it is not working as it doesnt capture the detached ones:

if [ -z "$igws.Attachments[]" ]; then
do
 echo "empty"
else
 echo "not empty"
fi

can someone help me?

2 Answers 2

1

If jq is available, please try:

if [[ -z $(jq '.[].Attachments[]' <<< "$igws") ]]; then
    echo "empty"
else
    echo "not empty"
fi
Sign up to request clarification or add additional context in comments.

Comments

1

maybe you can try this:

echo $igws|tr -s '\n' ' '|tr -d ' '|tr -s ',' '\n'|grep "Attachments"|awk -F':' '{print $2}'

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.