'GCP Cloud Function environment variable becomes unset
I am using Node.js 14 and I am trying to set a cloud function's name as one of its labels for billing purposes (I want to be able to display Cloud Function costs by function using Billing Export and BigQuery). According to docs, the environment variable K_SERVICE will hold the function's name, and I can set labels like so:
export const myFunc = functions
.runWith({
labels: {
'project-id': process.env.GCLOUD_PROJECT ?? 'not-found',
'function-name': process.env.K_SERVICE ?? 'not-found',
},
})
.https.onRequest((request, response) => {
response.json({ fname: process.env.K_SERVICE ?? 'not-found' });
});
When I deploy this code, it will fail with the following error:
Detailed stack trace: Error: Invalid labels: myFunc. Label values can only contain lowercase letters, international characters, numbers, _ or -.
I need my functions to use camelCase naming, so I can't just change the name to use all lower case letters. So I update my code as follows:
export const myFunc = functions
.runWith({
labels: {
'project-id': (process.env.GCLOUD_PROJECT ?? 'not-found').toLowerCase(),
'function-name': (process.env.K_SERVICE ?? 'not-found').toLowerCase(),
'control-label': 'MY-VAL'.toLowerCase(),
},
})
.https.onRequest((request, response) => {
response.json({ fname: (process.env.K_SERVICE ?? 'not-found').toLowerCase() });
});
Now when I deploy this version, it succeeds to deploy, but after doing a function describe using gcloud functions describe myFunc I get this in the labels section:
labels:
control-label: my-val
deployment-tool: cli-firebase
function-name: not-found
project-id: my-project-id
Notice several things here:
- My
control-labellabel works as expected, showing the value in all lower case (so functions like.toLowerCase()work inside the.runWith()method) - My
project-idlabel also works as expected, and although its original value is already in lower case, it is fetching the environment variable properly and displays it's value correctly even after applying the.toLowerCase()function. - Notably, and the purpose of this question: my
function-namelabel returns asnot-found, which is my default value if the environment variable were not set.
But I had previously verified that the environment variable K_SERVICE is indeed set, because if I don't apply the .toLowerCase() on it, the deploy will fail saying the value (properly logging its value) has upper case letters.
Also note that when I call the function and return the value as part of the .onRequest() method, it will correctly return the expected value myfunc all lowercase and matching the function's name.
So what's going on here? Why would the value of the environment variable change after having the .toLowerCase() method applied inside the .runWith() method?
Solution 1:[1]
According to the documentation the behavior is normal:
- the function-name environment variable is only available for python 3.7 and go1.11 runtime environment. For all the newest runtime, that values aren't available
- The function labels MUST be in lower case(4th bullet point)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | guillaume blaquiere |
