4.1 Environment Variables

Lesson

4.1 Environment Variables

Environment variables provide a powerful way to manage configuration settings and secrets directly from your operating system or deployment environment. This approach keeps sensitive information completely separate from your codebase, making it ideal for teams that follow infrastructure-as-code practices or work with containerized deployments.

When using environment variables in Mage, you can reference them directly in your pipeline code or configuration files. This method is particularly useful when deploying across multiple environments (development, staging, production) where connection details may differ but the code remains the same.

Reference environment variables in code (python):

api_key = os.environ.get('API_KEY')

Reference environment variables in code (YAML):

password: "{{ env_var('DATABASE_PASSWORD') }}"


Here’s how to create environment variables in Mage Pro:

  • From the Mage Pro cluster manage click the blue cluster link to open

  • Next click the blue “Edit / add variables” button to the right of the Environment variables section of the screen

  • You will see the values become editable in the UI after clicking this button

  • Next key in the value of a default Mage environment variable or enter a custom environment variable in the text boxes at the bottom of the page

  • Finally click the save changes button to save your new environment variables

💡 Note: When saving environment variables your cluster will restart. Save environment variables at the most opportune time for each cluster

Environment variables provide a robust foundation for managing configuration across different deployment environments while maintaining security best practices. By separating configuration from code, you create more maintainable pipelines that can easily adapt to different environments without code changes.