Time to complete
This tutorial takes approximately 45 minutes to complete.Assumed knowledge
To get the most out of this tutorial, make sure you have an understanding of:- XCom basics. See Passing data between Airflow tasks.
- Airflow connections. See Manage connections in Apache Airflow.
Prerequisites
- The Astro CLI with an Astro project running Astro Runtime 11.5.0 or higher (Airflow 2.9.2 or higher). To set up a custom XCom backend with older versions of Airflow, see Custom XCom backends.
- An account in either AWS, GCP, or Azure with permissions to create and configure an object storage container.
Step 1: Set up your object storage container
First, you need to set up the object storage container in your cloud provider where Airflow will store the XComs.- AWS
- GCP
- Azure
- Log into your AWS account and create a new S3 bucket. Ensure that public access to the bucket is blocked. You don’t need to enable bucket versioning.
-
Create a new IAM policy for Airflow to access your bucket. You can use the JSON configuration below or use the AWS GUI to replicate what you see in the screenshot. Replace
<your-bucket-name>with the name of your S3 bucket. -
Save your policy under the name
AirflowXComBackendAWSS3. -
Create an IAM user called
airflow-xcomwith the AWS credential typeAccess key - Programmatic accessand attach theAirflowXComBackendAWSS3policy to this user. -
Create an access key of the type
Third-party-servicefor yourairflow-xcomuser. Make sure to save the Access Key ID and the Secret Access Key in a secure location to use in Step 3.
For other ways to set up a connection between Airflow and AWS, see the Amazon provider documentation.
Step 2: Install the required provider packages
To use the Object Storage XCom Backend, you need to install the Common IO provider package and the provider package for your object storage container provider.- AWS
- GCP
- Azure
Add the Common IO and Amazon provider packages to your
requirements.txt file. Note that you need to install the s3fs extra to use the Amazon provider package with the object storage feature.Step 3: Set up your Airflow connection
An Airflow connection is necessary to connect Airflow with your object storage container provider. In this tutorial, you’ll use the Airflow UI to configure your connection.-
Start your Astro project by running:
- AWS
- GCP
- Azure
-
In the Airflow UI, go to Admin > Connections and click Create. Fill in the following fields:
- Conn Id:
my_aws_conn - Conn Type:
Amazon Web Services - AWS Access Key ID:
<your access key> - AWS Secret Access Key:
<your secret key>
- Conn Id:
Step 4: Configure your custom XCom backend
Configuring a custom XCom backend with object storage can be done by setting environment variables in your Astro project.If you are setting up a custom XCom backend for an Astro deployment, you have to set the following environment variables for your deployment. See Environment variables for instructions.
-
Add the
AIRFLOW__CORE__XCOM_BACKENDenvironment variable to your.envfile. It defines the class to use for the custom XCom backend implementation.
- AWS
- GCP
- Azure
-
Add the
AIRFLOW__COMMON_IO__XCOM_OBJECTSTORAGE_PATHenvironment variable to your.envfile to define the path in your S3 bucket where the XComs will be stored in the form of<connection id>@<bucket name>/<path>. Use the connection id of the Airflow connection you defined in step 3 and replace<my-bucket>with your S3 bucket name.
-
Add the
AIRFLOW__COMMON.IO__XCOM_OBJECTSTORAGE_THRESHOLDenvironment variable to your.envfile to determine when Airflow will store XComs in the object storage vs the metadata database. The default value is-1which will store all XComs in the metadata database. Set the value to0to store all XComs in the object storage. Any positive value means any XCom with a byte size greater than the threshold will be stored in the object storage and any XCom with a size equal to or less than the threshold will be stored in the metadata database. For this tutorial we will set the threshold to1000bytes, which means any XCom larger than 1KB will be stored in the object storage. -
Optional. Define the
AIRFLOW__COMMON_IO__XCOM_OBJECTSTORE_COMPRESSIONenvironment variable to compress the XComs stored in the object storage with fsspec supported compression algorithms likezip. The default value isNone. -
Restart your Airflow project by running:
Step 5: Test your custom XCom backend
We will use a simple DAG to test your custom XCom backend.-
Create a new file in the
dagsdirectory of your Astro project calledcustom_xcom_backend_test.pyand add the following code: -
Manually trigger the
custom_xcom_backend_testDAG in the Airflow UI and navigate to the XCom tab of thepush_objectstask. You should see that thesmall_objXCom shows its value, meaning it was stored in the metadata database, since it is smaller than 1KB. Thebig_objXCom shows the path to the object in the object storage containing the serialized value of the XCom.