Search code examples
gogoogle-app-enginegcloudgoogle-app-engine-go

Testing app that depends on environment variables locally


Google's App Engine provides a list of predefined environment variables and additional environment variables may be defined in app.yaml. Meanwhile, the instructions for Testing and Deploying your Application just say to use go run to test the app locally. If I test my app locally inside a cloud-sdk Docker container, is there a gcloud command (or another tool) that would create the same environment variables in my local container as in App Engine? Right now I am just setting the environment variables locally with a bash script, but that means that I need to maintain the variables in multiple locations.


Solution

  • The variables are all runtime metadata. Only the runtime can provide values for these variables and then the data is specific to the deployment.

    If your app needs this metadata, you will know which variables it uses and how it uses them and, when you specify the value, you will need to provide the variable name anyway, e g. GAE_SERVICE="freddie".

    For these reasons, it's likely not useful for local testing to spoof these values for you. When you go run your app, there's nothing intrinsic about it that makes it an App Engine app. It only becomes one, after you deploy it, because it's running on the App Engine service.

    If you're running your code in a container, you can provide environment variables to the container runtime. Doing so is likely preferable to scripting these:

    GAE_SERVICE="freddie"
    docker run .... \
    --env=GAE_SERVICE=${GAE_SERVICE} \
    ...
    

    Although not really practical with App Engine, there's an argument for having your code not bind directly to any runtime (e.g. App Engine) metadata. If it does, you can't run it easily elsewhere.

    In other platforms, metadata would be abstracted further and some sort of sidecar would convert the metadata into a form that's presented consistently regardless of where you deploy it; your code doesn't change but some adapter configures it correctly for each runtime.