Project: Prefect (Orchestration & Management / Scheduling & Orchestration)
File: deployments.md

description: Learn how Prefect flow deployments enable configuring flows for scheduled and remote execution. tags: orchestration flow runs deployments schedules triggers tutorial search: boost: 2 Learn how to use deployments to configure flows for scheduled and remote execution. !!! note "Reminder to connect to Prefect Cloud or a self-hosted Prefect server instance" Some features in this tutorial, such as scheduling, require you to connect to a Prefect server. If using a self-hosted setup, run `prefect server start` to run both the webserver and UI. If using Prefect Cloud, make sure you have . are server-side representations of flows. They store the crucial metadata needed for remote orchestration including when, where, and how a workflow should run. Attributes of a deployment include: Flow entrypoint: path to your flow function Schedule or Trigger: optional schedule or triggering rules for this deployment Tags: optional text labels for organizing your deployments Some of the most common reasons to use an orchestration tool like Prefect are for and . As opposed to manually triggering and managing flow runs, deploying a flow exposes an API and UI that allow you to: trigger new runs, , pause scheduled runs, customize parameters, and more remotely configure schedules and automation rules for your deployments dynamically provision infrastructure using Continuing with the `getrepoinfo` flow from the previous section, create a deployment by calling a single method on the flow object: `flow.serve`. ```python hllines="16-17" title="repoinfo.py" import httpx from prefect import flow @flow(log_prints=True) def getrepoinfo(repo_name: str = "PrefectHQ/prefect"): url = f"https://api.github.com/repos/{repo_name}" response = httpx.get(url) response.raiseforstatus() repo = response.json() print(f"{repo_name} repository statistics :") print(f"Stars : {repo['stargazers_count']}") print(f"Forks : {repo['forks_count']}") if name == "main": getrepoinfo.serve(name="my-first-deployment") ``` Running this script will: create a deployment called ```my-first-deployment``` for your flow in the Prefect API continue running to listen for flow runs for this deployment; when a run is found, it will asynchronously execute within a subprocess !!! warning "You must define deployments in static files" You can define and run flows interactively, within REPLs or Notebooks. However, deployments require that your flow definition exist in a known file. Because this deployment has no schedule or triggering automation, you must use the UI or API to create runs for it. Use the CLI (in a separate terminal window) to create a run for this deployment: <div class="terminal"> ```bash prefect deployment run 'get-repo-info/my-first-deployment' ``` </div> In your terminal or UI, you should see the newly created run execute successfully. Next, add a schedule and additional metadata. The `serve` method on flows exposes many options for the

Here's how to use some of those options: `cron`: a keyword that allows you to set a cron string schedule for the deployment; see for more advanced scheduling options `tags`: a keyword that allows you to tag this deployment and its runs for bookkeeping and filtering purposes `description`: a keyword that allows you to document what this deployment does; by default the description is set from the docstring of the flow function (if documented) `version`: a keyword that allows you to track changes to your deployment; uses a hash of the file containing the flow by default; popular options include semver tags or git commit hashes Next, add these options to your deployment: ```python if name == "main": getrepoinfo.serve( name="my-first-deployment", cron=" *", tags=["testing", "tutorial"], description="Given a GitHub repository, logs repository statistics for that repo.", version="tutorial/deployments", ) ``` When you rerun this script, you will find an updated deployment in the UI that is actively scheduling work. Stop the script in the CLI using `CTRL+C` and your schedule automatically pauses. !!! note "`.serve` is a long-running process" To execute remotely triggered or scheduled runs, your script with `flow.serve` must be actively running. This method is useful for creating deployments for single flows. For two or more flows, you must provide a few additional method calls and imports: ```python hllines="2 18-20" title="multiflow_deployment.py" import time from prefect import flow, serve @flow def slow_flow(sleep: int = 60): "Sleepy flow - sleeps the provided amount of time (in seconds)." time.sleep(sleep) @flow def fast_flow(): "Fastest flow this side of the Mississippi." return if name == "main": slowdeploy = slowflow.to_deployment(name="sleeper", interval=45) fastdeploy = fastflow.to_deployment(name="fast") serve(slowdeploy, fastdeploy) ``` A few things to note: the `flow.to_deployment` interface exposes the exact same options as `flow.serve`; this method produces a deployment object the deployments are only registered with the API once `serve(...)` is called when serving multiple deployments, the only requirement is that they share a Python environment; they can be executed and scheduled independently of each other A few optional steps for exploration include: pause and unpause the schedule for the "sleeper" deployment use the UI to submit ad-hoc runs for the "sleeper" deployment with different values for `sleep` cancel an active run for the "sleeper" deployment from the UI !!! tip "Hybrid execution option" Another implication of Prefect's deployment interface is that you can choose to use our hybrid execution model. Whether you use Prefect Cloud or host a Prefect server instance, you can run workflows in the environments best suited to their execution. This model enables efficient use of your infrastructure resources while maintaining the privacy of your code and data. There is no ingress required. For more information . Deploying flows through the `serve` method is a fast way to start scheduling flows with Prefect. However, if your team has more complex infrastructure requirements or you'd like to have Prefect manage flow execution, you can deploy flows to a work pool. Learn about work pools and how Prefect Cloud can handle infrastructure configuration for you in the .