0

I have a shell script as given below. This script actually add AWS instance in autoscalling scale in protection group. When I run individual commands that went fine. But when I created a shell file and tried to execute same there are error. See below script

set -x
INSTANCE_ID=$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-id)
ASG_NAME=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID"  --region us-east-2 | jq '.Tags[] | select(.["Key"] | contains("a:autoscaling:groupName")) | .Value')
ASG_NAME=$(echo $ASG_NAME | tr -d '"')
aws autoscaling set-instance-protection --instance-ids $INSTANCE_ID --auto-scaling-group-name $ASG_NAME  --protected-from-scale-in --region us-east-2

error is as given below. I think issue is with second line. It is not able to get ASG_NAME, I tried some of escape character but nothing is working.

+++ wget -q -O - http://169.254.169.254/latest/meta-data/instance-id
++ INSTANCE_ID=i-----
+++ aws ec2 describe-tags --filters Name=resource-id,Values=i------ --region us-east-2
+++ jq '.Tags[] | select(.["Key"] | contains("a:autoscaling:groupName")) | .Value'
++ ASG_NAME=
+++ echo
+++ tr -d '"'
++ ASG_NAME=
++ aws autoscaling set-instance-protection --instance-ids i---- --auto-scaling-group-name --protected-from-scale-in --region us-east-2
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help
aws: error: argument --auto-scaling-group-name: expected one argument

> Blockquote
Jaishree Mishra
  • 545
  • 2
  • 5
  • 24
  • 1
    What are you trying to do? With the spirit of this forum, what is your question? So notes: Don't use \` backticks, they are strongly discouraged. Use `$(...)` instead. [Obsolete and deprecated bash syntax](https://wiki-dev.bash-hackers.org/scripting/obsolete). And [when should you quote variables](https://stackoverflow.com/questions/35847655/when-should-i-quote-variables). Also [how to debug bash script](https://stackoverflow.com/questions/951336/how-to-debug-a-bash-script). Try adding `set -x` to inspect the script. – KamilCuk Feb 26 '20 at 14:08
  • Agree with all above, and until you get good at things, you'll want to include line like `printf "#dbg: INSTANCE_ID=[${INSTANCE_ID:-unset}]\n" 1>&2` for each variable you create. Good luck! – shellter Feb 26 '20 at 14:21
  • 1
    I see, I updated question, @KamilCuk, I also update script as well. – Jaishree Mishra Feb 26 '20 at 14:26
  • `++ ASG_NAME=` - see, so the `ASG_NAME` variable is empty. Must be something with that second `aws` or `jq`. Could be `aws ec2 describe-tags` outputs empty? Could be `jq '...| .Value'` outputs nothing? – KamilCuk Feb 26 '20 at 14:26
  • You don't necessarily need `jq` here. `aws` implements its own JSON processing using the `--query` argument and [JMESPath](http://jmespath.org). – chepner Feb 26 '20 at 14:28
  • @KamilCuk yes issue is with second line, what can be correct syntax, Second line output is nothing – Jaishree Mishra Feb 26 '20 at 14:40
  • @chepner how can use same in my scenario, specially second line. – Jaishree Mishra Feb 26 '20 at 14:40
  • try to change your seconde line with " around, like this : "INSTANCE_ID=$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-id)" same with next line but with escaping already " in it, like this : "ASG_NAME=$(aws ec2 describe-tags --filters \"Name=resource-id,Values=$INSTANCE_ID\" --region us-east-2 | jq '.Tags[] | select(.[\"Key\"] | contains(\"a:autoscaling:groupName\")) | .Value')" – Hayha Feb 26 '20 at 15:08

1 Answers1

1

Solved issue by recommendation of @chepner. Modified second line by

ASG_NAME=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID"  --region us-east-2 --query 'Tags[1].Value')
Jaishree Mishra
  • 545
  • 2
  • 5
  • 24