Push Work to Serverless Computing Infrastructure ¶
Push work pools are a special type of work pool that allows Prefect Cloud to submit flow runs for execution to serverless computing infrastructure without running a worker.
Push work pools currently support execution in AWS ECS tasks, Azure Container Instances, Google Cloud Run jobs, and Modal.
In this guide you will:
Create a push work pool that sends work to Amazon Elastic Container Service (AWS ECS), Azure Container Instances (ACI), Google Cloud Run, or Modal
Deploy a flow to that work pool
Execute a flow without having to run a worker or agent process to poll for flow runs
You can automatically provision infrastructure and create your push work pool using the prefect work-pool create CLI command with the --provision-infra flag.
This approach greatly simplifies the setup process.
Let's explore automatic infrastructure provisioning for push work pools first, and then we'll cover how to manually set up your push work pool.
With Perfect Cloud you can provision infrastructure for use with an AWS ECS, Google Cloud Run, ACI push work pool.
Push work pools in Prefect Cloud simplify the setup and management of the infrastructure necessary to run your flows.
However, setting up infrastructure on your cloud provider can still be a time-consuming process.
Prefect can dramatically simplify this process by automatically provisioning the necessary infrastructure for you.
We'll use the prefect work-pool create CLI command with the --provision-infra flag to automatically provision your serverless cloud resources and set up your Prefect workspace to use a new push pool.
To use automatic infrastructure provisioning, you'll need to have the relevant cloud CLI library installed and to have authenticated with your cloud provider.
Using the --provision-infra flag will automatically set up your default AWS account to be ready to execute flows via ECS tasks.
In your AWS account, this command will create a new IAM user, IAM policy, ECS cluster that uses AWS Fargate, VPC, and ECR repository if they don't already exist.
In your Prefect workspace, this command will create an AWSCredentials block for storing the generated credentials.
Here's an abbreviated example output from running the command:
After infrastructure provisioning completes, you will be logged into your new ECR repository and the default Docker build namespace will be set to the URL of the registry.
While the default namespace is set, you will not need to provide the registry URL when building images as part of your deployment process.
To take advantage of this, you can write your deploy scripts like this:
example_deploy_script.py
fromprefectimportflowfromprefect.deploymentsimportDeploymentImage@flow(log_prints=True)defmy_flow(name:str="world"):print(f"Hello {name}! I'm a flow running in a ECS task!")if__name__=="__main__":my_flow.deploy(name="my-deployment",work_pool_name="my-work-pool",image=DeploymentImage(name="my-repository:latest",platform="linux/amd64",))
This will build an image with the tag <ecr-registry-url>/my-image:latest and push it to the registry.
Your image name will need to match the name of the repository created with your work pool. You can create new repositories in the ECR console.
Using the --provision-infra flag will automatically set up your default Azure account to be ready to execute flows via Azure Container Instances.
In your Azure account, this command will create a resource group, app registration, service account with necessary permission, generate a secret for the app registration, and create an Azure Container Registry, if they don't already exist.
In your Prefect workspace, this command will create an AzureContainerInstanceCredentials block for storing the client secret value from the generated secret.
Here's an abbreviated example output from running the command:
After infrastructure provisioning completes, you will be logged into your new Azure Container Registry and the default Docker build namespace will be set to the URL of the registry.
While the default namespace is set, any images you build without specifying a registry or username/organization will be pushed to the registry.
To take advantage of this functionality, you can write your deploy scripts like this:
example_deploy_script.py
fromprefectimportflowfromprefect.deploymentsimportDeploymentImage@flow(log_prints=True)defmy_flow(name:str="world"):print(f"Hello {name}! I'm a flow running on an Azure Container Instance!")if__name__=="__main__":my_flow.deploy(name="my-deployment",work_pool_name="my-work-pool",image=DeploymentImage(name="my-image:latest",platform="linux/amd64",))
This will build an image with the tag <acr-registry-url>/my-image:latest and push it to the registry.
Using the --provision-infra flag will allow you to select a GCP project to use for your work pool and automatically configure it to be ready to execute flows via Cloud Run.
In your GCP project, this command will activate the Cloud Run API, create a service account, and create a key for the service account, if they don't already exist.
In your Prefect workspace, this command will create a GCPCredentials block for storing the service account key.
Here's an abbreviated example output from running the command:
After infrastructure provisioning completes, you will be logged into your new Artifact Registry repository and the default Docker build namespace will be set to the URL of the repository.
While the default namespace is set, any images you build without specifying a registry or username/organization will be pushed to the repository.
To take advantage of this functionality, you can write your deploy scripts like this:
example_deploy_script.py
fromprefectimportflowfromprefect.deploymentsimportDeploymentImage@flow(log_prints=True)defmy_flow(name:str="world"):print(f"Hello {name}! I'm a flow running on Cloud Run!")if__name__=="__main__":my_flow.deploy(name="my-deployment",work_pool_name="above-ground",image=DeploymentImage(name="my-image:latest",platform="linux/amd64",))
This will build an image with the tag <region>-docker.pkg.dev/<project>/<repository-name>/my-image:latest and push it to the repository.
Using the --provision-infra flag will trigger the creation of a ModalCredentials block in your Prefect Cloud workspace. This block will store your Modal API token, which is used to authenticate with Modal's API. By default, the token for your current Modal profile will be used for the new ModalCredentials block. If Prefect is unable to discover a Modal API token for your current profile, you will be prompted to create a new one.
That's it!
You're ready to create and schedule deployments that use your new push work pool.
Reminder that no worker is needed to run flows with a push work pool.
Using existing resources with automatic infrastructure provisioning¶
If you already have the necessary infrastructure set up, Prefect will detect that upon work pool creation and the infrastructure provisioning for that resource will be skipped.
For example, here's how prefect work-pool create my-work-pool --provision-infra looks when existing Azure resources are detected:
Provisioning infrastructure for an existing push work pool¶
If you already have a push work pool set up, but haven't configured the necessary infrastructure, you can use the provision-infra sub-command to provision the infrastructure for that work pool.
For example, you can run the following command if you have a work pool named "my-work-pool".
prefectwork-poolprovision-inframy-work-pool
Prefect will create the necessary infrastructure for the my-work-pool work pool and provide you with a summary of the changes to be made:
If you prefer to set up your infrastructure manually, don't include the --provision-infra flag in the CLI command.
In the examples below, we'll create a push work pool via the Prefect Cloud UI.
To push work to ECS, AWS credentials are required.
Create a user and attach the AmazonECS_FullAccess permissions.
From that user's page create credentials and store them somewhere safe for use in the next section.
To push work to Azure, an Azure subscription, resource group and tenant secret are required.
Create Subscription and Resource Group
In the Azure portal, create a subscription.
Create a resource group within your subscription.
Create App Registration
In the Azure portal, create an app registration.
In the app registration, create a client secret. Copy the value and store it somewhere safe.
Add App Registration to Resource Group
Navigate to the resource group you created earlier.
Choose the "Access control (IAM)" blade in the left-hand side menu. Click "+ Add" button at the top, then "Add role assignment".
Go to the "Privileged administrator roles" tab, click on "Contributor", then click "Next" at the bottom of the page.
Click on "+ Select members". Type the name of the app registration (otherwise it may not autopopulate) and click to add it. Then hit "Select" and click "Next". The default permissions associated with a role like "Contributor" might not always be sufficient for all operations related to Azure Container Instances (ACI). The specific permissions required can depend on the operations you need to perform (like creating, running, and deleting ACI container groups) and your organization's security policies. In some cases, additional permissions or custom roles might be necessary.
Click "Review + assign" to finish.
A GCP service account and an API Key are required, to push work to Cloud Run.
Create a service account by navigating to the service accounts page and clicking Create. Name and describe your service account, and click continue to configure permissions.
The service account must have two roles at a minimum, Cloud Run Developer, and Service Account User.
Once the Service account is created, navigate to its Keys page to add an API key. Create a JSON type key, download it, and store it somewhere safe for use in the next section.
A Modal API token is required to push work to Modal.
Create a Modal API token by navigating to Settings in the Modal UI. In the API Tokens section of the Settings page, click New Token.
Copy the token ID and token secret and store them somewhere safe for use in the next section.
Our push work pool will store information about what type of infrastructure our flow will run on, what default values to provide to compute jobs, and other important execution environment parameters. Because our push work pool needs to integrate securely with your serverless infrastructure, we need to start by storing our credentials in Prefect Cloud, which we'll do by making a block.
Navigate to the blocks page, click create new block, and select AWS Credentials for the type.
For use in a push work pool, region, access key, and access key secret must be set.
Provide any other optional information and create your block.
Navigate to the blocks page and click the "+" at the top to create a new block. Find the Azure Container Instance Credentials block and click "Add +".
Locate the client ID and tenant ID on your app registration and use the client secret you saved earlier. Be sure to use the value of the secret, not the secret ID!
Provide any other optional information and click "Create".
Navigate to the blocks page, click create new block, and select GCP Credentials for the type.
For use in a push work pool, this block must have the contents of the JSON key stored in the Service Account Info field, as such:
Provide any other optional information and create your block.
Navigate to the blocks page, click create new block, and select Modal Credentials for the type.
For use in a push work pool, this block must have the token ID and token secret stored in the Token ID and Token Secret fields, respectively.
Now navigate to the work pools page.
Click Create to start configuring your push work pool by selecting a push option in the infrastructure type step.
Each step has several optional fields that are detailed in the work pools documentation.
Select the block you created under the AWS Credentials field.
This will allow Prefect Cloud to securely interact with your ECS cluster.
Fill in the subscription ID and resource group name from the resource group you created.
Add the Azure Container Instance Credentials block you created in the step above.
Each step has several optional fields that are detailed in the work pools documentation.
Select the block you created under the GCP Credentials field.
This will allow Prefect Cloud to securely interact with your GCP project.
Each step has several optional fields that are detailed in the work pools documentation.
Select the block you created under the Modal Credentials field.
This will allow Prefect Cloud to securely interact with your Modal account.
Create your pool and you are ready to deploy flows to your Push work pool.
Deployment details are described in the deployments concept section.
Your deployment needs to be configured to send flow runs to our push work pool.
For example, if you create a deployment through the interactive command line experience, choose the work pool you just created. If you are deploying an existing prefect.yaml file, the deployment would contain:
work_pool:name:my-push-pool
Deploying your flow to the my-push-pool work pool will ensure that runs that are ready for execution will be submitted immediately, without the need for a worker to poll for them.
Serverless infrastructure may require a certain image architecture
Note that serverless infrastructure may assume a certain Docker image architecture; for example, Google Cloud Run will fail to run images built with linux/arm64 architecture. If using Prefect to build your image, you can change the image architecture through the platform keyword (e.g., platform="linux/amd64").
With your deployment created, navigate to its detail page and create a new flow run.
You'll see the flow start running without ever having to poll the work pool, because Prefect Cloud securely connected to your serverless infrastructure, created a job, ran the job, and began reporting on its execution.