<@U06RKAMK0UC> why can't I access the secrets thro...
# ask-ai
s
@ancient-wolf-19325 why can't I access the secrets through env vars but only through context? I'm using Flyte V1
Copy code
@fl.task(
    container_image=basic_image_spec,
    resources=fl.Resources(cpu="1", mem="1Gi", ephemeral_storage="1Gi"),
    secret_requests=[
        fl.Secret(group="test", key="RAMA_WAS_HERE"),
        fl.Secret(group="test", key="RAMA_WAS_HERE_2"),
    ],
)
def test_secrets_task():
    context = fl.current_context()
    secret_val = context.secrets.get("test", "RAMA_WAS_HERE")
    print(secret_val)

    secret_val2 = context.secrets.get("test", "RAMA_WAS_HERE_2")
    print(secret_val2)

    test = os.getenv("RAMA_WAS_HERE")
    if test is None:
        raise ValueError("secret1 is not set")
    test2 = os.getenv("RAMA_WAS_HERE_2")
    if test2 is None:
        raise ValueError("secret2 is not set")
    return {"test": test, "test2": test2}


@fl.workflow()
def test_secrets():
    test_secrets_task()
a
In Flyte v1, secrets injected into tasks are not directly accessible via environment variables with the key name (e.g., "RAMA_WAS_HERE"). Instead, Flyte uses a specific naming convention for environment variables, typically prefixed with
_FSEC_
and including the group and key (e.g.,
_FSEC_TEST_RAMA_WAS_HERE
). The recommended and portable way to access secrets in Flyte tasks is through the context object: `context.secrets.get("test", "RAMA_WAS_HERE")`β€”this abstracts away the underlying environment variable or file mounting details and ensures compatibility across secret managers and Flyte versions. Directly using
os.getenv("RAMA_WAS_HERE")
will not work unless you manually set tSorry, there was a temporary error when processing your request. Please try again. If this problem persists, please contact support. πŸ‘ 0 button πŸ‘Ž 0 button πŸ’‘ Hint: Mention @runllm in the thread for followups.
s
@ancient-wolf-19325 try again
a
In Flyte v1, secrets requested via the
secret_requests
parameter are not automatically injected as environment variables with the key name you specify (e.g.,
RAMA_WAS_HERE
). Instead, Flyte injects secrets using internal environment variable names (like
_FSEC_<GROUP>_<KEY>
) or mounts them as files, and the recommended way to access them is through
flytekit.current_context().secrets.get(group, key)
. Direct access via
os.getenv("RAMA_WAS_HERE")
will not work unless you explicitly configure the secret to be mounted as an environment variable with that exact name, which is not the default behavior in Flyte v1. This is confirmed in this issue and the documentation. If you want to control the environment variable name, you must use the
env_var
parameter in the
Secret
object (supported in newer Flyte versions, e.g., v1.15+), or read the secret using the context manager as shown in your code. For Flyte v1, the context-based access is the supported and portable method. Would you like more detail or examples on how to set the
env_var
parameter or upgrade to a version that supports it? Sources: - Flyte issue: control ENV var name for injected secrets - Flyte documentation: Using secrets in tasks πŸ‘ 0 button πŸ‘Ž 0 button πŸ’‘ Hint: Mention @runllm in the thread for followups.