Learn about code storage as it relates to execution of deployments
When a deployment runs, the execution environment needs access to the flow code. Flow code is not stored directly in Prefect server or Prefect Cloud; instead, it must be made available to the execution environment. There are two main ways to achieve this:
This page focuses on the second approach: retrieving code from a storage location at runtime.
You have several options for where your code can be stored and pulled from:
The ideal choice depends on your team’s needs and tools.
In the examples below, we show how to create a deployment configured to run on dynamic infrastructure for each of these storage options.
As detailed in the Deployment overview, you can create a deployment in one of two main ways:
Python code with the flow.deploy
method
When using .deploy
, specify a storage location for your flow with the flow.from_source
method.
The source
is either a URL to a git repository or a storage object. For example:
source=Path(__file__).parent
or source="/path/to/file"
source="https://github.com/org/my-repo.git"
source=GitRepository(url="https://github.com/org/my-repo.git")
The entrypoint
is the path to the file the flow is located in and the function name, separated by a colon.
YAML specification defined in a prefect.yaml
file
To create a prefect.yaml
file interactively, run prefect deploy
from the CLI and follow the prompts.
The prefect.yaml
file may define a pull
section that specifies the storage location for your flow. For example:
Whether you use from_source
or prefect.yaml
to specify the storage location for your flow code, the resulting deployment will have a set of pull
steps that your worker will use to retrieve the flow code at runtime.
If using a Process work pool, you can use one of the remote code storage options shown above, or you can store your flow code in a local folder.
Here is an example of how to create a deployment with flow code stored locally:
Git-based version control platforms provide redundancy, version control, and collaboration capabilities. Prefect supports:
For a public repository, you can use the repository URL directly.
If you are using a private repository and are authenticated in your environment at deployment creation and deployment execution, you can use the repository URL directly.
Alternatively, for a private repository, you can create a Secret
block or git
-platform-specific credentials block to store your credentials:
Then you can reference this block in the Python deploy
method or the prefect.yaml
file pull step.
If using the Python deploy
method with a private repository that references a block, provide a GitRepository
object instead of a URL, as shown below.
For accessing a private repository, we suggest creating a Personal Access Tokens (PATs). We recommend using HTTPS with fine-grained Personal Access Tokens to limit access by repository.
Per least privilege, we recommend granting the token the ability to read Contents and for your repository.
If using a Secret
block, you can create it through code or the UI ahead of time and reference it at deployment creation as shown above.
If using a GitHubCredentials
block to store your credentials, you can create it ahead of time and reference it at deployment creation.
prefect-github
with pip install -U prefect-github
prefect-github
with prefect block register -m prefect_github
GitHubCredentials
block through code or the Prefect UI and reference it at deployment creation as shown above.For accessing a private repository, we suggest creating a Personal Access Tokens (PATs). We recommend using HTTPS with fine-grained Personal Access Tokens to limit access by repository.
Per least privilege, we recommend granting the token the ability to read Contents and for your repository.
If using a Secret
block, you can create it through code or the UI ahead of time and reference it at deployment creation as shown above.
If using a GitHubCredentials
block to store your credentials, you can create it ahead of time and reference it at deployment creation.
prefect-github
with pip install -U prefect-github
prefect-github
with prefect block register -m prefect_github
GitHubCredentials
block through code or the Prefect UI and reference it at deployment creation as shown above.For accessing a private repository, we recommend using HTTPS with Repository, Project, or Workspace Access Tokens.
Create a token with read access to the repository.
Bitbucket requires you prepend the token string with x-token-auth:
The full string looks like this: x-token-auth:abc_123_this_is_a_token
.
If using a Secret
block, you can create it through code or the UI ahead of time and reference it at deployment creation as shown above.
If using a BitBucketCredentials
block to store your credentials, you can create it ahead of time and reference it at deployment creation.
prefect-bitbucket
with pip install -U prefect-bitbucket
prefect-bitbucket
with prefect block register -m prefect_bitbucket
BitBucketCredentials
block in code or the Prefect UI and reference at deployment creation as shown above.For accessing a private repository, we recommend using HTTPS with Project Access Tokens.
Create a token with the read_repository scope.
If using a Secret
block, you can create it through code or the UI ahead of time and reference it at deployment creation as shown above.
If using a GitLabCredentials
block to store your credentials, you can create it ahead of time and reference it at deployment creation.
prefect-gitlab
with pip install -U prefect-gitlab
prefect-gitlab
with prefect block register -m prefect_gitlab
GitLabCredentials
block in code or the Prefect UI and reference it at deployment creation as shown above.Note that you can specify a branch
if creating a GitRepository
object.
The default is "main"
.
Push your code
When you make a change to your code, Prefect does not push your code to your git
-based version control platform.
This is intentional to avoid confusion about the git
history and push process.
If you’re using Prefect Cloud you can use the Prefect Cloud GitHub App to authenticate to GitHub at runtime and access private repositories without creating a block and storing long lived credentials.
Install the Prefect Cloud GitHub App and select the repositories you want to be able to access.
Add the following pull steps to your prefect.yaml
file. Make sure to replace owner/repository
with the name of your repository:
Another popular flow code storage option is to include it in a Docker image. All work pool options except Process and Prefect Managed allow you to bake your code into a Docker image.
To create a deployment with Docker-based flow code storage use the Python deploy
method or create a prefect.yaml
file.
If you use the Python deploy
method to store the flow code in a Docker image, you don’t need to use the from_source
method.
The prefect.yaml
file below was generated by running prefect deploy
from the CLI (a few lines of metadata were excluded from the top of the file output for brevity).
Note that the build
section is necessary if baking your flow code into a Docker image.
By default, .deploy
will build a Docker image that includes your flow code and any pip
packages specified in a requirements.txt
file.
In the examples above, we elected not to push the resulting image to a remote registry.
To push the image to a remote registry, pass push=True
in the Python deploy
method or add a push_docker_image
step to the push
section of the prefect.yaml
file.
If an image
is not specified by one of the methods above, deployment flow runs associated with a Docker work pool will use the base Prefect image (e.g. prefecthq/prefect:3-latest
) when executing.
Alternatively, you can create a custom Docker image outside of Prefect by running docker build
&& docker push
elsewhere (e.g. in your CI/CD pipeline) and then reference the resulting image
in the job_variables
section of your deployment definition, or set the image
as a default directly on the work pool.
For more information, see this discussion of custom Docker images.
Another option for flow code storage is any fsspec-supported storage location, such as AWS S3, Azure Blob Storage, or GCP GCS.
If the storage location is publicly available, or if you are authenticated in the environment where you are creating and running your deployment, you can reference the storage location directly. You don’t need to pass credentials explicitly.
To pass credentials explicitly to authenticate to your storage location, you can use either of the following block types:
prefect-aws
library’s S3Bucket
block, which can use a AWSCredentials
block when it is created.Secret
blocksIf you use a storage block such as the S3Bucket
block, you need to have the prefect-aws
library available in the environment where your flow code runs.
You can do any of the following to make the library available:
{"EXTRA_PIP_PACKAGES":"prefect-aws"}
deploy
method as shown in the examples belowrequirements.txt
file and reference the file in the pull
step of the prefect.yaml
file like this:The examples below show how to create a deployment with flow code in a cloud provider storage location.
For each example, we show how to access code that is publicly available.
The prefect.yaml
example includes an additional line to reference a credentials block if authenticating to a private storage location through that option.
We also include Python code that shows how to use an existing storage block and an example of that creates, but doesn’t save, a storage block that references an existing nested credentials block.
To create an AwsCredentials
block:
pip install -U prefect-aws
prefect block register -m prefect_aws
AWSCredentials
block in code or the Prefect UI. In addition to the block name, most users will fill in the AWS Access Key ID and AWS Access Key Secret fields.To create an AwsCredentials
block:
pip install -U prefect-aws
prefect block register -m prefect_aws
AWSCredentials
block in code or the Prefect UI. In addition to the block name, most users will fill in the AWS Access Key ID and AWS Access Key Secret fields.To create an AzureBlobCredentials
block:
pip install -U prefect-azure
prefect block register -m prefect_azure
To create a GcpCredentials
block:
pip install -U prefect-gcp
prefect block register -m prefect_gcp
Another authentication option is to give the worker access to the storage location at runtime through SSH keys.
Learn about code storage as it relates to execution of deployments
When a deployment runs, the execution environment needs access to the flow code. Flow code is not stored directly in Prefect server or Prefect Cloud; instead, it must be made available to the execution environment. There are two main ways to achieve this:
This page focuses on the second approach: retrieving code from a storage location at runtime.
You have several options for where your code can be stored and pulled from:
The ideal choice depends on your team’s needs and tools.
In the examples below, we show how to create a deployment configured to run on dynamic infrastructure for each of these storage options.
As detailed in the Deployment overview, you can create a deployment in one of two main ways:
Python code with the flow.deploy
method
When using .deploy
, specify a storage location for your flow with the flow.from_source
method.
The source
is either a URL to a git repository or a storage object. For example:
source=Path(__file__).parent
or source="/path/to/file"
source="https://github.com/org/my-repo.git"
source=GitRepository(url="https://github.com/org/my-repo.git")
The entrypoint
is the path to the file the flow is located in and the function name, separated by a colon.
YAML specification defined in a prefect.yaml
file
To create a prefect.yaml
file interactively, run prefect deploy
from the CLI and follow the prompts.
The prefect.yaml
file may define a pull
section that specifies the storage location for your flow. For example:
Whether you use from_source
or prefect.yaml
to specify the storage location for your flow code, the resulting deployment will have a set of pull
steps that your worker will use to retrieve the flow code at runtime.
If using a Process work pool, you can use one of the remote code storage options shown above, or you can store your flow code in a local folder.
Here is an example of how to create a deployment with flow code stored locally:
Git-based version control platforms provide redundancy, version control, and collaboration capabilities. Prefect supports:
For a public repository, you can use the repository URL directly.
If you are using a private repository and are authenticated in your environment at deployment creation and deployment execution, you can use the repository URL directly.
Alternatively, for a private repository, you can create a Secret
block or git
-platform-specific credentials block to store your credentials:
Then you can reference this block in the Python deploy
method or the prefect.yaml
file pull step.
If using the Python deploy
method with a private repository that references a block, provide a GitRepository
object instead of a URL, as shown below.
For accessing a private repository, we suggest creating a Personal Access Tokens (PATs). We recommend using HTTPS with fine-grained Personal Access Tokens to limit access by repository.
Per least privilege, we recommend granting the token the ability to read Contents and for your repository.
If using a Secret
block, you can create it through code or the UI ahead of time and reference it at deployment creation as shown above.
If using a GitHubCredentials
block to store your credentials, you can create it ahead of time and reference it at deployment creation.
prefect-github
with pip install -U prefect-github
prefect-github
with prefect block register -m prefect_github
GitHubCredentials
block through code or the Prefect UI and reference it at deployment creation as shown above.For accessing a private repository, we suggest creating a Personal Access Tokens (PATs). We recommend using HTTPS with fine-grained Personal Access Tokens to limit access by repository.
Per least privilege, we recommend granting the token the ability to read Contents and for your repository.
If using a Secret
block, you can create it through code or the UI ahead of time and reference it at deployment creation as shown above.
If using a GitHubCredentials
block to store your credentials, you can create it ahead of time and reference it at deployment creation.
prefect-github
with pip install -U prefect-github
prefect-github
with prefect block register -m prefect_github
GitHubCredentials
block through code or the Prefect UI and reference it at deployment creation as shown above.For accessing a private repository, we recommend using HTTPS with Repository, Project, or Workspace Access Tokens.
Create a token with read access to the repository.
Bitbucket requires you prepend the token string with x-token-auth:
The full string looks like this: x-token-auth:abc_123_this_is_a_token
.
If using a Secret
block, you can create it through code or the UI ahead of time and reference it at deployment creation as shown above.
If using a BitBucketCredentials
block to store your credentials, you can create it ahead of time and reference it at deployment creation.
prefect-bitbucket
with pip install -U prefect-bitbucket
prefect-bitbucket
with prefect block register -m prefect_bitbucket
BitBucketCredentials
block in code or the Prefect UI and reference at deployment creation as shown above.For accessing a private repository, we recommend using HTTPS with Project Access Tokens.
Create a token with the read_repository scope.
If using a Secret
block, you can create it through code or the UI ahead of time and reference it at deployment creation as shown above.
If using a GitLabCredentials
block to store your credentials, you can create it ahead of time and reference it at deployment creation.
prefect-gitlab
with pip install -U prefect-gitlab
prefect-gitlab
with prefect block register -m prefect_gitlab
GitLabCredentials
block in code or the Prefect UI and reference it at deployment creation as shown above.Note that you can specify a branch
if creating a GitRepository
object.
The default is "main"
.
Push your code
When you make a change to your code, Prefect does not push your code to your git
-based version control platform.
This is intentional to avoid confusion about the git
history and push process.
If you’re using Prefect Cloud you can use the Prefect Cloud GitHub App to authenticate to GitHub at runtime and access private repositories without creating a block and storing long lived credentials.
Install the Prefect Cloud GitHub App and select the repositories you want to be able to access.
Add the following pull steps to your prefect.yaml
file. Make sure to replace owner/repository
with the name of your repository:
Another popular flow code storage option is to include it in a Docker image. All work pool options except Process and Prefect Managed allow you to bake your code into a Docker image.
To create a deployment with Docker-based flow code storage use the Python deploy
method or create a prefect.yaml
file.
If you use the Python deploy
method to store the flow code in a Docker image, you don’t need to use the from_source
method.
The prefect.yaml
file below was generated by running prefect deploy
from the CLI (a few lines of metadata were excluded from the top of the file output for brevity).
Note that the build
section is necessary if baking your flow code into a Docker image.
By default, .deploy
will build a Docker image that includes your flow code and any pip
packages specified in a requirements.txt
file.
In the examples above, we elected not to push the resulting image to a remote registry.
To push the image to a remote registry, pass push=True
in the Python deploy
method or add a push_docker_image
step to the push
section of the prefect.yaml
file.
If an image
is not specified by one of the methods above, deployment flow runs associated with a Docker work pool will use the base Prefect image (e.g. prefecthq/prefect:3-latest
) when executing.
Alternatively, you can create a custom Docker image outside of Prefect by running docker build
&& docker push
elsewhere (e.g. in your CI/CD pipeline) and then reference the resulting image
in the job_variables
section of your deployment definition, or set the image
as a default directly on the work pool.
For more information, see this discussion of custom Docker images.
Another option for flow code storage is any fsspec-supported storage location, such as AWS S3, Azure Blob Storage, or GCP GCS.
If the storage location is publicly available, or if you are authenticated in the environment where you are creating and running your deployment, you can reference the storage location directly. You don’t need to pass credentials explicitly.
To pass credentials explicitly to authenticate to your storage location, you can use either of the following block types:
prefect-aws
library’s S3Bucket
block, which can use a AWSCredentials
block when it is created.Secret
blocksIf you use a storage block such as the S3Bucket
block, you need to have the prefect-aws
library available in the environment where your flow code runs.
You can do any of the following to make the library available:
{"EXTRA_PIP_PACKAGES":"prefect-aws"}
deploy
method as shown in the examples belowrequirements.txt
file and reference the file in the pull
step of the prefect.yaml
file like this:The examples below show how to create a deployment with flow code in a cloud provider storage location.
For each example, we show how to access code that is publicly available.
The prefect.yaml
example includes an additional line to reference a credentials block if authenticating to a private storage location through that option.
We also include Python code that shows how to use an existing storage block and an example of that creates, but doesn’t save, a storage block that references an existing nested credentials block.
To create an AwsCredentials
block:
pip install -U prefect-aws
prefect block register -m prefect_aws
AWSCredentials
block in code or the Prefect UI. In addition to the block name, most users will fill in the AWS Access Key ID and AWS Access Key Secret fields.To create an AwsCredentials
block:
pip install -U prefect-aws
prefect block register -m prefect_aws
AWSCredentials
block in code or the Prefect UI. In addition to the block name, most users will fill in the AWS Access Key ID and AWS Access Key Secret fields.To create an AzureBlobCredentials
block:
pip install -U prefect-azure
prefect block register -m prefect_azure
To create a GcpCredentials
block:
pip install -U prefect-gcp
prefect block register -m prefect_gcp
Another authentication option is to give the worker access to the storage location at runtime through SSH keys.