Putnik
Putnik

Reputation: 6854

AWS EC2 and AMI tags with spaces using CLI

I try to create an instance using CLI, first like described in the doc (https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/run-instances.html, Example 4):

aws ec2 run-instances ... --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=val}]' 'ResourceType=volume,Tags=[{Key=Name,Value=val}]'

and it works well. The problem starts when I try to move whole tag-specifications in a separate bash variable, I need it because in fact there are many tags and they are built during the script run. So I do, but first use :

tags="--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=val}]' 'ResourceType=volume,Tags=[{Key=Name,Value=val}]'"   

aws ec2 run-instances ... $tags

and it fails:

Error parsing parameter '--tag-specifications': Expected: '=', received: ''' for input:
'ResourceType=instance,Tags=[{Key=Name,Value=val}]'
^

If I remove single quotes then it works:

tags="--tag-specifications ResourceType=instance,Tags=[{Key=Name,Value=val_no_spaces}] ResourceType=volume,Tags=[{Key=Name,Value=val_no_spaces}]" # just works.

Despite it works, it's not good to me because if the values have spaces it stops working again:

tags="--tag-specifications ResourceType=instance,Tags=[{Key=Name,Value=val with spaces}] ResourceType=volume,Tags=[{Key=Name,Value=val with spaces}]"                                                

Error parsing parameter '--tag-specifications': Expected: ',', received: 'EOF' for input:
ResourceType=instance,Tags=[{Key=Name,Value=val
                                               ^

I tried to wrap val into \", \' - neither works to me.

The same behavior is if you run very similar command aws ec2 create-image.

So, how can I add tags with spaces in the value and having them in a separate variable?

Upvotes: 3

Views: 1691

Answers (1)

deimagjas
deimagjas

Reputation: 58

I had the same problem with aws cli. I solved it using arrays. In your case I would do the following:


tags=(
    --tag-specifications
    'ResourceType=instance,Tags=[{Key=Name,Value=val}]' 
    'ResourceType=volume,Tags=[{Key=Name,Value=val with spaces}]'
)

aws ec2 run-instances ... "${tags[@]}"

Upvotes: 2

Related Questions