3

I am testing a cloud function.

When I run it from the "Testing" tab, online, and I paste the json dictionary as the argument to be passed as the request variable of the cloud function in the "Triggering event" editor as:

{"api_key":"MY_API_KEY"}

(with MY_API_KEY to be replaced with a plain text key) then the function runs through.

From bash, using gcloud instead, the same function fails with:

gcloud functions call MY_CLOUD_FUNCTION --data '{"api_key":"$API_KEY"}'
gcloud functions call MY_CLOUD_FUNCTION --data '{"api_key":"$API_KEY"}' 
ERROR: (gcloud.functions.call) ResponseError:
status=[404], code=[Ok], message=[Function MY_CLOUD_FUNCTION in region
us-central1 in project MY_CLOUD_PROJECT does not exist]

And this is most likely not a problem of the triggering event which just happens to be part of the args but should not play a role here. It is a problem of the different regions. gcloud expects the default region us-central1 while my function is europe-west3 (from the "Details" tab).

How can I tell gcloud to use the region of the function, and not the default region? Or what would be another way to fix this?

questionto42
  • 7,175
  • 4
  • 57
  • 90

1 Answers1

4

gcloud assumes defaults (that may be get|set using gcloud config)

In this case, you should add the flag --region=europe-west3 to your command to specify the intended region, i.e.:

REGION="europe-west3"

gcloud functions call your-function \
--region=${REGION} \
--data="{\"api_key\":\"${API_KEY}\"}"

You can verify this behavior (hopefully) by:

gcloud config list
[core]
account = your@email.com

Your active configuration is: [default]

NOTE The above command does not include functions/region; I suspect this is because us-central1 is the default (!) default value.

gcloud config get-value functions/region
us-central1

I discourage storing global state in gcloud config (partly because it results in this type of confusion) but, you may also:

gcloud config set functions/region europe-west3
Updated property [functions/region].

gcloud config get-value functions/region
europe-west3

gcloud functions call your-function \
--data="{\"api_key\":\"${API_KEY}\"}"
DazWilkin
  • 32,823
  • 5
  • 47
  • 88
  • works, also the default value is indeed `us-central1`. It seems I can also use `--data='{"api_key":"${API_KEY}"}'`, no need for the escaping backslashes. With or without the latter, I now get `ERROR: (gcloud.functions.call) ResponseError: status=[403], code=[Ok], message=[Permission denied on 'locations/europe-west-3' (or it may not exist)]' (or it may not exist)]` which is just a permission error and has nothing to do with the region error 404. Your answer still solves this. I will search for the 403 now :). – questionto42 Jan 19 '22 at 22:27
  • 1
    The region is `europe-west3` no second hyphen – DazWilkin Jan 19 '22 at 22:29
  • 1
    Yes, if you wish to use bash expansion on the `--data` value you need to use my form. If you have string literals, use yours. – DazWilkin Jan 19 '22 at 22:30
  • Ok, I took europe-west3 then, works. – questionto42 Jan 20 '22 at 09:13