1
---
2
name: managed-airflow-dag-authoring
3
description: >-
4
Provides guidance for authoring Apache Airflow DAGs in Managed Service for
5
Apache Airflow (MSAA; formerly Cloud Composer). Covers environment context
6
discovery, Airflow 2 vs 3 compatibility, authoring best practices, and local/remote
7
validation processes. Use when creating or extending an Airflow DAG. Don't
8
use when authoring Python code unrelated to Airflow DAGs.
9
metadata:
10
version: "1.0.1"
11
category: BigDataAndAnalytics
12
---
13
14
# GCP Managed Airflow DAG Authoring Guide
15
16
This skill guides you through authoring and validating Apache Airflow DAGs for
17
Managed Service for Apache Airflow (MSAA; formerly Cloud Composer) environments.
18
19
--------------------------------------------------------------------------------
20
21
## Attribution
22
23
**MUST READ**: Before executing any `gcloud`, REST API, or MCP command, you
24
MUST read [Usage Attribution](references/attribution.md). For commands executed
25
directly in the terminal (never in generated scripts, Terraform, or user code),
26
prefix `gcloud` with
27
`CLOUDSDK_METRICS_REQUEST_ATTRIBUTION="agent-skills/1.0 (skill:managed-airflow-dag-authoring)"`
28
and append `agent-skills/1.0 (skill:managed-airflow-dag-authoring)` to `User-Agent` headers.
29
30
## Phase 1: Context Discovery
31
32
Before writing any DAG code, you MUST understand the constraints (e.g. version
33
of Airflow) and capabilities of your target environment if user is willing to
34
provide them.
35
36
### 1.1 Identify Target Environment & Access
37
38
Determine if you have direct access to the target Managed Airflow environment,
39
local development environment or if you are working offline (only changing local
40
files without validation).
41
42
* **If environment access is available:** Use `gcloud` to inspect the
43
environment (see Section 1.3).
44
* **If offline:** Rely on user provided details.
45
46
### 1.2 Identify Development Environment
47
48
Determine if a local development environment is available.
49
50
* Check if `composer-dev` CLI is installed.
51
* Check if a local Python environment with `airflow` is available.
52
53
### 1.3 Inspect Target Environment (if available and requested)
54
55
Run the following commands to discover version constraints:
56
57
1. **Get Airflow/Image Version:**
58
59
```bash
60
gcloud composer environments describe {env_name} \
61
--location {region} \
62
--format="value(config.softwareConfig.imageVersion)"
63
```
64
65
2. **Get Installed Packages (Versions):**
66
67
```bash
68
gcloud composer environments describe {env_name} \
69
--location {region} \
70
--format="value(config.softwareConfig.pypiPackages)"
71
```
72
73
3. **Get DAGs GCS Bucket:**
74
75
```bash
76
gcloud composer environments describe {env_name} \
77
--location {region} \
78
--format="value(config.dagGcsPrefix)"
79
```
80
81
--------------------------------------------------------------------------------
82
83
## Phase 2: DAG Authoring Best Practices
84
85
### 2.1 General Airflow Best Practices
86
87
* **Idempotency:** Every task SHOULD be idempotent. Running it multiple times
88
with the same inputs (e.g., execution date) SHOULD produce the same result
89
and not duplicate data.
90
* **No Top-Level Code Execution:** Do NOT execute database queries, external
91
API calls, or heavy computations at the top level of the DAG file (outside
92
of tasks/operators). This code runs every few seconds during DAG parsing and
93
will degrade performance.
94
* **Explicit Catchup:** Always set `catchup=False` in the DAG definition
95
unless historical backfilling is explicitly required.
96
* **Use Airflow Variables/Connections:** Never hardcode credentials or
97
environment-specific configs. Use `Variable.get()` (with
98
`deserialize_json=True` if applicable) and `BaseHook.get_connection()`.
99
Access variables via Jinja templates (e.g., `{{ var.value.my_var }}`) to
100
avoid database calls during DAG parsing.
101
102
### 2.2 Airflow 2 vs Airflow 3 Compatibility
103
104
Use managed-airflow-migrations skill to navigate adjusting the code to
105
specific target Airflow version.
106
107
--------------------------------------------------------------------------------
108
109
## Phase 3: Validation Process
110
111
You MUST validate DAGs before concluding your task.
112
113
### 3.1 Local Validation (Offline/Pre-deployment)
114
115
#### 3.1.1 Static Analysis & Linting
116
117
Use `ruff` or `pylint` if available.
118
119
```bash
120
ruff check path/to/dag.py
121
```
122
123
* If targeting Airflow 3, check with Airflow 3 rules if rulesets are
124
available.
125
126
#### 3.1.2 Local Dev Environment (`composer-dev`)
127
128
If the user has `composer-dev` configured:
129
130
1. Copy the DAG to the local directory with DAGs:
131
132
```bash
133
cp path/to/dag.py $(composer-dev describe {local_env} --format="value(dags_directory)")
134
```
135
136
2. Verify parsing:
137
138
```bash
139
composer-dev run-airflow-cmd {local_env} dags list-import-errors
140
```
141
142
### 3.2: Target Environment Validation
143
144
Only perform these steps if you have GCP access and are authorized to deploy to
145
a target environment.
146
147
### 3.2.1 Deploy to GCS
148
149
Upload the DAG to the target environment's GCS bucket:
150
151
```bash
152
gcloud storage cp path/to/dag.py gs://{target_bucket}/dags/
153
```
154
155
### 3.2.2 Verify via Airflow CLI
156
157
Wait 1-2 minutes for the scheduler to parse the file, then run:
158
159
1. **Check for Import Errors:**
160
161
```bash
162
gcloud composer environments run {env_name} \
163
--location {region} \
164
dags list-import-errors
165
```
166
167
*Pass Criteria:* Output should be "No data found" or empty.
168
169
2. **Verify DAG is Listed:**
170
171
```bash
172
gcloud composer environments run {env_name} \
173
--location {region} \
174
dags list | grep {dag_id}
175
```
176
177
### 3.2.3 Monitor Cloud Logging
178
179
Check for runtime parsing errors in Cloud Logging:
180
181
```query
182
resource.type="cloud_composer_environment"
183
resource.labels.environment_name="{env_name}"
184
log_id("airflow-scheduler")
185
severity>=ERROR
186
textPayload:"{dag_file_name}"
187
```
188
189
--------------------------------------------------------------------------------
190
191
## Definition of Done
192
193
* DAG code adheres to Airflow version constraints of the target environment.
194
* DAG code follows best practices (no top-level execution, idempotent if
195
possible).
196
* DAG parses locally without import errors.
197
* (If environment is available) DAG is deployed to the target environment and
198
verified to have no import errors.
199