How environment variables work
Environment variables are set in the Runpod console and are available to your handler at runtime throughos.environ. Your handler can read these variables to configure its behavior.
Set environment variables
You can set environment variables in the Runpod console when creating or editing your endpoint:- Navigate to your endpoint in the Runpod console.
- Click on the Settings tab.
- Scroll to the Environment Variables section.
- Add your variables as key-value pairs.
- Click Save to apply the changes.
Access environment variables in your handler
You can access environment variables in your handler function at runtime usingos.environ.get("VARIABLE_NAME"). For example:
handler.py
Build-time vs runtime variables
There are two types of environment variables:Build-time variables
Build-time variables are set in your Dockerfile using theENV instruction. These are baked into your Docker image during the build:
Dockerfile
Runtime variables
Runtime variables are set in the Runpod console and can be changed without rebuilding your image. These override build-time variables with the same name: Runtime variables are useful for:- API keys and secrets.
- Environment-specific configuration (dev, staging, prod).
- Values that change frequently.
- Sensitive information that shouldn’t be in your image.
Common use cases
API keys and secrets
Store sensitive credentials as runtime environment variables:handler.py
S3 bucket configuration
Configure S3 or S3-compatible storage for uploading results:handler.py
BUCKET_ENDPOINT_URL: Your bucket endpoint (e.g.,https://your-bucket.s3.us-west-2.amazonaws.com)BUCKET_ACCESS_KEY_ID: Your access key IDBUCKET_SECRET_ACCESS_KEY: Your secret access key
The
BUCKET_ENDPOINT_URL should include your bucket name in the URL.Feature flags
Use environment variables to enable or disable features:handler.py
Model configuration
Configure model parameters without changing code:handler.py
Best practices
Use defaults
Always provide default values for non-critical environment variables:handler.py
Validate on startup
Validate critical environment variables when your handler starts:handler.py
Document your variables
Document the environment variables your handler expects in your README:README.md
Separate secrets from config
Use different approaches for secrets vs configuration:- Secrets: Only set as runtime variables in the Runpod console.
- Configuration: Can use build-time defaults with runtime overrides.