user1872371
user1872371

Reputation: 341

AWS trouble in Bash Script

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?

Upvotes: 6

Views: 3428

Answers (2)

Nate Diamond
Nate Diamond

Reputation: 51

For anyone stumbling on this - it is because of the way jq output is formatted - try adding --raw-output option to your jq command

Upvotes: 5

user1872371
user1872371

Reputation: 341

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.

Upvotes: 6

Related Questions