0
0
AirflowHow-ToBeginner · 3 min read

How to Create Variable in Airflow: Simple Guide

In Airflow, you create a variable using the Variable class from airflow.models. Use Variable.set('key', 'value') to create or update a variable, and Variable.get('key') to retrieve it in your DAGs or tasks.
📐

Syntax

The Variable class manages key-value pairs in Airflow. Use Variable.set(key, value) to create or update a variable. Use Variable.get(key, default_var=None) to retrieve the value by key. You can also provide a default value if the key does not exist.

python
from airflow.models import Variable

# Create or update a variable
Variable.set('my_key', 'my_value')

# Retrieve a variable
value = Variable.get('my_key', default_var='default_value')
print(value)
Output
my_value
💻

Example

This example shows how to create a variable named env with the value production and then retrieve it inside a PythonOperator task.

python
from airflow import DAG
from airflow.operators.python import PythonOperator
from airflow.models import Variable
from datetime import datetime

def print_env():
    env = Variable.get('env', default_var='development')
    print(f'Environment is: {env}')

default_args = {'start_date': datetime(2024, 1, 1)}

dag = DAG('variable_example', default_args=default_args, schedule_interval='@daily')

# Set variable
Variable.set('env', 'production')

print_task = PythonOperator(
    task_id='print_env_task',
    python_callable=print_env,
    dag=dag
)
Output
Environment is: production
⚠️

Common Pitfalls

  • Trying to access a variable that does not exist without a default value causes an error.
  • Setting variables with complex data types requires serialization (e.g., JSON strings).
  • Modifying variables in code does not update the Airflow UI unless you use Variable.set().
python
from airflow.models import Variable

# Wrong: This will raise an error if 'missing_key' does not exist
# value = Variable.get('missing_key')

# Right: Provide a default value to avoid errors
value = Variable.get('missing_key', default_var='default')
print(value)  # Output: default
Output
default
📊

Quick Reference

MethodDescriptionExample
Variable.set(key, value)Create or update a variableVariable.set('foo', 'bar')
Variable.get(key)Get variable value by keyVariable.get('foo') # returns 'bar'
Variable.get(key, default_var)Get variable or default if missingVariable.get('missing', default_var='default')

Key Takeaways

Use Variable.set('key', 'value') to create or update Airflow variables.
Retrieve variables with Variable.get('key', default_var) to avoid errors if missing.
Variables store strings; use JSON for complex data types.
Always provide default values when getting variables to prevent runtime errors.
Changes to variables via code reflect immediately; UI changes require refresh.