1

Consider the AWS CLI commands in a script file

#!/bin/bash
msgClr='\033[1;35m'
errClr='\033[1;31m'
cynClr='\033[1;36m'
NC='\033[0m'
#Read CLuster from command Args
cluster="MyCluster"

#Update Service
echo -e "${msgClr}Initiate: ${NC}Updating Service..."
serviceName=$(aws ecs list-services --cluster $cluster | jq '.serviceArns[0]')
allTaskDefs=$(aws ecs list-task-definitions | jq '.taskDefinitionArns')
taskDefLength=$(echo $allTaskDefs | jq '. | length')
latestTaskDefArn=$(echo $allTaskDefs | jq '.['`expr $taskDefLength - 1`']')
#latestTaskDef=$(echo $latestTaskDefArn | awk 'BEGIN{FS="/"}{print substr($2, 1, length($2)-1)}')
#echo -e "${msgClr}Using Task Definition: ${cynClr}"$latestTaskDef"${NC}"
echo "aws ecs update-service --cluster $cluster --task-definition $latestTaskDefArn --service $serviceName" 
$(aws ecs update-service --cluster $cluster --task-definition $latestTaskDefArn --service $serviceName)
echo -e "${msgClr}Complete: ${NC}Updating Service..."

It fails with

An error occurred (InvalidParameterException) when calling the UpdateService operation: Invalid revision number. Number: aws:ecs:eu-west-1:630026061543:task-definition/ecscompose-production:33"

but if I echo out the last command

$(aws ecs update-service --cluster $cluster --task-definition $latestTaskDefArn --service $serviceName)

and copy paste it, it runs smoothly. Why and what am I doing wrong?

user1872371
  • 139
  • 10
  • Can you add **--debug** to the update-service command and verify that the debug print says that same revision number is sent with both calls? – Jonatan Jan 17 '18 at 12:30
  • 2
    Are you sure `aws ecs update-service` will print a valid Bash command? What exactly do you expect it to do? – tripleee Jan 17 '18 at 12:33
  • 1
    In passing, is there any valid reason to hard-code terminal-specific escape codes instead of using `tput` to keep it portable? – Toby Speight Jan 17 '18 at 13:13
  • @TobySpeight The only argument I can think of is that tput takes some milliseconds to run... – Camusensei Jan 17 '18 at 13:23
  • @Camusensei **Milli**seconds? Really? That's a very slow system you're running it on. – Toby Speight Jan 17 '18 at 13:27
  • No need to bash me, I'm just trying to help here :) FYI `3 to 7ms` for a linux server, `1ms` for a VM and `25ms` for cygwin. Don't ask me to explain these values – Camusensei Jan 17 '18 at 14:09

1 Answers1

2

Thank you for all the comments. I think I found why it wasn't working.

It seems that AWS CLI commands from within a bash-script file don't work if the task-definition or the service are full ARNs (contrary to the documentation). My script started working as soon as I stripped the task-definition and the service to their name(and revision number) by removing the arn:aws:..<up to>../ part.

Just to be clear, the full arn for task-definitions and service do work from the command prompt...just not from withinthe bash-script.

user1872371
  • 139
  • 10