简体   繁体   English

运行AWS CLI时更改Bash脚本变量值

[英]Bash script variable value changing when running AWS CLI

I have written this script to capture all instances in an AWS region then print out the AZ, VolumeId, InstanceId, and Volume size to a file. 我已编写此脚本来捕获AWS区域中的所有实例,然后将AZ,VolumeId,InstanceId和Volume大小打印到文件中。 The execution seems smooth until line 13. All the values are received as per expectation. 执行似乎很顺利,直到第13行。所有值都按预期收到。 I get the instance_id's required. 我得到了instance_id的要求。 I have checked the values in instance_id variable using a for loop and I have multiple ids. 我已经使用for循环检查instance_id变量中的值,并且我有多个ID。 When the script reaches line 14, there is an error saying that there is no instance id. 当脚本到达第14行时,将出现一个错误,提示没有实例ID。 The echo command shows the instance ids in the variable but the describe-instance command seems to not receive it. echo命令在变量中显示实例ID,但describe-instance命令似乎未接收到它。 Here is my script: 这是我的脚本:

#!/bin/bash
envs=("aws_cloud" "aws_dev" "aws_prod")
i=0
vols=""
echo "AvailabilityZone  ,   VolumeId    ,   InstanceId  ,   Size">>untagged_volumes.txt
for env in aws_cloud aws_dev aws_prod
    do
    for region in ca-central-1 us-east-1 us-west-2 eu-west-1 eu-central-1 ap-southeast-1 ap-southeast-2
    do
        instance_id=$(aws ec2 describe-volumes --profile=${env} --region=${region} --query 'Volumes[?!not_null(Tags[?Key== `Name`].Value) && (Attachments[?State!=""])].{InstanceId:Attachments[0].InstanceId}' --output text ) 
        for instance in "${instance_id}"
        do
            ec2_name=$(aws ec2 describe-instances --profile=${env} --region=${region} --instance-ids=${instance} --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value]' --output text)

            vol_info=$(aws ec2 describe-volumes --profile=${env} --region=${region} --filters Name=attachment.instance-id,Values=${instance} --query 'Volumes[?!not_null(Tags[?Key== `Name`].Value) && (Attachments[?State!=""])].{ID:VolumeId,InstanceId:Attachments[0].InstanceId,AZ:AvailabilityZone,Size:Size}' --output text)
            echo "${env}    ${ec2_name}""${vol_info}">>untagged_volumes.txt
        done
    done
    i=${i+1}
done

I am out of my wits trying to debug. 我不敢尝试调试。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

Edit: So after staring at the error for hours what I understood was that for one instance there was a single id returned and then a string was returned which had that instance id along with single quotes 编辑:所以在盯着错误看了几个小时之后,我了解到的是,对于一个实例,返回了一个id,然后返回了一个包含该实例ID和单引号的字符串。

An error occurred (InvalidInstanceID.NotFound) when calling the DescribeInstance' does not exist instance ID 'i-02f4a--------------- 调用DescribeInstance时不存在错误(InvalidInstanceID.NotFound)实例ID'i-02f4a ---------------

so i checked for any values i get that were greater than the length of instance id. 所以我检查了我得到的所有大于实例ID长度的值。 according to aws docs they say that the id length for instances is fixed and that is 17+2 for i and -. 根据AWS文档,他们说实例的ID长度是固定的,并且i和-为17 + 2。 After checking for any instance id longer than 19 we can only use the ids with length 19. Here is the correct code (for anyone who might need it). 检查任何实例ID大于19后,我们只能使用长度为19的ID。这是正确的代码(适用于可能需要使用此代码的任何人)。

#!/bin/bash
envs=("aws_cloud" "aws_dev" "aws_prod")
vols=""
echo "Environemnt   ,   InstanceName    ,   AvailabilityZone    ,   VolumeId    ,   InstanceId  ,   Size">>untagged_volumes.txt
for env in aws_cloud aws_dev aws_prod
    do
    for region in ca-central-1 us-east-1 us-west-2 eu-west-1 eu-central-1 ap-southeast-1 ap-southeast-2
    do
        instance_id=$(aws ec2 describe-volumes --profile=${env} --region=${region} --query 'Volumes[?!not_null(Tags[?Key== `Name`].Value) && (Attachments[?State!=""])].{InstanceId:Attachments[0].InstanceId}' --output text );
        for j in ${instance_id}
        do
            if [[ "${#j}" -gt 19 ]] || [[ "${#j}" -lt 19 ]]
            then
                continue
            else
                ec2_name=$(aws ec2 describe-instances --profile="${env}" --region="${region}" --instance-ids="${j}" --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value]' --output text)

                vol_info=$(aws ec2 describe-volumes --profile=${env} --region=${region} --filters Name=attachment.instance-id,Values="${j}" --query 'Volumes[?!not_null(Tags[?Key== `Name`].Value) && (Attachments[?State!=""])].{ID:VolumeId,InstanceId:Attachments[0].InstanceId,AZ:AvailabilityZone,Size:Size}' --output text)
                echo "${env}    ${ec2_name}     ""${vol_info}">>untagged_volumes.txt
            fi
        done
    done
done

Thank you all for your help. 谢谢大家的帮助。

我相信这部分将帮助您更正脚本

instance_id=$(aws ec2 describe-volumes --query 'Volumes[?!not_null(Tags[?Key== `Name`].Value) && (Attachments[?State!=""])].{InstanceId:Attachments[0].InstanceId}' --output text); for i in ${instance_id}; do aws ec2 describe-instances --instance-ids=${i};done

Aplogies everyone for taking you time. 道歉大家花时间。 And also for posting after so long. 而且还可以发布这么长时间。 The problem was not with the code. 问题不在于代码。 The problem was due to the gitbash terminal. 问题出在gitbash终端。 As soon as i ran my code in ubuntu terminal the code executed perfectly. 一旦我在ubuntu终端中运行代码,代码就会完美执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM