<@ULVA73B9P> how to use environment variables from...
# ask-marvin
p
@Marvin how to use environment variables from .env file in YAML based deployments
m
thought for 42.6 seconds
Let me help you understand how to use environment variables from a .env file in your YAML-based deployments. Here's a comprehensive guide: 1. Create your .env file
Copy code
env
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:
Copy code
python
from dotenv import load_dotenv
load_dotenv()
b. Or using the
python-dotenv
CLI when running commands:
Copy code
bash
dotenv run prefect deploy
3. Reference environment variables in prefect.yaml Use the
{{ $ENV_VAR_NAME }}
syntax to reference environment variables:
Copy code
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:
Copy code
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!