Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Zone variable in gcp compute command is giving error.

I am taking zone value from a csv file . i have created variable of zone and trying to use below command-

 
while IFS=","  read -r vm_Name  zone; do   
....
       vm_info=$(gcloud compute instances describe "$Instance_Name"  --zone '$zone' )
 .....
 

ERROR: (gcloud.compute.instances.describe) Could not fetch resource:
- Invalid value for field 'zone': '$zone'. Must be a match of regex '[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?'

 

it's working fine when i am using direct value . what could be the issue 

0 4 2,833
4 REPLIES 4

Try

vm_info=$(gcloud compute instances describe "$Instance_Name"  --zone=$zone)

 

..... NoCommandLine ......
 https://nocommandline.com
A GUI for Google App Engine

 

Thank you for the response. I tried using it but it's not working.

csv file -

Instance_Name,Project_Id,Zone
instance-1,dev,us-central1-a
instance-1,prod,us-central1-a

Please find my script.

    IFS=',' read -r  header _ < file.csv
    while IFS=','  read -r  Instance_Name Project_Id Zone; do  
         gcloud config set project $Project_Id  
         echo "This is $Zone"

     info=$(gcloud compute instances describe "$Instance_Name"  --zone=$Zone --format='value(name)') >/dev/null 2>&1
       done < <(tail -n +2 file.csv)
 
it is giving below error -

Updated property [core/project].
This is us-central1-a
ERROR: (gcloud.compute.instances.describe) Could not fetch resource:
- Invalid value for field 'zone': '$Zone'. Must be a match of regex '[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?'

Updated property [core/project].
This is us-central1-a
ERROR: (gcloud.compute.instances.describe) Could not fetch resource:
- Invalid value for field 'zone': '$Zone'. Must be a match of regex '[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?'

I tried debugging using $Zone with "" , ''   and without any quotes , it is still giving same error. but tried echo command also to check the value ,it is giving correct zone name as in above output. 

hardcoded value name is giving correct output but with variable $Zone , it's giving regex error.

Hello @gcloudLearning ,

Welcome to Google Cloud Community!

You can check out this Stack Overflow post as you might have a similar case. According to @Alex Martelli, your value must match the regex, which specifies that it should be all lowercase. Please verify the letter case of your variable to resolve any issues causing the regex error.

If the issue still persists, you may contact Google Cloud Support for further assistance. Thank you.

Hey, Have you got the solution?