<@ULVA73B9P> How can I trigger a flow run for a de...
# ask-marvin
n
@Marvin How can I trigger a flow run for a deployment in a different workspace in Prefect2? I'm trying to use the
run_deployment
function but I receive an error specifying that the deployment doesn't exist as its looking in the wrong workspace
Copy code
from prefect.deployments import run_deployment 

run_deployment(name="a1234", timeout=0, as_subflow=False)
This raises the following error:
Copy code
prefect.exceptions.PrefectHTTPStatusError: Client error '404 Not Found' for url '<https://api.prefect.cloud/api/accounts/><my-account>/workspaces/<current-workspace-not-the-target-workspace>/deployments/a1234'
Response: {'detail': 'Deployment not found'}
For more information check: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404>
1
m
To trigger a flow run for a deployment in a different workspace using Prefect 2.x, you need to ensure that the
run_deployment
function is aware of the correct workspace context. The error you encountered indicates that it's looking for the deployment in the current workspace instead of the target one. Here's how you can specify the correct workspace: 1. Set the Workspace Context: You need to configure your Prefect client to use the desired workspace. This can be done by setting the appropriate environment variables or configuration before calling
run_deployment
. 2. Use the Prefect Client: You might need to interact with the Prefect client directly to switch workspaces. Unfortunately, the documentation excerpts didn't provide a direct method, but typically you would: - Set the
PREFECT_API_URL
to point to the correct workspace. - Use the Prefect client to authenticate and switch workspaces. 3. Double-check Deployment Name: Ensure that the deployment name is correct and exists in the target workspace. For further details, you might want to consult the Prefect documentation on deployment management and CI/CD practices. If you continue to face issues, consider reviewing your workspace configuration and ensuring that your environment is set up to access the correct workspace.
n
@Marvin can you provide an example of how I can achieve this by updating my provided code:
Copy code
from prefect.deployments import run_deployment 

run_deployment(name="a1234", timeout=0, as_subflow=False)
m
To trigger a flow run for a deployment in a different workspace using Prefect 2.x, you need to set the workspace context correctly. Unfortunately, the documentation excerpts didn't provide a direct example, but you can typically achieve this by configuring the Prefect client to point to the correct workspace before calling
run_deployment
. Here's a conceptual example of how you might adjust your code:
Copy code
python
from prefect import get_client
from prefect.deployments import run_deployment

# Configure the Prefect client to use the target workspace
client = get_client()
client.api_url = "<https://api.prefect.cloud/api/accounts/<your-account-id>/workspaces/<target-workspace-id>>"

# Now run the deployment in the specified workspace
run_deployment(name="a1234", timeout=0, as_subflow=False)
### Steps to Follow: 1. Get the Account and Workspace IDs: You'll need to know the account ID and the workspace ID for the target workspace. These can be found in your Prefect Cloud settings. 2. Set the API URL: Update the
client.api_url
with the correct account and workspace IDs. 3. Run the Deployment: Call
run_deployment
after setting the client configuration to ensure it triggers in the correct workspace. For more specific instructions, it might be helpful to consult the Prefect documentation on workspaces and connecting to Prefect Cloud. If you have access to Prefect's support or community forums, they might provide additional guidance tailored to your setup.
n
@Marvin client.api_url is specified as read only
m
To run a deployment in a different workspace using Prefect 2.x, you need to set the active workspace context via the Prefect CLI. The API URL is indeed read-only, but you can switch workspaces using the CLI. Here's how you can do it: 1. List Available Workspaces: Use the CLI to list available workspaces and find the one you want to switch to.
Copy code
bash
   prefect cloud workspace ls
2. Set Active Workspace: Use the CLI to set the desired workspace as the active one.
Copy code
bash
   prefect cloud workspace set --workspace <workspace-name>
3. Run the Deployment: Once the workspace is set, you can run your deployment as intended.
Copy code
python
   from prefect.deployments import run_deployment

   run_deployment(name="a1234", timeout=0, as_subflow=False)
By setting the workspace using the Prefect CLI, you ensure that the
run_deployment
function operates within the correct workspace context. For more information, you can refer to the Prefect Cloud connection documentation.
n
marvin's on the wrong track here
@Marvin look up temporary_settings ctx manager
m
It seems that the
temporary_settings
context manager wasn't covered in the Prefect 2.x documentation excerpts I found. However, if you want to temporarily change the context or settings, you might consider using environment variables or directly interacting with the Prefect client configuration, although these methods weren't explicitly detailed in the documentation. For now, I recommend using the Prefect CLI to set the workspace as previously described. If you have any specific context manager in mind or additional documentation, please let me know, and I can assist further. Alternatively, you could explore Prefect's GitHub repository or community forums for more insights on this feature.
n
doh
Copy code
from prefect.settings import PREFECT_API_URL, temporary_settings

with temporary_settings({PREFECT_API_URL: <new value>}):
  # call run_deployment
prefect spin 1
🙇 1
n
Thats very neat thank you 🙇
catjam 1
@Nate strangely thats still throwing a 404 error despite it referencing the correct url 🤔 I have tried it with both the deployment id and deployment name e.g:
Copy code
prefect.exceptions.PrefectHTTPStatusError: Client error '404 Not Found' for url '<https://api.prefect.cloud/api/accounts/123/workspaces/abc4/deployments/name/my_flow_name/my_deployment_name>'
or
Copy code
prefect.exceptions.PrefectHTTPStatusError: Client error '404 Not Found' for url '<https://api.prefect.cloud/api/accounts/123/workspaces/abc4/deployments/def5>'
This matches the url in the Prefect 2 UI:
Copy code
<https://app.prefect.cloud/account/123/workspace/abc4/deployments/deployment/def5>
I have tested it with a deployment in the same workspace without any issues
n
do you have a separate api key for the other workspace?
n
Oh good point. Is that under service accounts? I don't think we have specified service accounts per workspaces but our service accounts run as members and not owner or admins
Thanks I got it to work now with workpace sharing 🙌
catjam 1