我尝试使用CLI创建一个实例,首先像文档(https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/run-instances.html,示例4)中描述的那样:
aws ec2 run-instances ... --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=val}]' 'ResourceType=volume,Tags=[{Key=Name,Value=val}]'
效果很好。当我试图在一个单独的bash变量中移动整个tag-specifications
时,问题就开始了,我需要它,因为实际上有许多标签,它们是在脚本运行期间构建的。所以我这样做了,但首先使用:
tags="--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=val}]' 'ResourceType=volume,Tags=[{Key=Name,Value=val}]'"
aws ec2 run-instances ... $tags
失败:
Error parsing parameter '--tag-specifications': Expected: '=', received: ''' for input:
'ResourceType=instance,Tags=[{Key=Name,Value=val}]'
^
如果我去掉单引号,它就可以工作了:
tags="--tag-specifications ResourceType=instance,Tags=[{Key=Name,Value=val_no_spaces}] ResourceType=volume,Tags=[{Key=Name,Value=val_no_spaces}]" # just works.
尽管它可以工作,但对我来说不是很好,因为如果值有空格,它会再次停止工作:
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
^
我试图将val包装成"
,'
-两者都不适合我。
如果您运行非常相似的命令aws ec2 create-image
,则会出现相同的行为。
那么,我如何在值中添加带有空格的标签并将它们放在单独的变量中呢?
我在使用aws cli时遇到了同样的问题。我用数组解决了它。在您的情况下,我会做以下操作:
tags=(
--tag-specifications
'ResourceType=instance,Tags=[{Key=Name,Value=val}]'
'ResourceType=volume,Tags=[{Key=Name,Value=val with spaces}]'
)
aws ec2 run-instances ... "${tags[@]}"