<@ULVA73B9P> how can I create multiple Prefect dep...
# marvin-in-the-wild
o
@Marvin how can I create multiple Prefect deployments using the Python API. These deployments need to include
pull_steps
as well as an image for the deployment. I looked at the APIs but not seeing an obvious way to do this with the Python API.
m
It appears that you can create multiple Prefect deployments using the Python API with the
deploy
function and specifying the necessary parameters. Here’s a relevant excerpt from the Prefect documentation that illustrates how to do this: ### Creating Multiple Deployments
Copy code
python
from prefect import deploy, flow

@flow(log_prints=True)
def buy():
    print("Buying securities")

if __name__ == "__main__":
    deploy(
        buy.to_deployment(name="dev-deploy", work_pool_name="my-dev-work-pool"),
        buy.to_deployment(name="prod-deploy", work_pool_name="my-prod-work-pool"),
        image="my-registry/my-image:dev",
        push=False,
    )
In this example, two deployments are created from the same flow, but with different work pools. You can also create multiple deployments from different flows. ### Adding
pull_steps
and Custom Image To include
pull_steps
, use the appropriate configuration in your deployment YAML or programmatically. Here's an example of including `pull_steps`:
Copy code
yaml
deploy:
  - pull:
      - prefect_aws.deployments.steps.pull_from_s3:
          requires: prefect-aws>=0.3.0
          bucket: my-bucket
          folder: "{{ push-code.folder }}"
          credentials: null
For programmatically defining
pull_steps
and including an image during deployment, you would need to ensure your deployment includes those details. ### Key Steps and Links 1. Define Flows Define your flows as functions decorated with
@flow
in Prefect. 2. Deployment Programmatically - Use
deploy()
to create and manage deployments. - Specify your
pull_steps
and image as needed. 3. Useful Documentation and Issues - Deployment Guide for additional context on deployment configurations. - Example issue related to deployment settings: Issue #10899. 4. Image and Steps Customization - For adding pull steps and image configuration, ensure you've configured them correctly in the deployment function or YAML. This approach should serve as a decent starting point for creating multiple Prefect deployments in a programmatic manner. If you encounter issues, refer to the Prefect documentation and relevant GitHub issues for troubleshooting steps.
o
@Marvin can you show me code example on how to create a deployment with a flow that uses an image AND includes pull_steps?
m
To create a Prefect deployment that includes an image and
pull_steps
, you can combine various elements from the documentation. Here's a code example of how to do this: ### Example Code
Copy code
python
from prefect import flow
from prefect.deployments import Deployment
from prefect.infrastructure import KubernetesJob
from prefect_aws.deployments.steps import pull_from_s3

# Define your flow
@flow(log_prints=True)
def my_workflow():
    print("Running my workflow")

# Define the image and Kubernetes job
k8s_job = KubernetesJob(
    image="my-registry/my-image:latest"
)

# Create the deployment
deployment = Deployment.build_from_flow(
    flow=my_workflow,
    name="my-deployment",
    infrastructure=k8s_job,
    pull_steps=[
        pull_from_s3(
            requires="prefect-aws",
            bucket="my-bucket",
            folder="my-folder",
            credentials="{{ prefect.blocks.aws-credentials.my-credentials }}"
        )
    ]
)

# Apply the deployment
if __name__ == "__main__":
    deployment.apply()
### Key Points - `KubernetesJob`: Specifies the Docker image for the deployment. - `pull_steps`: Utilizes `prefect_aws`'s
pull_from_s3
step to pull data from S3. - `Deployment.build_from_flow`: Combines the flow, infrastructure, and pull steps into a single deployment. ### Useful Links - Prefect Deployment Guide - Prefect AWS Steps This script sets up a flow that utilizes a specified Docker image and S3 pull steps for the deployment. Adapt the example to fit your specific requirements as needed.