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. The execution seems smooth until line 13. All the values are received as per expectation. I get the instance_id's required. I have checked the values in instance_id
variable using a for loop and I have multiple ids. When the script reaches line 14, there is an error saying that there is no instance id. The echo
command shows the instance ids in the variable but the describe-instance
command seems to not receive it. 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
An error occurred (InvalidInstanceID.NotFound) when calling the DescribeInstance' does not exist instance ID 'i-02f4a---------------
so i checked for any values i get that were greater than the length of instance id. according to aws docs they say that the id length for instances is fixed and that is 17+2 for i and -. 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).
#!/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. As soon as i ran my code in ubuntu terminal the code executed perfectly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.