This script automates the process of rotating a Composer environment to a new Cloud Composer environment. It performs the following steps:
- Save the DAG inventory locally and take a snapshot of the old Composer environment.
- Pause all DAGs instantly in the old Composer environment to ensure no overlap in DAG runs.
- Load the snapshot into the new Composer environment.
While in-place updates are preferred for Composer environments (e.g., minor version updates using the Google beta-provider), there are scenarios where environment rotation is unavoidable:
- Changing network configurations like tags, VPC networks, or IP ranges.
- Updating the encryption key.
- Resolving an unhealthy environment status that doesn't go away.
- downgrading environment versions
- migrating resources to a new region
Safe updates that do not require environment rotation include:
- Modifying labels.
- Updating software configurations such as image versions, PyPI packages, and environment variables.
- Adjusting workload configurations like the scheduler and triggerer.
- Ensure you have the Google Cloud SDK (
gcloud
) installed and configured. - Have appropriate permissions to manage Composer environments and Cloud Storage.
- The
pause_all_dags.py
script should be available in thedags/
directory.
The script accepts the following arguments:
-p
: Project ID where Composer is deployed.-c
: Old Composer Environment Name.-l
: Old Composer Environment Location.-C
: New Composer Environment Name.-L
: New Composer Environment Location.-g
: GCS Location for Snapshot (e.g.,gs://your-bucket/composer-snapshots/
).-d
: Old DAG Folder (GCS path to the DAG folder of the old environment).
./rotate-composer.sh \
-p "your-project-id" \
-c "old-composer-env" \
-l "us-central1" \
-C "new-composer-env" \
-L "us-central1" \
-g "gs://your-bucket/composer-snapshots/" \
-d "gs://your-old-dag-folder-path"
-p
(Project ID): The Google Cloud project ID where the Composer environments are deployed.-c
(Old Composer Environment Name): The name of the Composer environment you wish to rotate from.-l
(Old Composer Environment Location): The region of the old Composer environment.-C
(New Composer Environment Name): The name of the new Composer environment you wish to rotate to.-L
(New Composer Environment Location): The region of the new Composer environment.-g
(GCS Location for Snapshot): The Cloud Storage bucket path where the snapshot of the old environment will be stored.-d
(Old DAG Folder): The Cloud Storage path to the DAG folder of the old Composer environment.
-
Begin Rotation:
- The script starts by printing a message indicating the rotation process has begun.
-
Save Airflow DAG Inventory:
- Runs
gcloud composer environments run
to list all DAGs in the old environment. - Saves the inventory to a local file named
${OLD_COMPOSER_ENV}_${OLD_COMPOSER_LOCATION}_inventory
.
- Runs
-
Save Snapshot of Old Environment:
- Uses
gcloud beta composer environments snapshots save
to create a snapshot. - Stores the snapshot in the specified GCS location.
- Uses
-
Upload
pause_all_dags.py
DAG:- Copies the
pause_all_dags.py
script to the old environment's DAG folder to pause all DAGs.
- Copies the
-
Wait for DAG Sync:
- Polls the old environment to check if the
pause_all_dags
DAG is available and not paused. - Continues checking every 15 seconds until the DAG is ready.
- Polls the old environment to check if the
-
Trigger
pause_all_dags
DAG:- Triggers the
pause_all_dags
DAG to pause all other DAGs in the old environment.
- Triggers the
-
Remove
pause_all_dags.py
DAG:- Removes the
pause_all_dags.py
script from the old environment's DAG folder.
- Removes the
-
Load Snapshot into New Environment:
- Uses
gcloud beta composer environments snapshots load
to load the snapshot into the new environment.
- Uses
-
Complete Rotation:
- Prints a completion message indicating the rotation is complete.
- The script uses
set -e
to exit immediately if a command exits with a non-zero status. - Ensure that the
pause_all_dags.py
script is available in thedags/
directory from which the script is run. - The rotation process can take up to 30 minutes to complete.
- After the rotation, update any external services or configurations that point to the old Composer environment.
- Permission Errors:
- Ensure you have the necessary IAM permissions for Composer and Cloud Storage operations.
- Environment Variables:
- Double-check that all environment variables and configurations are correctly set in the new Composer environment.
- DAG Synchronization Issues:
- If the script hangs while waiting for the
pause_all_dags
DAG to sync, verify that the DAG was correctly copied to the old environment's DAG folder.
- If the script hangs while waiting for the