使用 CLI 的带空格的 AWS EC2 和 AMI 标签
AWS EC2 and AMI tags with spaces using CLI
我尝试使用 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}]'
而且效果很好。当我尝试将整个 tag-specifications
移动到一个单独的 bash 变量时,问题就开始了,我需要它,因为实际上有很多标签,它们是在脚本 运行 期间构建的。所以我这样做,但首先使用 :
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[@]}"
我尝试使用 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}]'
而且效果很好。当我尝试将整个 tag-specifications
移动到一个单独的 bash 变量时,问题就开始了,我需要它,因为实际上有很多标签,它们是在脚本 运行 期间构建的。所以我这样做,但首先使用 :
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[@]}"