Environment variables and Secrets

Similar to how one can set environment variables in a local environment, one can also pass environment variables when running Photons remotely. This can be done either via the CLI, or via the GUI.

Env Variables via CLI

While running a photon, you can specify the environment variables to be passed to the deployment using the --env flag. The value should be a key-value pair, in the format KEY=VALUE. Multiple environment variables can be passed by specifying the --env flag multiple times. For example, you can pass the environment variables FOO=BAR and HELLO=WORLD to a photon using the following command:

lep photon run -n my-photon --env FOO=BAR --env HELLO=WORLD

and in the photon, you can access the environment variables using the os module:

import os
# if you know FOO will always be set.
foo_value = os.environ['FOO']
# if you are not sure if HELLO will be set.
hello_value = os.environ.get('HELLO', 'default_value') 

Using Secrets

Secrets are similar to environment variables, but the actual value never leaves the cloud environment. Secrets can be used to store sensitive information like API keys and passwords, which one does not want to accidentally leak into display output. Secrets can be created and managed via the CLI using the lep secret command.

For example, to create a secret named MY_SECRET with the value MY_SECRET_VALUE, you can run:

lep secret create -n MY_SECRET -v MY_SECRET_VALUE

Secrets are stored and associated with the workspace in which they are created. To use a secret, you can use the --secret flag when running a photon. For example, to pass the secret MY_SECRET to a photon, you can run:

lep photon run -n my-photon --secret MY_SECRET

Inside the deployment, you can access the secret value in the same way it would access an environment variable. For more ways to manage the secrets, please refer to the CLI documentation on secret.

Predefined Environment Variables

When you are running a photon on the Lepton cloud, there are some environment variables that are predefined for you. These variables are:

  • LEPTON_WORKSPACE_ID: The ID of the workspace in which the photon is running.
  • LEPTON_DEPLOYMENT_NAME: The name of the deployment that is running.
  • LEPTON_PHOTON_NAME: The name of the photon that is running.
  • LEPTON_PHOTON_ID: The ID of the photon that is running.
  • LEPTON_RESOURCE_ACCELERATOR_TYPE: The accelerator type of the resource on which the photon is running.

You can access the value of these variables in the same way you would access any other environment variable. As a quick example, in your deployment, if you need to create a client to access another deployment named foo in the same workspace, you can use the following code:

from leptonai.client import Client
client = Client(os.environ("LEPTON_WORKSPACE_ID"), "foo")

Note that, for security reasons, we don't automatically include the workspace token as an environment variable. If your deployment needs it, You can manually pass the workspace token as a secret you create and access.