modal.secret

modal.secret.Secret

class Secret(modal.object.Object)

Secrets provide a dictionary of environment variables for images.

Secrets are a secure way to add credentials and other sensitive information to the containers your functions run in. You can create and edit secrets on the dashboard, or programmatically from Python code.

See the secrets guide page for more information.

def __init__(self, *args, **kwargs):

from_dict

@typechecked
@staticmethod
def from_dict(
    env_dict: Dict[
        str, Union[str, None]
    ] = {},  # dict of entries to be inserted as environment variables in functions using the secret
):

Create a secret from a str-str dictionary. Values can also be None, which is ignored.

Usage:

@stub.function(secrets=[modal.Secret.from_dict({"FOO": "bar"})])
def run():
    print(os.environ["FOO"])

from_local_environ

@typechecked
@staticmethod
def from_local_environ(
    env_keys: List[str],  # list of local env vars to be included for remote execution
):

Create secrets from local environment variables automatically.

from_dotenv

@staticmethod
def from_dotenv(path=None):

Create secrets from a .env file automatically.

If no argument is provided, it will use the current working directory as the starting point for finding a .env file. Note that it does not use the location of the module calling Secret.from_dotenv.

If called with an argument, it will use that as a starting point for finding .env files. In particular, you can call it like this:

@stub.function(secrets=[modal.Secret.from_dotenv(__file__)])
def run():
    print(os.environ["USERNAME"])  # Assumes USERNAME is defined in your .env file

This will use the location of the script calling modal.Secret.from_dotenv as a starting point for finding the .env file.

from_name

@staticmethod
def from_name(
    label: str,
    namespace=api_pb2.DEPLOYMENT_NAMESPACE_WORKSPACE,
    environment_name: Optional[str] = None,
) -> "_Secret":

Create a reference to a persisted Secret

secret = modal.Secret.from_name("my-secret")

@stub.function(secrets=[secret])
def run():
   ...

lookup

@staticmethod
def lookup(
    label: str,
    namespace=api_pb2.DEPLOYMENT_NAMESPACE_WORKSPACE,
    client: Optional[_Client] = None,
    environment_name: Optional[str] = None,
) -> "_Secret":

Lookup a secret with a given name

s = modal.Secret.lookup("my-secret")
print(s.object_id)