Pradnyesh Choudhari
07/28/2025, 3:09 PMMarvin
07/28/2025, 3:09 PMMarvin
07/28/2025, 3:10 PMenv
DATABASE_URL=<postgresql://user:pass@localhost:5432/db>
API_KEY=your-secret-key
2. Load the .env file
You'll need to ensure your .env file is loaded before running Prefect commands. You can do this in two ways:
a. Using python-dotenv in your deployment script:
python
from dotenv import load_dotenv
load_dotenv()
b. Or using the python-dotenv CLI when running commands:
bash
dotenv run prefect deploy
3. Reference environment variables in prefect.yaml
Use the {{ $ENV_VAR_NAME }} syntax to reference environment variables:
yaml
deployments:
- name: my-deployment
entrypoint: flow.py:my_flow
work_pool:
name: my-pool
job:
variables:
DATABASE_URL: "{{ $DATABASE_URL }}"
API_KEY: "{{ $API_KEY }}"
4. Deploy your flow
When you run prefect deploy, Prefect will automatically substitute the environment variables from your local environment into the deployment configuration.
Important Notes:
- Environment variables must be present in the environment when you run prefect deploy
- The values will be stored securely in the Prefect database
- You can mix static values and environment variables in your prefect.yaml
- For sensitive values, consider using Prefect Blocks instead, especially for credentials
Example with mixed static and environment variables:
yaml
deployments:
- name: my-deployment
entrypoint: flow.py:my_flow
work_pool:
name: my-pool
job:
variables:
DATABASE_URL: "{{ $DATABASE_URL }}"
API_KEY: "{{ $API_KEY }}"
RETRY_ATTEMPTS: 3 # static value
ENVIRONMENT: "production" # static value
For more details and advanced configurations, you can refer to:
- Customizing Job Variables Documentation
- Prefect YAML Configuration Guide
Let me know if you need any clarification or have questions about specific use cases!