0

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!

4
  • 1
    Have you tried escaping backquotes? Like \`$1\` Commented Dec 3, 2019 at 9:51
  • I have just tried and aws-cli say that it produces wrong syntax Commented Dec 3, 2019 at 9:55
  • 2
    Shouldn't '$target_group_arn' be surrounded with " instead? Commented Dec 3, 2019 at 9:55
  • Hi Trinh! Can you specify which error message do you get, and from which line? Commented Dec 3, 2019 at 10:02

2 Answers 2

1

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:

  1. backticks are used in shell to execute the command inside the backticks and resolve to the output of the command, which is clearly not what you want.
  2. The AWS query would work well with double quotes ("). I never saw backticks used that way.
  3. Regardless - because the whole query is single quoted, your $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
Sign up to request clarification or add additional context in comments.

1 Comment

it should be backstick :D this is the syntax of awscli, if you use "", it will result in error. However, I have found answer for my own question. Thank you so much for your help!
0

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

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.