Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google cloud functions default environment variables not set

Are there any conditions to the default environment variables being set on google cloud function?

I have the following code:

func init() {
    projectID := os.Getenv("GCP_PROJECT")
    log.Printf("projectID: %s\n", projectID)

    functionName := os.Getenv("FUNCTION_NAME")
    log.Printf("functoinName: %s\n", functionName)

    region := os.Getenv("FUNCTION_REGION")
    log.Printf("region: %s\n", region)
}

and the values are empty.

Even if I do:

func GameUpdate(ctx context.Context, e FirestoreEvent) error {
    functionName := os.Getenv("FUNCTION_NAME")
    log.Printf("functoinName: %s\n", functionName)
}

They are still empty.

According to documentation, I would expect them to be set and available. But they are not :|

EDIT:

I am using go 1.13 as runtime and as Armatorix mentioned, these env variables are not available in that runtime...

Why I needed them was to write a wrapper for cloud.google.com/go/logging to be able to tag the severity of the logs.

I ended up prepending my stdout logs with [INFO]/[ERROR], and creating a tag from it \[([A-Z]+)\].*. Bonus is that I don't have to do a network call in my function to ship the logs.

Still disappointing that these environment variables are not available.

like image 266
maclir Avatar asked Jul 04 '26 19:07

maclir


1 Answers

So I've read the same documentation. Here you've got the info that it works like this with go1.11 (And it works, I tested it out). BUT for go1.13 these are not set. You can still do it manually. Also I've checked which envs are set on 1.13 version.

From os.Envrion()

PATH=/layers/google.go.build/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
FUNCTION_SIGNATURE_TYPE=http
DEBIAN_FRONTEND=noninteractive
HOME=/root
K_REVISION=9
FUNCTION_TARGET=HelloWorld
PORT=8080
GOROOT=/usr/local/go/
CGO_ENABLED=1
PWD=/srv
K_SERVICE=function-1

So the env that you probably want to use is K_SERVICE

like image 181
Armatorix Avatar answered Jul 10 '26 00:07

Armatorix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!