How to Set Environment Variables in Google App Engine
To set environment variables in Google App Engine, add an
env_variables section in your app.yaml file with key-value pairs. This works for both standard and flexible environments and allows your app to access these variables at runtime.Syntax
The env_variables section in app.yaml defines environment variables as key-value pairs. Each key is the variable name, and the value is its string value.
- env_variables: Starts the environment variables block.
- KEY: The name of the environment variable.
- VALUE: The string value assigned to the variable.
yaml
env_variables: KEY1: "value1" KEY2: "value2"
Example
This example shows how to set two environment variables, API_KEY and ENV, in the app.yaml file. Your application can then read these variables at runtime.
yaml
runtime: python39 env_variables: API_KEY: "12345-abcde" ENV: "production"
Common Pitfalls
Common mistakes when setting environment variables in App Engine include:
- Not quoting string values, which can cause parsing errors.
- Placing
env_variablesoutside the root level ofapp.yaml. - Trying to set environment variables in the flexible environment without redeploying the app.
- Using environment variables for sensitive data without encryption or secret management.
yaml
Wrong: env_variables: API_KEY: 12345-abcde # Missing quotes can cause issues Right: env_variables: API_KEY: "12345-abcde"
Quick Reference
Remember these tips when setting environment variables in App Engine:
- Always use quotes around string values.
- Define
env_variablesat the root level inapp.yaml. - Redeploy your app after changing environment variables.
- Use Google Secret Manager for sensitive data instead of plain environment variables.
Key Takeaways
Set environment variables in the app.yaml file under the env_variables section as key-value pairs.
Always quote string values to avoid parsing errors.
Redeploy your App Engine app after changing environment variables to apply updates.
Avoid storing sensitive data directly in env_variables; use Secret Manager instead.
Place env_variables at the root level of app.yaml for proper recognition.