DocsWith Env Vars
Passing ParamsWith Env Vars

Passing parameters as environment variables requires that you set the proper handling of environment variables in your programs.

Overview

Oftentimes you will need to expose an input or a context meta data as an environment variable, in some other situations you might want to skip creating a CLI to run a program and handle the logic based on the environment variables exposed in the system.

Polyaxon exposes the full Kubernetes container specification, which means that you can leverage the env sub-section to expose environment variables:

Example

Let's look at a simple program that just prints some information based on an environment variable:

import os

print(os.environ.get("MESSAGE"))

In order to run this program, we can use the following Polyaxonfile passing-params/env-vars.yaml:

version: 1.1
kind: component
name: environment-variable-parameter
inputs:
  - name: message
    type: str
run:
  kind: job
  init:
    - file:
        content: |
          import os

          print(os.environ.get("MESSAGE"))
        filename: message.py
  container:
    image: python:3.11
    workingDir: "{{ globals.artifacts_path }}"
    command: [python3, message.py]
    env:
      - name: MESSAGE
        value: "{{ message }}"

Now you can run multiple version of this example:

polyaxon run -f passing-params/env-vars.yaml -P message="test 1" -l
polyaxon run -f passing-params/env-vars.yaml -P message="test 2" -l

Exposing inputs/outputs to env var

N.B: Requires CLI >= v1.12.

To improve the previous process, Polyaxon IO/Params specification has an option to expose the value to an environment variable automatically. Instead of manually using the configuration:

    env:
      - name: MESSAGE
        value: "{{ message }}"

It's possible to use the specification toEnv to tell Polyaxon to expose the variable:

inputs:
  - name: message
    type: str
    toEnv: MESSAGE

toEnv is also available on the params specification, in case the component does not define if the IO should expose the value to an env var, or in case a param is used as context only:

params:
  message:
    value: "New message"
    toEnv: ENV_VAR_MESSAGE  # <----