I want to write a bash script for automatically register and deregister target from target group in aws.
The script like this:
#!/bin/bash
target_group_name=$1
instance_Id=$2
target_group_arn="$(aws elbv2 describe-target-groups --query 'TargetGroups[?contains(TargetGroupName, `$1`)].TargetGroupArn' --output text)"
echo -n "Enter the option you want to do (enter 1 for register and enter 2 for deregister): "
read OPTION
case $OPTION in
1)
if register_output=$(aws elbv2 register-targets --target-group-arn '$target_group_arn' --targets Id="$2");
then
echo "Registering instance into target group. Please wait for 5 minutes registration progress finished"
else
echo "!!!! ERROR !!!!"
fi
;;
2)
if deregister_output=$(aws elbv2 deregister-targets --target-group-arn '$target_group_arn' --targets Id="$2");
then
echo "Deregistering instance into target group. Please wait for 5 minutes registration progress finished"
else
echo "!!!! ERROR !!!!"
fi
;;
*)
echo "unknown option"
;;
esac
However, when I run the script, no arn found.
I tried print out the $1 (S1 is inside the left operand sign ). However, it seems that bash script do not understand, it print out nothing.
Can you help me to figure out what's wrong with my script? Or how I can reproduce my query to get target group's arn so that I can pass it to my commands?
Thank you so much!
\`$1\`'$target_group_arn'be surrounded with"instead?