4  Secrets

03_secrets

Author

Ryan Wesslen

Let’s explore secrets in Modal.

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.

To inject Secrets into the container running your function, you add the secrets=[…] argument to your app.function annotation. For deployed Secrets (typically created via the Modal dashboard) you can refer to those using Secret.from_name(secret_name).

For example, if you have a Secret called secret-keys containing the key MY_PASSWORD:

import os
import modal

app = modal.App()


@app.function(secrets=[modal.Secret.from_name("secret-keys")])
def some_function():
    secret_key = os.environ["MY_PASSWORD"]
    ...

Each Secret can contain multiple keys and values but you can also inject multiple Secrets, allowing you to separate Secrets into smaller reusable units:

@app.function(secrets=[
    modal.Secret.from_name("my-secret-name"),
    modal.Secret.from_name("other-secret"),
])
def other_function():
    ...

The Secrets are applied in order, so key-values from later modal.Secret objects in the list will overwrite earlier key-values in the case of a clash. For example, if both modal.Secret objects above contained the key FOO, then the value from “other-secret” would always be present in os.environ["FOO"].

5 04_secrets.py

TBD