Managed Airflow (Gen 3) | Managed Airflow (Gen 2) | Managed Airflow (Legacy Gen 1)
This page describes how to use Cloud Run functions to trigger Managed Service for Apache Airflow DAGs in response to events.
Apache Airflow is designed to run DAGs on a regular schedule, but you can also trigger DAGs in response to events. One way to do this is to use Cloud Run functions to trigger Managed Airflow DAGs when a specified event occurs.
You can also:
- Trigger DAGs using only the Airflow REST API.
- Create a function that triggers a DAG when a message is pushed to a Pub/Sub topic.
The example in this guide demonstrates a function that triggers a DAG in response to an event:
- You configure triggers for your function in Cloud Run functions.
- When the function is triggered, it makes a request to trigger a DAG through the Airflow REST API of your Managed Airflow environment. The request contains event's identifier and type, and the event's payload.
- Airflow processes this request and runs the DAG specified in the request. The DAG outputs the data that was passed to it from the function.
Before you begin
This section lists preparatory steps.
Check your environment's networking configuration
This solution does not work in Private IP and VPC Service Controls configurations because it isn't possible to configure connectivity from Cloud Run functions to the Airflow web server in these configurations.
Enable APIs for your project
Console
Enable the Managed Airflow and Cloud Run functions APIs.
Roles required to enable APIs
To enable APIs, you need the serviceusage.services.enable permission. If you
created the project, then you likely already have this permission through the
Owner role (roles/owner). Otherwise, you can get this permission through the
Service Usage Admin role (roles/serviceusage.serviceUsageAdmin).
Learn how to grant roles.
gcloud
Enable the Managed Airflow and Cloud Run functions APIs:
Roles required to enable APIs
To enable APIs, you need the serviceusage.services.enable permission. If you
created the project, then you likely already have this permission through the
Owner role (roles/owner). Otherwise, you can get this permission through the
Service Usage Admin role (roles/serviceusage.serviceUsageAdmin).
Learn how to grant roles.
gcloud services enable cloudfunctions.googleapis.comcomposer.googleapis.com
Enable the Airflow REST API
Depending on your version of Airflow:
- For Airflow 2, the stable REST API is already enabled by default. If your environment has the stable API disabled, then enable the stable REST API.
- For Airflow 1, enable the experimental REST API.
Allow API calls to Airflow REST API using web server network access control
Cloud Run functions can reach out to the Airflow REST API through a IPv4 or IPv6 address.
If you are not sure what will be the calling IP range then use a default
configuration option in
Webserver Access Control which is All IP addresses have access (default)
to not accidentally block your Cloud Run functions. You can always
configure web server network access later.
Get the Airflow web server URL
This example makes REST API requests to the Airflow web server endpoint.
You use the part of the Airflow web interface URL before .appspot.com in your
Cloud Function code.
Console
In the Google Cloud console, go to the Environments page.
Click the name of your environment.
On the Environment details page, go to the Environment configuration tab.
The URL of the Airflow web server is listed in the Airflow web UI item.
gcloud
Run the following command:
gcloud composer environments describe ENVIRONMENT_NAME \
--location LOCATION \
--format='value(config.airflowUri)'
Replace:
ENVIRONMENT_NAMEwith the name of the environment.LOCATIONwith the region where the environment is located.
Get the client_id of the IAM proxy
To make a request to the Airflow REST API endpoint, the function requires the client ID of the Identity and Access Management proxy that protects the Airflow web server.
Managed Airflow does not provide this information directly. Instead, make an unauthenticated request to the Airflow web server and capture the client ID from the redirect URL:
cURL
curl -v AIRFLOW_URL 2>&1 >/dev/null | grep -o "client_id\=[A-Za-z0-9-]*\.apps\.googleusercontent\.com"
Replace AIRFLOW_URL with the URL of the Airflow web interface.
In the output, search for the string following client_id. For example:
client_id=836436932391-16q2c5f5dcsfnel77va9bvf4j280t35c.apps.googleusercontent.com
Python
Save the following code in a file called get_client_id.py. Fill in your
values for project_id, location, and composer_environment, then run
the code in Cloud Shell or your local environment.
Upload a DAG to your environment
Upload a DAG to your environment. The following example DAG outputs the received DAG run configuration. You trigger this DAG from a function, which you create later in this guide.
import datetime
import airflow
from airflow.operators.bash_operator import BashOperator
with airflow.DAG(
'composer_sample_trigger_response_dag',
start_date=datetime.datetime(2026, 1, 1),
# Not scheduled, trigger only
schedule=None) as dag:
# Print the dag_run's configuration, which includes information about the
# Cloud Storage object change.
print_gcs_info = BashOperator(
task_id='print_gcs_info', bash_command='echo {{ dag_run.conf }}}}')
Deploy a function that triggers the DAG
You can deploy a function using your preferred language supported by Cloud Run functions or Cloud Run. This tutorial demonstrates a Cloud Function implemented in Python and Java.
Specify function configuration parameters
Trigger: Select an Eventarc trigger or several triggers for your function.
For more information about creating triggers, see Create triggers with Eventarc. For example, you can trigger functions from Cloud Storage using Eventarc.
Service account: The service account that you specify for the trigger must have enough permissions to trigger DAGs in Managed Airflow environments.
We recommend to follow the minimum privilege principle and grant only the Composer User (
composer.user) role to it. For more information about configuring permissions, see Roles and permissions for Cloud Run targets.Function entry point:
(Python) When adding code for this example, select the Python 3.10 or later runtime and specify
trigger_dagas the entry point.
Add requirements
Specify the dependencies in the requirements.txt file:
Add function code
Put the following code to the main.py file and make the following
replacements:
Replace the value of the
client_idvariable with theclient_idvalue that you obtained earlier.Replace the value of the
webserver_idvariable with your tenant project ID, which is a part of the Airflow web interface URL before.appspot.com. You obtained the Airflow web interface URL earlier.Specify the Airflow REST API version that you use:
- If you use the stable Airflow REST API, set the
USE_EXPERIMENTAL_APIvariable toFalse. - If you use the experimental Airflow REST API, no changes are needed. The
USE_EXPERIMENTAL_APIvariable is already set toTrue.
- If you use the stable Airflow REST API, set the
Test your function
To check that your function and DAG work as intended:
- Wait until your function deploys.
- Trigger the function according to the specified trigger. You can also trigger the function manually by selecting the Test the function action for it in Google Cloud console.
- Check the DAG page in the Airflow web interface. The DAG should have one active or already completed DAG run.
- In the Airflow UI, check task logs for this run. You should see
that the
print_gcs_infotask outputs the data received from the function to the logs:
Example output:
[2021-04-04 18:25:44,778] {bash_operator.py:154} INFO - Output:
[2021-04-04 18:25:44,781] {bash_operator.py:158} INFO - Triggered from GCF:
{bucket: example-storage-for-gcf-triggers, contentType: text/plain,
crc32c: dldNmg==, etag: COW+26Sb5e8CEAE=, generation: 1617560727904101,
... }
[2021-04-04 18:25:44,781] {bash_operator.py:162} INFO - Command exited with
return code 0h
What's next
- Access Airflow UI
- Access Airflow REST API
- Write DAGs
- Write Cloud Run functions
- Cloud Storage triggers