About blocks
Prefect blocks store configuration and provide an interface for interacting with external systems. Blocks expose methods that provide functionality specific to these systems. For example, you can use blocks to:- Download data from, or upload data to, an S3 bucket
- Query data from, or write data to, a database
- Send a message to a Slack channel.
prefect block type ls
from the CLI or navigate to the Blocks page in the UI and click +.
Prefect built-in blocks
Commonly used block types come built-in with Prefect. You can create and use these block types through the UI and without installing any additional packages.| Block | Slug | Description |
|---|---|---|
| Azure | azure | Stores data as a file on Azure Data Lake and Azure Blob Storage. |
| Custom Webhook | custom-webhook | Calls custom webhooks. |
| Discord Webhook | discord-webhook | Calls Discord webhooks. |
| GCS | gcs | Store data as a file on Google Cloud Storage. |
| GitHub | github | Interacts with files stored on public GitHub repositories. |
| Kubernetes Cluster Config | kubernetes-cluster-config | Stores configuration for interaction with Kubernetes clusters. |
| Local File System | local-file-system | Stores data as a file on a local file system. |
| Mattermost Webhook | mattermost-webhook | Sends notifications through a provided Mattermost webhook. |
| Microsoft Teams Webhook | ms-teams-webhook | Sends notifications through a provided Microsoft Teams webhook. |
| Opsgenie Webhook | opsgenie-webhook | Sends notifications through a provided Opsgenie webhook. |
| Pager Duty Webhook | pager-duty-webhook | Sends notifications through a provided PagerDuty webhook. |
| Remote File System | remote-file-system | Stores data as a file on any remote file system that supports fsspec. |
| S3 | s3 | Stores data as a file on AWS S3. |
| Secret | secret | Stores a secret value. The value will be obfuscated when this block is logged or shown in the UI. |
| Sendgrid Email | sendgrid-email | Sends notifications through Sendgrid email. |
| Slack Webhook | slack-webhook | Sends notifications through a provided Slack webhook. |
| SMB | smb | Stores data as a file on a SMB share. |
| Twilio SMS | twilio-sms | Sends notifications through Twilio SMS. |
Blocks in Prefect integration libraries
Some block types that appear in the UI can be created immediately, with the corresponding integration library installed for use. For example, an AWS Secret block can be created, but not used until theprefect-aws library is installed.
Anyone can create block types and optionally share them with the community.
Find available block types in many of the published Prefect integrations libraries.
If a block type is not available in the UI, you can register it through the CLI.
Use existing block types
Blocks are classes that subclass theBlock base class. You can instantiate and use them like normal classes.
Instantiate blocks
To instantiate a block that stores an S3 bucket value, use theS3Bucket block:
Save blocks
To retrieve this saved value use the.save() method:
overwrite=True:
S3Bucket by setting the name parameter to a new value:
Load blocks
You can use the block name to load the block:S3Bucket block from above, run the following:
Delete blocks
Delete a block with the.delete() method:
Create a new block type
To create a custom block type, define a class that subclassesBlock. The Block base class builds
on Pydantic’s BaseModel, so you can declare custom blocks just like a Pydantic model.
Here’s a block that represents a cube and holds information about the length of each edge in inches:
Cube block type in a flow:
Secret fields
All block values are encrypted before being stored. If you have values that you would not like visible in the UI or in logs, use theSecretStr field type provided by Pydantic to automatically obfuscate those values.
You can use this functionality for fields that store credentials such as passwords and API tokens.
Here’s an example of an AWSCredentials block that uses SecretStr:
aws_secret_access_key has the SecretStr type hint assigned to it,
the value of that field is not exposed if the object is logged:
SecretDict field type allows you to add a dictionary field to your block
that automatically obfuscates values at all levels in the UI or in logs.
This functionality is useful for blocks where typing or structure of secret fields is not known until configuration time.
Here’s an example of a block that uses SecretDict:
system_secrets is obfuscated when system_configuration_block is displayed, but system_variables show up in plain-text:
Block type metadata
Set metadata fields on a block type’s subclass to control how a block displays. Available metadata fields include:| Property | Description |
|---|---|
| _block_type_name | Display name of the block in the UI. Defaults to the class name. |
| _block_type_slug | Unique slug used to reference the block type in the API. Defaults to a lowercase, dash-delimited version of the block type name. |
| _logo_url | URL pointing to an image that should be displayed for the block type in the UI. Default to None. |
| _description | Short description of block type. Defaults to docstring, if provided. |
| _code_example | Short code snippet shown in UI for how to load/use block type. Default to first example provided in the docstring of the class, if provided. |
Nested blocks
Blocks are composable: a block can be used within other blocks. You can create a block type that uses functionality from another block type by declaring it as an attribute. Nestable blocks are loosely coupled, and configuration can be changed for each block independently. This allows sharing configuration across multiple use cases. For example, here’s an expandedAWSCredentials block that enables an authenticated session through the boto3 library:
AWSCredentials block within an S3Bucket block to provide authentication when interacting with an S3 bucket:
S3Bucket block with previously saved AWSCredentials block values in order to interact with the configured S3 bucket:
AWSCredentials are saved with my_s3_bucket and are not usable with any other blocks.
Update custom Block types
Here’s an example of how to add a bucket_folder field to your custom S3Bucket block; it represents the default path to
read and write objects from (this field exists on our implementation).
Add the new field to the class definition:
Register blocks
Prefect comes with many blocks pre-registered and ready to use. If you do not have a block available for use, you can register it. Register blocks from a Python module available in the current environment with a CLI command:.py file, you can register the block with the CLI command: