Mitch
06/09/2023, 7:55 PMJake Kaplan
06/09/2023, 8:12 PMJake Kaplan
06/09/2023, 8:14 PM$ cat ~/.prefect/profiles.toml                                                                                                                                                                      
active = "workspace1"
[profiles.workspace1]
PREFECT_API_KEY = "<api key for workspace1>"
PREFECT_API_URL = "<workspace1 url>"
[profiles.workspace2]
PREFECT_API_KEY = "<api key for workspace2>"
PREFECT_API_URL = "<workspace2 url>"Mitch
06/12/2023, 1:22 PMMitch
06/12/2023, 5:03 PM/usr/local/bin/prefect
Currently I'm logging into the workspace at the end of the docker file using RUN /bin/sh -c "prefect cloud login --key <creds> --<workspace>"
I think I can pass the args into the dockerfile and use them as such
RUN prefect profile create 'dev'
RUN prefect --profile "dev" config set PREFECT_API_KEY==PREFECT_API_KEY_DEV PREFECT_API_URL=PREFECT_API_URL_DEV
RUN prefect profile create 'prod'
RUN prefect --profile "prod" config set PREFECT_API_KEY==PREFECT_API_KEY_PROD PREFECT_API_URL=PREFECT_API_URL_PROD
Then default the container to use dev
RUN prefect use dev
I would then be able to run in my deploy section if I wanted to use the other workspace
def deploy():
    import os
    os.system('prefect use prod')
    deployment = Deployment.build_from_flow(...)
Using this syntax, I would not need to run the prefect cloud login command at the end of my dockerfile?
RUN /bin/sh -c "prefect cloud login --key <creds> --<workspace>"
What is the difference between using a profile with the api key and URL (prefect use) vs using prefect cloud login?Mitch
06/12/2023, 5:22 PM./usr/local/lib/python3.10/site-packages/prefect/profiles.tomlJake Kaplan
06/12/2023, 6:52 PMprefect cloud login ... is effectively a nice interactive wrapper around setting PREFECT_API_KEY and PREFECT_API_URLMitch
06/12/2023, 8:36 PMos.system('prefect profile use prod'). When switching, everything is showing up as prod, however when running the deployment, it is only deploying to dev.
prefect profile inspect and prefect cloud workspace ls  shows only the prod workspace and is active.
I think it may have something to do with having two separate service roles, one for each workspace.
Again, everything in the configuration is showing I am in the proper workspace, but when running any prefect commands it is somehow using the other workspace/profile.Jake Kaplan
06/12/2023, 8:38 PMos.system('prefect profile use prod') once you're already inside a prefect entrypoint? a prefect cli command or flow run basically?Jake Kaplan
06/12/2023, 8:39 PMMitch
06/12/2023, 8:47 PMprefect use profile dev . When I would like to run in production, I add os.system('prefect profile use prod') which successfully shows me getting into the production workspace and proper profile, however when I run a command it still uploads to the other workspace. Could you elaborate on the entrypoint? I do not see an entrypoints in the .prefect folder in the container, however both of my profiles look good in the profiles.tomlMitch
06/12/2023, 8:49 PMJake Kaplan
06/12/2023, 9:04 PMJake Kaplan
06/12/2023, 9:04 PMfrom prefect.context import use_profile
from prefect import flow
from prefect.settings import load_current_profile
@flow
def my_flow():
    print(load_current_profile())
if __name__ == '__main__':
    import os
    profile = os.environ.get("PROFILE")
    with use_profile(profile):
        my_flow()Mitch
06/12/2023, 9:04 PMJake Kaplan
06/12/2023, 9:04 PM# profiles.toml
active_profile = "dev_profile"
[profiles.dev_profile]
PREFECT_API_KEY = "dev_api_key"
[profiles.prod_profile]
PREFECT_API_KEY = "prod_api_key"Jake Kaplan
06/12/2023, 9:04 PM(py311) ➜  prefect git:(disable-events) ✗ export PROFILE=prod_profile
(py311) ➜  prefect git:(disable-events) ✗ python my_flow.py          
17:03:19.353 | INFO    | prefect.engine - Created flow run 'ultramarine-jackal' for flow 'my-flow'
name='prod_profile' settings={<PREFECT_API_KEY: str>: 'prod_api_key'} source=PosixPath('/Users/jakekaplan/.prefect/profiles.toml')
17:03:19.533 | INFO    | Flow run 'ultramarine-jackal' - Finished in state Completed()
(py311) ➜  prefect git:(disable-events) ✗ export PROFILE=dev_profile
(py311) ➜  prefect git:(disable-events) ✗ python my_flow.py         
17:03:35.346 | INFO    | prefect.engine - Created flow run 'stereotyped-agama' for flow 'my-flow'
name='dev_profile' settings={<PREFECT_API_KEY: str>: 'dev_api_key'} source=PosixPath('/Users/jakekaplan/.prefect/profiles.toml')
17:03:35.633 | INFO    | Flow run 'stereotyped-agama' - Finished in state Completed()Mitch
06/12/2023, 9:14 PMprefect profile use?Jake Kaplan
06/12/2023, 9:16 PMMitch
06/12/2023, 9:25 PMJake Kaplan
06/12/2023, 9:25 PMMitch
06/12/2023, 9:26 PMJake Kaplan
06/12/2023, 9:36 PM--profile in the CLI (doing something for 1 flow run)
that being said I think, you should be able to do that within/around a single task, but I would recommend testing to make sure it's working as expectedMitch
06/12/2023, 9:37 PMMitch
06/12/2023, 9:46 PMJake Kaplan
06/12/2023, 9:48 PMMitch
06/12/2023, 9:50 PMJake Kaplan
06/12/2023, 9:50 PMMitch
06/13/2023, 4:15 PM#using profile 'z'
with use_profile('x'):
    y = String.load('y') #Still prints string from 'z'
Would it be bad practice to have a service role with access to both workspaces?Jake Kaplan
06/13/2023, 5:18 PM