An environment variable is a value that affects the way the code runs and is dependent on the environment on which it is running.For example, you have written a ML model API service that,
Downloads the model from somewhere.
Loads the model from disk.
Serves a /infer route, which calls the model’s inference function.
Now, your service code may not change if you re-train and update the model. In this case, we can pass the model path via an environment variable.main.py
Copy
Ask AI
import osfrom fastapi import FastAPIimport mlfoundry as mlfclient = mlf.get_client()# NOTE: `MODEL_FQN` variable will now contain the value# of environment variable `MODEL_FQN`MODEL_FQN = os.getenv("MODEL_FQN")MODEL = client.get_model(MODEL_FQN).load()S3_BUCKET = os.getenv("S3_BUCKET_NAME")app = FastAPI()@app.get("/infer")def infer(...): ...
You can then run this service and inject the environment variable value like below,
Copy
Ask AI
MODEL_FQN="YOUR MODEL FQN" S3_BUCKET_NAME="my-s3-bucket" uvicorn main:app --port 8000 --host 0.0.0.0
You can also use a .env file on your local dev environment and use python-dotenv..env
Copy
Ask AI
MODEL_FQN="YOUR MODEL FQN FOR LOCAL RUN"S3_BUCKET_NAME="S3 bucket for local development"
How to inject environment variables in TrueFoundry
In this guide we will learn how can we inject environment variables in our deployments in TrueFoundry.
Copy
Ask AI
"""Both `Service ` and `Job` classes have an argument `env` where you can pass a dictionary. The dictionary keys will be assumed as environment variable names and the values will be the environment variable values."""import loggingfrom servicefoundry import Build, Service, DockerFileBuildlogging.basicConfig(level=logging.INFO)service = Service( name="my-service", image=Build(build_spec=DockerFileBuild()), ports=[{"port": 8501}], env={ "MODEL_FQN": "YOUR MODEL FQN", "S3_BUCKET_NAME": "my-s3-bucket" },)service.deploy(workspace_fqn="YOUR_WORKSPACE_FQN")
The variables MODEL_FQN and S3_BUCKET_NAME should be available in your environment on deployment.
Was this page helpful?
Assistant
Responses are generated using AI and may contain mistakes.