Reputation: 77
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!
Upvotes: 0
Views: 569
Reputation: 77
because that I have just find out the answer for my question, I will post my answer here for someone who interests and close my post.
Thank you guys so much for helping me! :D
#!/bin/bash
profile=$1
target_group_name=$1
instance_id=$2
target_group_arn="$(aws elbv2 describe-target-groups --query 'TargetGroups[].[TargetGroupArn]' --names "$2" --profile "$1" --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
Upvotes: 0
Reputation: 32354
Your target group ARN resolver looks like this:
aws elbv2 describe-target-groups \
--query 'TargetGroups[?contains(TargetGroupName, `$1`)].TargetGroupArn' \
--output text
I'm not sure of your use of backticks around the $1
variable, but:
"
). I never saw backticks used that way.$1
variable won't resolve - the AWS CLI will get the query exactly as written, not even the backticks being executed, which you would see if you run that command with echo
in the beginning.You probably want to write this instead:
aws elbv2 describe-target-groups \
--query "TargetGroups[?contains(TargetGroupName, \"$1\")].TargetGroupArn" \
--output text
Upvotes: 1