Amazon web services 变量中的AWS CLI实例ID

Amazon web services 变量中的AWS CLI实例ID,amazon-web-services,amazon-ec2,Amazon Web Services,Amazon Ec2,嗨,有人能帮忙吗 我在bash脚本中使用amazonaws-CLI命令,并具有以下行及其提供的输出 aws ec2 describe-instances --instance-ids $Ins --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value]' --output text ' does not existd (InvalidInstanceID.NotFound) when calling the Des

嗨,有人能帮忙吗

我在bash脚本中使用amazonaws-CLI命令,并具有以下行及其提供的输出

 aws ec2 describe-instances --instance-ids $Ins --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value]' --output text

    ' does not existd (InvalidInstanceID.NotFound) when calling the DescribeInstances operation: 

The instance ID 'i-0c7bf4181bdfxxxxx Will be backed up
如果我回显$ins的值并在命令中硬编码,如

$ echo $Ins
i-0c7bf4181bdfxxxxx

$ aws ec2 describe-instances --instance-ids i-0c7bf4181bdfxxxxx --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value]' --output text
lon-prod-xxxx-xxxx
我不明白为什么它在复制和粘贴时在命令中起作用,而在用作变量时却不起作用

额外的代码,当然有更简洁的方法可以做到这一点,但只是需要一些快速的东西。只需从一个VPC获取所有实例ID,然后尝试依次获取每个实例ID的图像

Instances=$(aws ec2  describe-instances --filter "Name=vpc-id,Values=$VPCID" --query 'Reservations[*].Instances[*].[InstanceId]' --output text)

for Ins in $Instances; do
echo $Ins
name=$(aws ec2 describe-instances --instance-ids $Ins --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value]' --output text)
echo $name Will be backed up
echo $Ins
aws ec2 create-image --instance-id $Ins --name "$name" --description "Auto backed up on $(date)" --no-reboot --$dryrun
echo "***"
done
enter code here
下面是错误,第一个id是我回显$Ins的位置,因此它似乎知道该id,但我认为它后面有a/r/n

     i-0c7bf4181bdfxxxxx

    ' does not existd (InvalidInstanceID.NotFound) when calling the DescribeInstances operation: 
The instance ID 'i-0c7bf4181bdfxxxxx Will be backed up

好的,我修正了它,变量后面有一个新行/r

增加了这一行


Ins=${Ins/$'\r'/}将其删除,现在可以正常工作

你的指挥很好。看起来变量中的实例id有问题。错误消息具有误导性。你能描述一下完整的错误信息吗?就是这样,我将在上面添加更多关于我试图运行的代码snipt的内容。我尝试了以下脚本,它的工作方式很有魅力:Ins=i-1234567;aws ec2描述实例-实例ID$Ins-查询“保留[*].instances[*].[Tags[?Key==Name].Value]'-输出文本。它打印实例名。这里也是一样,如果我手动设置变量,它会工作,但是如果我尝试获取一个列表并循环它,它似乎有问题