Aws-cli: aws ec2 create-tags throwing error

Created on 17 Aug 2017  路  6Comments  路  Source: aws/aws-cli

I wrote a shell script that is supposed to tag volumes and had to collect all 'Key="foo",Value=bar' in a $TAGS variable.

When I issue the aws ec2 create-tags --resources vol-xxxx --tags $TAGS from within the script, it throws this error :

Error parsing parameter '--tags': Expected: '=', received: 'K' for input: Key=Name,Value=BAR

The same command (including how the $TAGS variable is being created) would work in the command line outside of the script.

Here is the output of the command when --debug is added

debug-aws.txt

awscli version I am using

aws --version aws-cli/1.11.136 Python/2.7.12 Linux/4.10.0-32-generic botocore/1.6.3

Any idea what the issue might be?

closing-soon guidance

Most helpful comment

When you pass $TAGS it will always be seen as a single argument to the CLI. Your intention is that this string actually has multiple arguments (contained in single quotes, seperated by spaces). You can use eval to evaluate any variables, etc. in the command before it's executed:
eval aws ec2 create-tags --resources vol-xxxx --tags $TAGS

All 6 comments

Seems like the issue is some leading white space in the command.

--tags ' Key=Name,Value=PROJECT'

will fail as the leading space will break the parsing.

@joguSD Thank you for the reply.

After removing the leading space, I was still facing a similar issue. I think I have more insight now.

To create the $TAGS variable, I am using a loop over a file that contains in its first column the tag name and in its second column the value.

The command looks like this :
while read -r line; do read Key Value <<< $(echo "$line" | awk '{print $1" "$2}'); TAGS="$TAGS""'Key=\"$Key\",Value=$Value' "; done < "$FILE"

Now when I print the value of $TAGS (e.g. using echo, I get the expected format) :

'Key="Name",Value=Name' 'Key="Owner",Value=mdallali' 'Key="Project",Value=Proejct' 'Key="Region",Value=EU'

However, aws seems to "see" the the characters that are escaped by "\" in the linux echo command (which look to be contained in the variable) and will still see them : here is part of the --debug output where I can see this is what is happening :
2017-08-18 13:52:06,067 - MainThread - awscli.clidriver - DEBUG - Arguments entered to CLI: ['ec2', 'create-tags', '--resources', 'vol-02466b71046481876', '--tags', '\'Key="Name",Value=Name\' \'Key="Owner",Value=mdallali\' \'Key="Project",Value=Project\' \'Key="Region",Value=EU\' ', '--debug']

Desired output would be :
2017-08-18 14:28:42,655 - MainThread - awscli.clidriver - DEBUG - Arguments entered to CLI: ['ec2', 'create-tags', '--resources', 'vol-02466b71046481876', '--tags', 'Key="Name",Value=Name', 'Key="Owner",Value=mdallali', 'Key="Project",Value=Project', 'Key="Region",Value=EU', '--debug']

Not sure if the way I formulated this makes sense but please let me know if you can think of a way I can work around this.

Appreciate your help

Pretty sure this is an issue with how the quotes are being applied / passed into the program.
Try this instead (I removed the single quotes).

TAGS="$TAGS""Key=\"$Key\",Value=$Value ";

You had your single quotes in the double quotes meaning the program you're passing these to will see them as literal characters.

That would not work either as the expected value needs the single quotes at the beginning and end of each of the Key="Foo",Value=Bar blocks [from the create-tags documentation, Section To add tags with special characters ] That syntax is needed to escape the special characters if they are present in the Key.

What I did is the following :
TAGS="$TAGS""Key=$Key,Value=$Value "

This will obviously not cover the case where the Key contains special characters.

My current use cases can survive this so I will ignore support for special characters for the time being until I can figure this out.

Please let me know if you have any other ideas and feel free to close this case if you think no further help can be provided.

Thanks.

When you pass $TAGS it will always be seen as a single argument to the CLI. Your intention is that this string actually has multiple arguments (contained in single quotes, seperated by spaces). You can use eval to evaluate any variables, etc. in the command before it's executed:
eval aws ec2 create-tags --resources vol-xxxx --tags $TAGS

Another for these tricky bash cases is to use --cli-input-json which will load the arguments from a json file so you don't have to worry about bashisms.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ikim23 picture ikim23  路  3Comments

ronaldpetty picture ronaldpetty  路  3Comments

rahul003 picture rahul003  路  3Comments

maanbsat picture maanbsat  路  3Comments

kangman picture kangman  路  3Comments