<@ULVA73B9P> I have a self hosted Prefect instance...
# ask-marvin
p
@Marvin I have a self hosted Prefect instance and also I use the Cloud one. I have the same automation in both, which uses the following code:
Copy code
flow_run_problem_notification = {
        "type": "send-notification",
        "block_document_id": slack_webhook_block._block_document_id,
        "subject": ':rotating_light: Prefect Run Problem on PROD',
        "body": 'Flow run {{ flow.name }}/{{ flow_run.name }} observed in ' +
                'state `{{ flow_run.state.name }}` at ' +
                '{{ flow_run.state.timestamp }}.\n' +
                'Flow ID: {{ flow_run.flow_id }}\n' +
                'Flow run ID: {{ flow_run.id }}\n' +
                'Flow run URL: {{ flow_run|ui_url }}\n' +
                'Flow run Params: {{ flow_run.parameters }}\n' +
                'State message: {{ flow_run.state.message }}'
    }
While this works fine in the Cloud, it fails to resolve the Flow run URL, on the self hosted. How can I fix this?
m
thought for 45.2 seconds
Based on the research, I can help you fix this issue. The problem occurs because the self-hosted instance needs to know its own base URL to properly construct the UI links. Here are two solutions: 1. Configure Server URL (Recommended): You need to set the
PREFECT_UI_URL
environment variable in your self-hosted environment. This tells Prefect where your UI is hosted. For example:
Copy code
bash
   export PREFECT_UI_URL="<https://your-prefect-instance.company.com>"
2. Alternative: Manual URL Construction: If setting the environment variable isn't feasible, you can manually construct the URL in your notification template:
Copy code
python
   flow_run_problem_notification = {
       "type": "send-notification",
       "block_document_id": slack_webhook_block._block_document_id,
       "subject": ':rotating_light: Prefect Run Problem on PROD',
       "body": 'Flow run {{ flow.name }}/{{ flow_run.name }} observed in ' +
               'state `{{ flow_run.state.name }}` at ' +
               '{{ flow_run.state.timestamp }}.\n' +
               'Flow ID: {{ flow_run.flow_id }}\n' +
               'Flow run ID: {{ flow_run.id }}\n' +
               'Flow run URL: <https://your-prefect-instance.company.com/flow-runs/>{{ flow_run.id }}\n' +
               'Flow run Params: {{ flow_run.parameters }}\n' +
               'State message: {{ flow_run.state.message }}'
   }
The first approach (setting
PREFECT_UI_URL
) is recommended because: 1. It's more maintainable - you don't have to hardcode URLs in your code 2. It ensures consistency across all notifications and UI links 3. It works with the existing template variable
{{ flow_run|ui_url }}
Would you like me to provide more details about setting up the environment variable in your specific deployment environment?
p
@Marvin Where is the documentation regarding PREFECT_UI_URL variable, and how is different from PREFECT_UI_API_URL?
m
thought for 36.7 seconds
Let me clarify the differences between these environment variables: 1. PREFECT_UI_URL - This is the base URL where your Prefect UI is accessible to users - Used for generating links that users will click on (like in notifications) - Example:
<https://prefect.your-company.com>
- This is what users will see in their browser when accessing the Prefect UI 2. PREFECT_UI_API_URL - This is the URL that the Prefect UI uses to communicate with the Prefect API - Used by the UI frontend to make API calls to the backend - Particularly important when using reverse proxies or when the API is hosted at a different location than the UI - Example:
<https://prefect-api.your-company.com/api>
For your specific case with the notification URLs,
PREFECT_UI_URL
is the variable you want to set, as it determines how the
{{ flow_run|ui_url }}
template variable gets rendered. A typical setup might look like this:
Copy code
bash
# The URL where users access the UI
export PREFECT_UI_URL="<https://prefect.your-company.com>"

# The URL where the UI connects to the API (if different)
export PREFECT_UI_API_URL="<https://prefect-api.your-company.com/api>"
You can find the official documentation for these settings in the Prefect Settings Reference. However, I notice that the documentation could be more detailed about these specific variables. I would recommend also checking the Security Settings page for additional context about configuring URLs in a self-hosted environment. Would you like me to provide more specific examples of how to set these up in different deployment environments (Docker, Kubernetes, etc.)?
p
@Marvin how should I set this new enviroment variable in the HELM deployment?
m
thought for 67.6 seconds
There are two ways to set the
PREFECT_UI_URL
environment variable in your Helm deployment: 1. Using values.yaml (Recommended): Modify your
values.yaml
file to include the environment variable:
Copy code
yaml
   server:
     env:
       - name: PREFECT_UI_URL
         value: "<https://your-prefect-instance.company.com>"

   ui:
     env:
       - name: PREFECT_UI_URL
         value: "<https://your-prefect-instance.company.com>"
2. Using Helm CLI: If you prefer not to modify the values file, you can set it during the helm install/upgrade:
Copy code
bash
   helm upgrade --install prefect prefect/prefect \
     --set server.env[0].name=PREFECT_UI_URL \
     --set server.env[0].value=<https://your-prefect-instance.company.com> \
     --set ui.env[0].name=PREFECT_UI_URL \
     --set ui.env[0].value=<https://your-prefect-instance.company.com>
Important notes: 1. Make sure to set this for both the
server
and
ui
components 2. The URL should be the externally accessible URL of your Prefect UI 3. If you're using an ingress controller, this should match your ingress host configuration 4. Don't forget the protocol (https:// or http://) Here's a more complete example of a
values.yaml
configuration that includes both the UI URL and ingress settings:
Copy code
yaml
server:
  env:
    - name: PREFECT_UI_URL
      value: "<https://your-prefect-instance.company.com>"

ui:
  env:
    - name: PREFECT_UI_URL
      value: "<https://your-prefect-instance.company.com>"

ingress:
  enabled: true
  hosts:
    - host: <http://your-prefect-instance.company.com|your-prefect-instance.company.com>
      paths:
        - path: /
          pathType: Prefix
  tls:
    - secretName: your-tls-secret
      hosts:
        - <http://your-prefect-instance.company.com|your-prefect-instance.company.com>
After making these changes, you'll need to upgrade your Helm deployment:
Copy code
bash
helm upgrade prefect prefect/prefect -f values.yaml
Would you like me to provide more specific details about any part of this configuration, such as ingress setup or TLS configuration?