Skip to content

Commit 602d2b0

Browse files
authored
Update for compatibility with Windows GitBash
This proposal is based on a customer case which revealed a bug when running this script in GitBash on Windows. Please see how the query fails in GitBash on Windows: ——— $ ./findEniAssociations --eni "eni-0c9f62047560173ee" --region "us-east-1" This script is for determining why an ENI that is managed by AWS Lambda has not been deleted. using Security Groups sg-08fa5776048baa8ccc57c3be6142316 and Security Groups sg-08fa5776048baa8cc... subnet-082c57c3be6142316 Bad value for --query {"NextToken": NextToken, "VpcConfigsByFunction": Functions[?VpcConfig!=`null` && VpcConfig.SubnetIds!=`[]`] | [].{Arn:FunctionArn, Subnets:VpcConfig.SubnetIds, SecurityGroups: VpcC:) == `true`] }: Bad jmespath expression: Bad token subnet-082c57c3be6142316 {"NextToken": NextToken, "VpcConfigsByFunction": Functions[?VpcConfig!=`null` && VpcConfig.SubnetIds!=`[]`] | [].{Arn:FunctionArn, Subnets:VpcConfig.SubnetIds, SecurityGroups: VpcConfig.SecurityGroupIds`) == `true`] }Subnets, `subnet-082c57c3be6142316 ——— The proposed changes are two-fold: 1. Includes the -a option (or --ascii-output) on 3 of the jq commands. The -a option forces jq to produce pure ASCII, which causes some issues due to how different operating systems interpret newlines and escape sequences[1][2]. 2. Uses a variable SUBNET_STRING to hold the value of the subnet(s), the format of which was causing an issue for the same reason. This updated version has been tested by me on Windows_Server-2022-English-Full-Base-2022.12.14, Amazon Linux 2 AMI, and Apple M1 MacBook. [1] https://stedolan.github.io/jq/manual/ [2] https://www.aleksandrhovhannisyan.com/blog/crlf-vs-lf-normalizing-line-endings-in-git/#:~:text=Whereas%20Windows%20follows%20the%20original,the%20era%20of%20the%20typewriter.
1 parent 3470f4c commit 602d2b0

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

Lambda/FindEniMappings/findEniAssociations

+7-5
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,19 @@ fi
5353
# search for the ENI to get the subnet and security group(s) it uses
5454
METADATA="$(aws ec2 describe-network-interfaces --network-interface-ids ${ENI} --filters Name=network-interface-id,Values=${ENI} --region ${REGION} --output json --query 'NetworkInterfaces[0].{Subnet:SubnetId,SecurityGroups:Groups[*].GroupId}')"
5555

56-
read Subnet < <(echo $METADATA | jq -r '.Subnet')
56+
read Subnet < <(echo $METADATA | jq -ar '.Subnet')
5757
SecurityGroups=()
58-
for row in $(echo $METADATA | jq -r '.SecurityGroups[]')
58+
for row in $(echo $METADATA | jq -ar '.SecurityGroups[]')
5959
do
6060
SecurityGroups+=(${row})
6161
done
6262
# Sort the list of SGs, so that we can easily compare with the list from a Lambda function
6363
IFS=$'\n' SortedSGs=($(sort <<<"${SecurityGroups[*]}"))
6464
unset IFS
65-
echo "Found "${ENI}" with "$Subnet" using Security Groups" ${SortedSGs[@]}
66-
echo "Searching for Lambda function versions using "$Subnet" and Security Groups" ${SortedSGs[@]}"..."
65+
#convert Subnet to "echo-able", if $Subnet is used directly, GitBash skips the call outputting: ' using Security Groups "sg-012345example" '
66+
SUBNET_STRING=$(echo $Subnet)
67+
echo "Found "${ENI}" with $SUBNET_STRING using Security Groups" ${SortedSGs[@]}
68+
echo "Searching for Lambda function versions using "$SUBNET_STRING" and Security Groups" ${SortedSGs[@]}"..."
6769

6870
# Get all the Lambda functions in an account that are using the same subnet, including versions
6971
Functions=()
@@ -89,7 +91,7 @@ for each in "${Functions[@]}"
8991
do
9092
# Check if there are any functions that match the security groups of the ENI
9193
LambdaSGs=()
92-
for row in $(echo "$each" | jq -r '.SecurityGroups[]')
94+
for row in $(echo "$each" | jq -ar '.SecurityGroups[]')
9395
do
9496
LambdaSGs+=(${row})
9597
done

0 commit comments

Comments
 (0)