airflow

Export all airflow connections to new environment


I'm trying to migrate all the existing airflow connections to a new airflow.

I was looking at the cli options airflow connections --help, it gives an option to list but doesn't give an option to export/import to/from json format.

Is there a way via cli/airflow ui to migrate connections across multiple airflows?


Solution

  • You can either connect directly to the Airflow meta db and dump those connections, then load them in a separate database. However, if you want to automate something like this, you can get started by dumping them in a CSV file:

    from airflow.utils import db
    from airflow.models import Connection
    import csv
    
    outfile = open('myconnections.csv', 'w')
    outcsv = csv.writer(outfile)
    
    with db.create_session() as session:
        connections = session.query(Connection).all()
    
    conn_list = [
        [getattr(c, column.name) for column in Connection.__mapper__.columns]
        for c in connections
    ]
    outcsv.writerows(conn_list)
    outfile.close()
    

    After that, you can load that to a new DB manually or with a similar script.

    IMPORTANT: if you have enabled encryption, the passwords stored for these connections will be encrypted, and when you load them to the new DB, you must use the identical fernet key, or otherwise you won't be able to decrypt them.